Treebuilder.inspecificscope optimizations#2485
Open
JH-A-Kim wants to merge 6 commits intojhy:masterfrom
Open
Conversation
This reverts commit 3ecf0fe.
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.
Performance Optimization: Algorithmic Improvement for HtmlTreeBuilder.inSpecificScope
Executive Summary
This PR addresses a significant performance bottleneck in the HTML parser's scope-checking logic. Through profiling with Java Flight Recorder (JFR), we identified that
HtmlTreeBuilder.inSpecificScopeand its dependency onArrays.binarySearchwere responsible for a dominant portion of CPU cycles.By replacing repeated binary search lookups with a pre-calculated bit flag on the$O(n \log m)$ to a strictly linear $O(n)$ .
Tagobject, we have shifted the algorithmic complexity of stack traversal fromKey Performance Gains
In synthetic workloads testing deep nesting (depths up to 120), the following improvements were observed:
CPU Profiling (JFR Samples)
Arrays.binarySearchHtmlTreeBuilder.inSpecificScopeTreeBuilder.stepParserTokeniser.readCall Stack Hot Paths
StringUtil.inSortedHtmlTreeBuilder.inSpecificScopeHtmlTreeBuilderState.processAlgorithmic Improvement
StringUtil.inSorted, which triggersArrays.binarySearchfor every step. Total complexity:applet,caption,html,table, etc.) are now pre-marked with aTag.InScopebit flag during tag setup. The traversal now uses a constant-time bitwise check (el.tag().is(Tag.InScope)), resulting in a linearMemory & Stability
Memory behavior remains neutral. The gains are strictly algorithmic and do not increase heap pressure or Garbage Collection frequency.
Justification
This optimization fundamentally improves the scalability of the parser for modern, deeply nested web documents. By reducing the per-step overhead of scope checking to a single bitwise instruction, we ensure Jsoup remains performant and deterministic in high-throughput or resource-constrained environments.