Skip to content

⚡ Bolt: Fast manual HTML parsing to avoid regexp engine overhead - #184

Open
MEKXH wants to merge 1 commit into
devfrom
bolt-optimize-html-to-text-6248319092574302039
Open

⚡ Bolt: Fast manual HTML parsing to avoid regexp engine overhead#184
MEKXH wants to merge 1 commit into
devfrom
bolt-optimize-html-to-text-6248319092574302039

Conversation

@MEKXH

@MEKXH MEKXH commented May 27, 2026

Copy link
Copy Markdown
Owner

💡 What: Replaced regex-based string replacements (htmlScriptRe, htmlStyleRe, htmlTagRe) in htmlToText with an optimized manual single-pass parsing loop utilizing strings.Builder and a custom indexIgnoreCase helper.

🎯 Why: The original implementation utilized 3 successive regexp.ReplaceAllString calls to strip tags from HTML text. The regex engine incurs significant runtime overhead, state machine evaluation, and generates an intermediate string allocation for each pass. This caused the web fetching tool to run slowly, especially when querying large HTML documents.

📊 Impact: The optimization runs approximately ~6.3x faster (22896 ns/op down to 3656 ns/op based on benchmarking) by removing intermediate string replacements and entirely avoiding the regex engine.

🔬 Measurement: A temporary benchmark test BenchmarkHtmlToTextOptimized in internal/tools/bench_temp_test.go confirms the speedup. Correctness is verified via exact string comparison in unit tests.


PR created automatically by Jules for task 6248319092574302039 started by @MEKXH

Replaced the regex-based implementation in `htmlToText` with manual string parsing.
`regexp.MustCompile` and `ReplaceAllString` calls have significant overhead and generate intermediate allocations.
Manual parsing with `strings.Builder` and the `indexIgnoreCase` helper removes regex state machine execution and runs up to 6.3x faster for html to text conversion.

Co-authored-by: MEKXH <59291264+MEKXH@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.

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