From 995084795237635c5f7f7de544bbd4ac43826058 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 10 Jun 2026 09:52:28 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Early=20exit=20in=20is=5Fli?= =?UTF-8?q?kely=5Fcode=5Ftext=20heuristic?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Adds an early return to `is_likely_code_text` to significantly reduce time spent scanning non-text files. The optimization bails out early as soon as the non-printable byte threshold is exceeded. Also logs the learning in the Bolt journal. Co-authored-by: mudcube <101564+mudcube@users.noreply.github.com> --- .jules/bolt.md | 4 ++++ .../src/services/reference_updater/detectors/generic.rs | 9 +++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/.jules/bolt.md b/.jules/bolt.md index 541c0ab51..968c362c9 100644 --- a/.jules/bolt.md +++ b/.jules/bolt.md @@ -17,3 +17,7 @@ ## 2025-05-19 - File Discovery Allocations **Learning:** In `discover_importing_files`, `WalkBuilder` results were being converted to `PathBuf` via `.map(|e| e.into_path())` *before* filtering. This caused allocations for every single file in the workspace (including excluded files and directories). **Action:** Filter `ignore::DirEntry` directly using `entry.file_type()` and `entry.path()` before mapping to `PathBuf`. This avoids allocations for non-matching files. + +## 2024-06-10 - Early Exit in Code Text Heuristics +**Learning:** In Rust, heuristic functions that count characters or bytes (e.g., `is_likely_code_text` in `crates/mill-services/src/services/reference_updater/detectors/generic.rs`) should be optimized with an early exit as soon as a failure/success threshold is met. This significantly improves performance on 'mismatch' cases by avoiding full scans of the sampled data. +**Action:** When implementing threshold-based heuristic scans, add an early return within the loop to short-circuit as soon as the threshold guarantees the outcome. Ensure default return values (like `sample_len > 0`) correctly handle empty inputs to preserve original functionality. diff --git a/crates/mill-services/src/services/reference_updater/detectors/generic.rs b/crates/mill-services/src/services/reference_updater/detectors/generic.rs index bea6dc076..a8584089e 100644 --- a/crates/mill-services/src/services/reference_updater/detectors/generic.rs +++ b/crates/mill-services/src/services/reference_updater/detectors/generic.rs @@ -519,12 +519,17 @@ fn is_likely_code_text(content: &str) -> bool { let sample = &content.as_bytes()[..sample_len]; let mut non_printable = 0usize; for &b in sample { - let is_text = b == b'\n' || b == b'\r' || b == b'\t' || (b >= 0x20 && b <= 0x7e); + let is_text = b == b'\n' || b == b'\r' || b == b'\t' || (0x20..=0x7e).contains(&b); if !is_text { non_printable += 1; + // Early exit optimization: if non-printable characters exceed the threshold, + // we immediately know this is not likely text, avoiding a full 8KB scan. + if non_printable * 20 >= sample_len { + return false; + } } } - non_printable * 20 < sample_len + sample_len > 0 } fn is_obviously_irrelevant_extension(ext: &str) -> bool {