⚡ perf: Optimize allowlist matching to avoid string slicing memory overhead#82
⚡ perf: Optimize allowlist matching to avoid string slicing memory overhead#82google-labs-jules[bot] wants to merge 1 commit into
Conversation
Replaced string slicing in `_matched_line` with `content.find()` using start/end bounds in a new `_line_contains` function. This prevents O(N) memory allocations per check, which is especially noticeable for very long lines or large files.
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
💡 What: Refactored
_matched_lineto a new_line_containshelper function inhooks/secret_scanner.py. Instead of creating a new sliced string (content[start:end]) to check if an allowlist marker is present, the new implementation searches directly within the bounds of the original string usingcontent.find(marker, start, end) != -1.🎯 Why: The previous
_matched_lineimplementation sliced the string, which created a copy of the substring in memory. This happens insidescan_contentand can occur multiple times for matches that fail validation or are on allowlisted lines. For extremely large files or very long single lines, the string slicing caused significant CPU time spent in allocation and O(N) memory overhead per call.📊 Measured Improvement:
Measured with
tracemallocandtimeiton a synthesized large string matching scenario (~20MB string, single line):PR created automatically by Jules for task 4249636977872444921 started by @zknpr