⚡ Bolt: Remove intermediate allocations in normalize_search_text#609
⚡ Bolt: Remove intermediate allocations in normalize_search_text#609madmax983 wants to merge 1 commit into
normalize_search_text#609Conversation
💡 What: Rewrote `normalize_search_text` in `src/skills.rs` to construct the normalized string directly, avoiding intermediate `String` and `Vec` allocations. Fixed existing clippy warnings.
🎯 Why: `.split_whitespace().collect::<Vec<_>>().join(" ")` causes unnecessary heap allocations.
📊 Impact: Reduces memory allocations during skill indexing by building the normalized string in a single pre-allocated pass.
🔬 Measurement: Run `cargo test --lib skills`.
Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
|
👋 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. |
There was a problem hiding this comment.
Code Review
This pull request introduces several optimizations and cleanups across the codebase. In src/run/swebench.rs, a socket read loop is simplified to a while let loop. In src/skills.rs, the normalize_search_text function is refactored to construct the normalized string in a single pass, eliminating intermediate allocations, and unit tests are added to verify its behavior. In src/stream/sweep_webhook.rs, timeout error matching is made more explicit, and a map().unwrap_or() chain is simplified to map_or(). The reviewer provided a valuable performance optimization suggestion for normalize_search_text to iterate over bytes instead of characters, which avoids UTF-8 decoding overhead for ASCII-only checks.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| fn normalize_search_text(text: &str) -> String { | ||
| let mut out = String::with_capacity(text.len()); | ||
| let mut in_word = false; | ||
| for ch in text.chars() { | ||
| if ch.is_ascii_alphanumeric() || ch == '$' || ch == '@' || ch == '/' { | ||
| if !in_word && !out.is_empty() { | ||
| out.push(' '); | ||
| } | ||
| out.push(ch.to_ascii_lowercase()); | ||
| in_word = true; | ||
| } else { | ||
| out.push(' '); | ||
| in_word = false; | ||
| } | ||
| } | ||
| out.split_whitespace().collect::<Vec<_>>().join(" ") | ||
| out | ||
| } |
There was a problem hiding this comment.
The current implementation of normalize_search_text iterates over the characters of the input string using text.chars(). Since the allowed characters (is_ascii_alphanumeric, $, @, /) are all ASCII, any non-ASCII character or byte will naturally fall into the else branch and be treated as a word boundary.
By iterating over bytes using text.as_bytes() instead of text.chars(), we can completely avoid the overhead of UTF-8 decoding for the input string. This provides a significant performance boost, especially when processing large amounts of search text during indexing, while remaining fully safe and functionally identical.
| fn normalize_search_text(text: &str) -> String { | |
| let mut out = String::with_capacity(text.len()); | |
| let mut in_word = false; | |
| for ch in text.chars() { | |
| if ch.is_ascii_alphanumeric() || ch == '$' || ch == '@' || ch == '/' { | |
| if !in_word && !out.is_empty() { | |
| out.push(' '); | |
| } | |
| out.push(ch.to_ascii_lowercase()); | |
| in_word = true; | |
| } else { | |
| out.push(' '); | |
| in_word = false; | |
| } | |
| } | |
| out.split_whitespace().collect::<Vec<_>>().join(" ") | |
| out | |
| } | |
| fn normalize_search_text(text: &str) -> String { | |
| let mut out = String::with_capacity(text.len()); | |
| let mut in_word = false; | |
| for &b in text.as_bytes() { | |
| if b.is_ascii_alphanumeric() || b == b'$' || b == b'@' || b == b'/' { | |
| if !in_word && !out.is_empty() { | |
| out.push(' '); | |
| } | |
| out.push(b.to_ascii_lowercase() as char); | |
| in_word = true; | |
| } else { | |
| in_word = false; | |
| } | |
| } | |
| out | |
| } |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## trunk #609 +/- ##
==========================================
- Coverage 85.19% 85.19% -0.01%
==========================================
Files 114 114
Lines 65408 65418 +10
==========================================
+ Hits 55725 55733 +8
- Misses 9683 9685 +2 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
💡 What: Rewrote
normalize_search_textinsrc/skills.rsto construct the normalized string directly, avoiding intermediateStringandVecallocations, and fixed existing clippy warnings.🎯 Why:
.split_whitespace().collect::<Vec<_>>().join(" ")causes unnecessary heap allocations.📊 Impact: Reduces memory allocations during skill indexing by building the normalized string in a single pre-allocated pass.
🔬 Measurement: Run
cargo testandcargo bench.PR created automatically by Jules for task 7326076267954398302 started by @madmax983