Repository navigation
⚡️ perf(build): skip the Mapping ABC for dicts - #2469
Conversation
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%).
Merging this PR will improve performance by 13.64%2 warnings
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
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.
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.
#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.
Element(...)andE.<tag>(...)askedisinstance(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/Textchildren 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 sameKeyErroras 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:
construct10,848 → 10,192 (−6%),build-e28,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.