Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion main.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,10 @@ def _clean_env_kv(value: Optional[str], key: str) -> Optional[str]:
]

BATCH_SIZE = 500
# Optimization: Pre-calculate batch keys to avoid string formatting in the hot loop.
# This saves ~1.1s of CPU time per 10k batches by avoiding f-string interpolation.
Copy link

Copilot AI Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment claims "~1.1s of CPU time per 10k batches" but this appears inconsistent with the PR description which states "Micro-benchmark showed ~3x speedup for the dictionary construction loop (0.5s vs 1.6s for 5M ops)". For 10k batches with BATCH_SIZE=500, that would be 5M operations total (10,000 * 500 = 5,000,000), which aligns with the benchmark. However, the savings would be approximately 1.1s (1.6s - 0.5s) for all 10k batches combined, not "per 10k batches" which suggests repeated measurements. The wording should be clarified to say "per 10k batches" or "for 10k batches" to avoid ambiguity.

Suggested change
# This saves ~1.1s of CPU time per 10k batches by avoiding f-string interpolation.
# This saves ~1.1s of CPU time for 10k batches by avoiding f-string interpolation.

Copilot uses AI. Check for mistakes.
_BATCH_KEYS = [f"hostnames[{i}]" for i in range(BATCH_SIZE)]

MAX_RETRIES = 10
RETRY_DELAY = 1
FOLDER_CREATION_DELAY = 5 # <--- CHANGED: Increased from 2 to 5 for patience
Expand Down Expand Up @@ -500,7 +504,10 @@ def push_rules(
"group": str(folder_id),
}
for j, hostname in enumerate(batch):
data[f"hostnames[{j}]"] = hostname
if j < len(_BATCH_KEYS):
data[_BATCH_KEYS[j]] = hostname
else:
data[f"hostnames[{j}]"] = hostname

Comment on lines +507 to 511
Copy link

Copilot AI Jan 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fallback condition else at line 509 is unreachable because batches are created with a maximum size of BATCH_SIZE (500), and _BATCH_KEYS is pre-calculated with exactly BATCH_SIZE elements. Since j enumerates over a batch that never exceeds BATCH_SIZE elements, j will always be in the range [0, BATCH_SIZE-1], making the condition j < len(_BATCH_KEYS) always true. The else branch can never execute.

Suggested change
if j < len(_BATCH_KEYS):
data[_BATCH_KEYS[j]] = hostname
else:
data[f"hostnames[{j}]"] = hostname
data[_BATCH_KEYS[j]] = hostname

Copilot uses AI. Check for mistakes.
try:
_api_post_form(client, f"{API_BASE}/{profile_id}/rules", data=data)
Expand Down
Loading