Skip to content

⚡ Bolt: Optimize text sanitization in utils.py#2814

Merged
danielaskdd merged 3 commits intomainfrom
bolt/optimize-sanitize-text-14023176920321041783
Mar 21, 2026
Merged

⚡ Bolt: Optimize text sanitization in utils.py#2814
danielaskdd merged 3 commits intomainfrom
bolt/optimize-sanitize-text-14023176920321041783

Conversation

@danielaskdd
Copy link
Copy Markdown
Collaborator

💡 What: Optimized the sanitize_text_for_encoding utility function by replacing a manual character-by-character loop with pre-compiled regular expressions and re-ordering html.unescape.

🎯 Why: The original implementation was inefficient (character-by-character processing) and prone to crashing on lone surrogate characters due to a redundant .encode("utf-8") check that blocked the intended cleaning logic.

📊 Impact: Provides a ~10-18x speedup for text sanitization tasks and improves robustness by correctly handling invalid Unicode and surrogates introduced via HTML entities.

🔬 Measurement: Verified with benchmarks showing:

  • Clean text: ~18x speedup.
  • Large text (2.2MB): ~12x speedup (0.04s vs 0.4s).
  • All 10 tests in tests/test_write_json_optimization.py passed.

PR created automatically by Jules for task 14023176920321041783 started by @danielaskdd

- Replaced manual character loop with pre-compiled regex in `sanitize_text_for_encoding`.
- Removed redundant `.encode()` check that caused crashes on lone surrogates.
- Re-ordered `html.unescape` to occur before sanitization for better robustness.
- Updated Bolt's journal with performance learnings.

Co-authored-by: danielaskdd <36760595+danielaskdd@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

…t_char in sanitize_text_for_encoding

- narrow _CONTROL_CHAR_PATTERN_ALL to exclude \x7F-\x9F range, preserving
  valid Latin-1 C1 characters (e.g. \x85) used in European language text
- apply replacement_char consistently to control char substitution instead
  of hardcoding empty string deletion
@danielaskdd danielaskdd marked this pull request as ready for review March 21, 2026 07:04
@danielaskdd danielaskdd merged commit 26eba24 into main Mar 21, 2026
5 checks passed
@danielaskdd danielaskdd deleted the bolt/optimize-sanitize-text-14023176920321041783 branch March 21, 2026 07:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant