Repository navigation
perf(documents): strip ix: tags from escaped TextBlocks inside lxml - #2014
Merged
Merged
Conversation
#1903 made _project_escaped drop ix:exclude and unwrap ix: elements at every depth of an escaped TextBlock child (GH #1770). It found them by testing every descendant from Python, three times per child: once to decide whether to copy, then twice over the copy. Nested TextBlocks put the same subtrees through that hundreds of times per filing. On Apple's FY2026 10-K (0000320193-26-000026) the step took 17.9 ms per parse, and full parsing slowed about 8% between 6.0.0 and 6.1.0. The ix: tag names a document uses are now collected once per tree, and tag-filtered iterdescendants(), strip_elements() and strip_tags() do the detection and removal in libxml2. _is_xbrl_element and the new path share one predicate, _is_ix_tag. Measured on that filing (min of 7, interleaved): _project_escaped 17.9 -> 8.3 ms, parse 0.384 -> 0.376 s, median 0.400 -> 0.390 s. The remaining regression is the deep copy and serialization the correct output needs (8.4 ms) and the HTML table changes in #1904. Equivalence: projection output identical to the previous implementation on all 14,836 escaped-TextBlock children in the 86 inline fixtures (4,632 of them changed by projection); all 247,860 extracted facts byte-identical. A new test pins the per-tree tag cache and fails when the cache ignores the tree. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Why
Parsing an inline XBRL 10-K got about 8% slower between 6.0.0 and 6.1.0. Timing the same saved HTML (Apple's FY2026 10-K,
0000320193-26-000026) at each commit in that range puts the step at #1903: the minimum parse time goes from 0.354s to 0.382s. That PR made_project_escapeddropix:excludeand unwrapix:elements at every depth of an escaped TextBlock (#1770), which was a correct fix. It found those elements by testing every descendant from Python, three times per child: once to decide whether to copy and twice more over the copy. Nested TextBlocks sent the same subtrees through that loop hundreds of times per filing.What changes
ix:tag names a document uses are collected once per tree.iterdescendants(), and removal usesstrip_elements()andstrip_tags(). All three run inside libxml2, so no Python object is created for each XHTML node._is_xbrl_elementand the new code share one predicate,_is_ix_tag, instead of two copies of the rule.Measured
Same HTML, fastest of 7 parses, interleaved with main, three rounds:
_project_escapedper parseThat recovers about a third of the slowdown. The rest is the deep copy and serialization the correct output needs (8.4ms) and the HTML table changes in #1904.
Two earlier versions were measured and dropped. Running XPath once per TextBlock child was slower than main, because each call has a fixed setup cost and it ran about 900 times per parse. A whole-document XPath with string functions cost 12ms on its own, while a single Python pass over the tree costs about 3ms.
Verification
test_projection_reads_each_documents_own_ix_tagsintests/issues/regression/test_issue_1770.pypins the per-tree tag cache. It passes on main and on this branch, and it fails if the cache ignores which tree it belongs to (checked by mutating the code).AI tool: Claude Code (Claude Opus 5.5).
🤖 Generated with Claude Code