Skip to content

⚡️ perf(build): skip the Mapping ABC for dicts - #2469

Merged
gaborbernat merged 4 commits into
tox-dev:mainfrom
gaborbernat:perf/build-element-checks
Nov 18, 2026
Merged

gaborbernat merged 4 commits into
tox-dev:mainfrom
gaborbernat:perf/build-element-checks

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

Element(...) and E.<tag>(...) asked isinstance(x, collections.abc.Mapping) for every attribute argument and child, which costs 190–250 ns per call through the ABC machinery, and lowercased every attribute name through a heap buffer.

Exact dicts and exact Element/Text children now answer the shape test without the ABC, and attribute names up to 64 bytes fold on the stack. Attribute values are still read from a key snapshot, so a value whose __iter__ mutates the dict raises the same KeyError as before; tests pin that, including a free-threaded test that builds elements while another thread mutates the dict.

Callgrind, instructions per element or row over 2,000 rows: construct 10,848 → 10,192 (−6%), build-e 28,768 → 24,695 (−14%). A 44-case differential (dict, OrderedDict, dict subclass, non-dict mappings, wide and long names, every error) matches main's output and exceptions.

The builder ran collections.abc.Mapping's isinstance on every argument,
about 100 ns a call. An exact dict, Element or Text now answers the shape
test directly. This applies only when the shape test is
collections.abc.Mapping, the type turbohtml.build passes; any other shape
test still asks isinstance, and a dict subclass or other mapping still
goes through keys() and __getitem__.

Element() keeps the order it had: it snapshots the keys first, then reads
each value with PyDict_GetItemRef, raising KeyError for a key that Python
code removed meanwhile. A value whose iteration changes the dict therefore
behaves as before, and no reference is borrowed across Python code. A
lowercased attribute name up to 64 bytes lives on the stack.

Instructions per element under callgrind (release build, 2000 rows):
construct 10848 -> 10192 (-6.0%), build-e 28768 -> 24695 (-14.2%).
@gaborbernat gaborbernat added the enhancement New feature or request label Nov 18, 2026
@codspeed

codspeed Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by 13.64%

2 warnings

⚠️ 2 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 2 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

⚡ 2 improved benchmarks
✅ 578 untouched benchmarks
⏩ 91 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_feature[build-e] 2.4 ms 2 ms +20.83%
⚡ test_mutating_feature[observe-registrations] 1.2 ms 1.1 ms +6.88%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:perf/build-element-checks (b06e771) with main (a08976e)

Open in CodSpeed

Footnotes

  1. 91 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@gaborbernat
gaborbernat merged commit 00df6b9 into tox-dev:main Nov 18, 2026
2 of 47 checks passed
gaborbernat added a commit to gaborbernat/turbohtml that referenced this pull request Nov 18, 2026
tox-dev#2469 moved the attribute loop of make_element into three helpers,
attr_value, set_element_slot and store_element_attr. Each attribute
then paid for the exact-dict branch, the extra calls and the stack or
heap choice on top of the loop, about 650 Ir per attribute. Element("li",
{"class": ..., "data-i": ...}) rose by 1,300 Ir and build and build-e
by about 1,200 Ir.

The loop is back to the pre-tox-dev#2469 form: one PyObject_GetItem per
attribute, which already raises the same KeyError when a value callback
removes a key. The lowercased name still lives in a 64-byte stack buffer,
so the per-attribute malloc of tox-dev#2469 stays gone. The duplicate definition
of ELEMENT_ATTR_NAME_STACK_BYTES goes with the helpers.

The Mapping shape test of the builder is unchanged.
gaborbernat added a commit to gaborbernat/turbohtml that referenced this pull request Nov 18, 2026
tox-dev#2469 moved the attribute loop of make_element into three helpers,
attr_value, set_element_slot and store_element_attr. Each attribute
then paid for the exact-dict branch, the extra calls and the stack or
heap choice on top of the loop, about 650 Ir per attribute. Element("li",
{"class": ..., "data-i": ...}) rose by 1,300 Ir and build and build-e
by about 1,200 Ir.

The loop is back to the pre-tox-dev#2469 form: one PyObject_GetItem per
attribute, which already raises the same KeyError when a value callback
removes a key. The lowercased name still lives in a 64-byte stack buffer,
so the per-attribute malloc of tox-dev#2469 stays gone. The duplicate definition
of ELEMENT_ATTR_NAME_STACK_BYTES goes with the helpers.

The Mapping shape test of the builder is unchanged.
gaborbernat added a commit that referenced this pull request Nov 18, 2026
#2469 moved the attribute loop of `make_element` into three helpers,
`attr_value`, `set_element_slot` and `store_element_attr`. Each
attribute paid for the extra calls, the exact-dict branch and the
stack-or-heap choice, and `Element("li", {...})` got 1,300 Ir slower
than before #2469. `build` and `build-e` regressed by about 1,200 Ir per
call for the same reason.

This restores the pre-#2469 loop: one `PyObject_GetItem` per attribute,
which already raises the same `KeyError` when a value callback removes a
key. The lowercased name still lives in a 64-byte stack buffer, so the
per-attribute `malloc` that #2469 removed stays gone. The builder's
`Mapping` shape test from #2469 is unchanged, so `build` and `build-e`
keep their gain.

Callgrind, self instructions per call in `_html*.so` (12 calls minus 2,
divided by 10):

| row | pre-#2469 (`ed29a247f`) | current main (`e118657f9`) | this PR |
| ----------------- | ----------------------: |
-------------------------: | ----------------------------: |
| build | 410,780 | 412,047 | 409,951 (-0.20% vs pre-#2469) |
| build-e | 545,166 | 546,296 | 544,224 (-0.17%) |
| construct | 189,200 | 190,500 | 188,400 (-0.42%) |
| shadow-assignment | 906,625 | 908,225 | 908,225 (+0.18%) |

Every row in the element-building set is within +0.01% of pre-#2469
except `shadow-assignment`, which is +0.18%. Its value is identical on
current main, so this PR does not change it.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant