Skip to content

lexbor: Shrink the initial tokenizer pools and mraw cache - #23949

Open
nicolas-grekas wants to merge 2 commits into
php:masterfrom
nicolas-grekas:lexbor-small-pools
Open

nicolas-grekas wants to merge 2 commits into
php:masterfrom
nicolas-grekas:lexbor-small-pools

Conversation

@nicolas-grekas

@nicolas-grekas nicolas-grekas commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

On top of #25431, which is the first commit here.

Dom\HTMLDocument::createFromString() and setting innerHTML allocate about 1.1MB on every call, even for a few bytes of input, because lexbor preallocates 4096 tokens, 4096 token attributes and, in each of the 7 lexbor_mraw_t of a parser, 512 entries for its cache of freed blocks. It's freed after the call, but it's enough to push a small request into a second 2MB memory chunk.

The tree builder uses a single token and these pools grow on demand, so this makes them start with 16 tokens, 256 attributes and 64 cache entries. The change is patch 0026. It's lexbor/lexbor#427, which isn't merged yet. The patch allows setting the sizes at build time, and PHP sets them in config.m4 and config.w32. So php-src doesn't depend on the defaults that upstream picks, and the patch can be replaced by the upstream commit once it's merged.

Parsing a small string then needs about 200KB instead of 1.1MB, and large documents parse as fast as before.

@nicolas-grekas
nicolas-grekas force-pushed the lexbor-small-pools branch 2 times, most recently from 8f41929 to 7a758c4 Compare October 2, 2026 10:36
@nicolas-grekas
nicolas-grekas force-pushed the lexbor-small-pools branch 2 times, most recently from 846fe04 to ad352ea Compare October 6, 2026 13:53
lexbor_array_obj_init() and lexbor_array_obj_expand() allocated 8 times
the memory they need on 64-bit platforms, and lexbor_array_obj_delete()
moved 8 times the bytes of the remaining entries. This is patch 0025,
merged upstream as lexbor/lexbor@f4cbbcd.

ext/dom uses such an array as its work list when it converts the parsed
tree to libxml2, so gh25118.phpt needs a lower memory_limit to still
fail during that conversion.
The HTML tokenizer preallocated 4096 tokens and 4096 token attributes,
and each lexbor_mraw_t preallocated 512 entries for its cache of freed
blocks, 7 of them per parser. This made Dom\HTMLDocument::createFromString()
and setting innerHTML allocate about 1.1MB even for a few bytes of input.

The tree builder uses a single token and these pools grow on demand, so
start with 16 tokens, 256 attributes and 64 cache entries.

This is patch 0026, the pending lexbor/lexbor#427. It allows setting
the sizes at build time, which PHP does in config.m4 and config.w32.
@nicolas-grekas

Copy link
Copy Markdown
Contributor Author

I moved the lexbor_array_obj_t fix to #25431. lexbor/lexbor#428 is merged upstream, and it's the bigger win. This PR is now only about the pool sizes, on top of #25431.

lexbor/lexbor#427 is still open. The maintainer likes the #ifndef form, and the default values are what's left to decide. The patch here now matches that form, and PHP sets the three sizes in its build. So php-src doesn't depend on the defaults that upstream picks, and the patch can be replaced once #427 is merged.

@kocsismate @ndossche WDYT? Parsing a small string needs about 200KB instead of 1.1MB.

Comment thread NEWS
after a dot segment in a path, replacement file drive paths, the output
encoding used for percent-encoding, the URLSearchParams tail pointer and
fragment serialization without a query. (alexandre-daubois)
. Merge patch lexbor/lexbor@f4cbbcd, fixing lexbor_array_obj_t allocating 8

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this too low level for a NEWS entry? I'm wondering if it would be possible to mention something directly related to end users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants