perf: use Uint8Array bitmaps for faster tag ID lookups#1679
Open
TrevorBurnham wants to merge 1 commit intoinikulin:masterfrom
Open
perf: use Uint8Array bitmaps for faster tag ID lookups#1679TrevorBurnham wants to merge 1 commit intoinikulin:masterfrom
TrevorBurnham wants to merge 1 commit intoinikulin:masterfrom
Conversation
Replace Set.has() with Uint8Array bitmap lookups for scope checking and foreign content exit detection. Bitmap lookup is ~3-4x faster than Set.has() for small integer keys like TAG_IDs. Changes: - open-element-stack.ts: Use bitmaps for scope boundary checking - foreign-content.ts: Use bitmap for EXITS_FOREIGN_CONTENT check Also adds SVG-specific benchmark for testing foreign content parsing performance.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR replaces
Set.has()lookups withUint8Arraybitmap lookups for tag ID membership checks in hot paths. Bitmap array indexing is ~3-4x faster thanSet.has()for small integer keys, and TAG_ID values (0-120) are ideal candidates for this optimization.This PR also adds benchmarks for SVG parsing. The effect of this change on those benchmarks is minimal, but they should be useful for preventing future performance regressions and testing other potential optimizations.
Changes
packages/parse5/lib/parser/open-element-stack.tsSet<$>withUint8Arraybitmaps for scope boundary checking:SCOPING_ELEMENTS_HTML_BITMAPSCOPING_ELEMENTS_HTML_LIST_BITMAPSCOPING_ELEMENTS_HTML_BUTTON_BITMAPSCOPING_ELEMENTS_MATHML_BITMAPSCOPING_ELEMENTS_SVG_BITMAPhasInDynamicScope()to use bitmap lookups instead ofSet.has()packages/parse5/lib/common/foreign-content.tsEXITS_FOREIGN_CONTENTSet withEXITS_FOREIGN_CONTENT_BITMAPUint8ArraycausesExit()to use bitmap lookupbench/perf/svg-benchmark.js(new)Micro-benchmark Results
End-to-End Benchmark Results
The bitmap optimization shows neutral to slightly positive results in end-to-end benchmarks (within margin of error).
Why This Approach
bitmap[tagId]vsset.has(tagId)- direct array access vs hash lookup