Repository navigation
Studio: cheaper per-token work in web page Markdown extraction - #16286
Conversation
The title-button scan and the renderer now share one attribute dict and one hidden / ARIA-heading check per start tag, skip implicit-close recovery for tags that close nothing, find the innermost open element with a C-speed list lookup, call guarded helpers only when their own early-return test would pass, keep recorded attrs uncopied, and run SiteLinks.clean only where a header frame measures text. Output is unchanged.
…g stays linear [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
@codex review |
1 similar comment
|
@codex review |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 2463a3803b
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| break | ||
| open_tags = self._open_tags | ||
| if tag not in _VOID_TAGS and tag in open_tags: | ||
| i = len(open_tags) - 1 - open_tags[::-1].index(tag) |
There was a problem hiding this comment.
Avoid copying the entire open-tag stack on each closing tag
For deeply nested HTML, open_tags[::-1] copies the entire remaining stack even when the closing tag matches its last element. Closing N nested elements therefore becomes quadratic instead of linear; the identical change in _TitleButtonScan.endtag adds another full-stack copy per close. Testing '<div>' * 20000 + 'text' + '</div>' * 20000 (220 KB) with main_content=True took approximately 2.00 seconds after this change versus 0.46 seconds before it, while 10,000 levels took 0.55 versus 0.24 seconds. Preserve a last-element fast path or use a reverse lookup that does not copy the stack in both locations so fetched pages with deep nesting do not incur this regression.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 84d4a3c: one non-copying lookup (top of stack first, then a reverse walk only for misnested tags) serves both end-tag paths; 20,000 nested div/span pairs now render in 0.53s vs 0.79s on base and 8.05s before the fix, with a no-slice regression test.
|
@codex review |
1 similar comment
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 84d4a3cc04
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| if tag not in stack: | ||
| return -1 | ||
| while stack[i] != tag: | ||
| i -= 1 |
There was a problem hiding this comment.
Search backward without a forward membership scan
For deeply nested HTML with shallow misnesting, tag not in stack scans the entire enclosing prefix even when the matching tag is only one slot below the top, making repeated closes quadratic. The previous copying issue is fixed; fresh evidence is '<div>' * n + '<section><span>text</section>' * n + '</div>' * n, where each </section> now scans all enclosing divs in both end-tag paths. With main_content=True, 10,000 levels took 3.43 seconds versus 0.82 seconds on the parent, and 20,000 took 12.97 versus 1.69 seconds. Use a bounded backward search that also handles absent tags, avoiding the forward membership pass so malformed fetched pages retain linear lookup work.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 91478e4: the lookup now searches backward with operator.indexOf(reversed(stack), tag), no forward membership pass and no copy; the misnested case at 20,000 levels takes 0.82s vs 1.25s on base (7.31s on the previous head), with a test for it.
…the top stays cheap [skip ci]
|
@codex review |
1 similar comment
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
What changes
web_fetchturns each fetched HTML page into Markdown withhtml_to_markdown(main_content=True). Most pages get a single tokenizer pass, and the per-token Python work in the renderer and in the title-button scan that runs alongside it is the main cost after the stdlib tokenizer. This PR cuts that work and leaves the output alone:dict(attrs)and one hidden / ARIA-heading check per start tag. Before, each of them built its own dict and ran its own checks._is_hidden_elementand_is_aria_headingskip their string work when the attribute is missing._IMPLICIT_CLOSERSset (_ANY_IMPLICIT_CLOSER). Before, it walked the open-tag stack for everyspan,a,imgand so on.endtag, renderer_exit_tag) is found by_innermost_index: the top of the stack first, then a backward C-level search (operator.indexOf(reversed(stack), name)) that never copies the stack, where it used to be a Python loop._prepare_foreign_html_start,_prepare_heading_start,_prepare_table_start,_close_header_framesand the scan's_close_headingsare only called when their own early-return test would let them do something._emittakes a direct route when no header frame, heading or heading-link is open, and it now callsSiteLinks.cleanonly when a header frame measures the text.lineno/offsetdirectly.Results
The corpus is 39 real pages fetched once through
_fetch_url_raw: Wikipedia articles and big tables, Python / Django / MDN / Kubernetes / Docker / GitHub REST docs, PyPI, GitHub README, issues and PR lists, BBC, Guardian, CNN, Ars Technica, HN, Lobsters, LWN, Discourse, the Bootstrap accordion docs and the WHATWG tables spec, from 13 KiB to 3.8 MiB. Base and head ran interleaved in one process over 11 rounds, comparing the sum of per-page minimums:An A/A run (base against itself) moved 0.5%. Three more runs on a loaded host gave -15.0% to -16.1%.
The title-button scan's own cost over the recorded tokens of the corpus fell from 285 ms to 199 ms. That was measured before the shared-attribute change, which removes more from it.
Deep nesting
The lookup touches only the slots above the match, so closing deep or misnested pages stays linear (
main_content=True, one process, busy host):divdiv+ 20,000 misnested<section><span>t</section>div+ 20,000 stray</span>Re-check
A second corpus of 20 live pages (Wikipedia, Python / MDN / Django / Docker / Kubernetes / GitHub REST docs, PyPI, GitHub, HN, Lobsters, LWN, BBC, Guardian, Ars Technica, WHATWG tables, Bootstrap), interleaved base / head / base over 7 rounds: -15.9% on the sum of per-page minimums (per-page median -13.5%), A/A within 2.4%.
html_to_markdownin both modes and theSiteLinks.stripoutput are byte identical on all pages, and 3,000 to 4,000 fuzz documents per run show 0 mismatches on Python 3.11.17, 3.12.3, 3.13.12 and 3.14.3.Tests
html_to_markdownoutput andSiteLinks.stripoutput are identical on all 39 pages. The full_fetch_page_textresult (network stubbed,max_chars=16000) is identical too (same SHA-1 over all pages)._emit,>=to>in the header-frame guard) were each caught.tests/test_html_to_md_fast_paths.py: golden outputs produced by the pre-change converter (ARIA-heading title buttons, hidden variants, implicit closers, header frames, foreign content, skipped subtrees), the attribute-check formulas compared with the old ones, the closer union, and the innermost-tag lookup (no stack copy, deep and misnested pages). The goldens pass on base as well._html_to_md/_fetch_page_text, plus web / html / fetch / markdown / search): base and head fail the same IDs, all pre-existing and outside this change:test_diffusion_offload_prefetch::test_resident_onload_skips_the_copy_stream_wait_when_fenced,test_diffusion_small_host_prefetch::test_group_offload_installs_prefetch_only_on_small_host_pipes, an import error intest_h3_stream_prefetch.py, andtest_web_search_fallback::test_primary_eight_parallel_successes_all_use_provider. That last one is flaky under xdist and fails on both arms when run alone.Not covered
lineno/offset, which the existing token replay already relies on.