Skip to content

perf(documents): strip ix: tags from escaped TextBlocks inside lxml - #2014

Merged
dgunning merged 1 commit into
mainfrom
perf/ixbrl-escaped-fastpath
Nov 20, 2026
Merged

dgunning merged 1 commit into
mainfrom
perf/ixbrl-escaped-fastpath

Conversation

@dgunning

Copy link
Copy Markdown
Owner

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_escaped drop ix:exclude and unwrap ix: 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

  • The ix: tag names a document uses are collected once per tree.
  • Detection uses tag-filtered iterdescendants(), and removal uses strip_elements() and strip_tags(). All three run inside libxml2, so no Python object is created for each XHTML node.
  • _is_xbrl_element and 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_escaped per parse parse (min) parse (median)
main 17.9 ms 0.384 s 0.400 s
this PR 8.3 ms 0.376 s 0.390 s
v6.0.0 – 0.356 s 0.359 s

That 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

  • Projection output: identical to main's implementation on all 14,836 escaped TextBlock children in the 86 inline fixtures, 4,632 of which the projection actually changes.
  • Facts: all 247,860 facts extracted from those 86 fixtures are byte-identical to main (compared by per-file SHA-256).
  • Existing tests: 642 offline tests across the 37 files that cover inline XBRL extraction pass.
  • New test: test_projection_reads_each_documents_own_ix_tags in tests/issues/regression/test_issue_1770.py pins 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

#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>
@dgunning
dgunning merged commit a59b6d6 into main Nov 20, 2026
11 checks passed
@dgunning
dgunning deleted the perf/ixbrl-escaped-fastpath branch November 20, 2026 22:37
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