Skip to content

perf(sanitizer): avoid Token() allocations in stripIter#4443

Open
jvoisin wants to merge 1 commit into
miniflux:mainfrom
jvoisin:stroptogs
Open

perf(sanitizer): avoid Token() allocations in stripIter#4443
jvoisin wants to merge 1 commit into
miniflux:mainfrom
jvoisin:stroptogs

Conversation

@jvoisin

@jvoisin jvoisin commented Jul 5, 2026

Copy link
Copy Markdown
Collaborator

Tokenizer.Token() allocates a Token struct and parses the full attribute slice for every start tag, even though stripIter only consumes text tokens. Switch to checking the TokenType from Next() and calling Text() only for text tokens.

Shared by StripTags and TruncateHTML, both called per entry:

StripTags 247KB -> 71KB/op (-71%), 12623 -> 4423 allocs (-65%)
TruncateHTML 19KB -> 7.9KB/op (-59%), 819 -> 297 allocs (-64%)

Both roughly 2x faster on tag-heavy input.

Have you followed these guidelines?

Tokenizer.Token() allocates a Token struct and parses the full
attribute slice for every start tag, even though stripIter only
consumes text tokens. Switch to checking the TokenType from Next()
and calling Text() only for text tokens.

Shared by StripTags and TruncateHTML, both called per entry:

  StripTags     247KB -> 71KB/op (-71%), 12623 -> 4423 allocs (-65%)
  TruncateHTML  19KB  -> 7.9KB/op (-59%),   819 ->  297 allocs (-64%)

Both roughly 2x faster on tag-heavy input.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant