Skip to content

gh-162350: Use Argument Clinic vectorcall for xml.etree.ElementTree.Element - #162653

Open
cmaloney wants to merge 2 commits into
python:mainfrom
cmaloney:landable/ac-vc-etree
Open

cmaloney wants to merge 2 commits into
python:mainfrom
cmaloney:landable/ac-vc-etree

Conversation

@cmaloney

@cmaloney cmaloney commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

In xml.etree.ElementTree.Element replace PyArg_ParseTuple with Argument Clinic and add a generated vectorcall. Keys that are not strings in **extra now raise TypeError on a normal call, as the Python implementation already did, because vectorcall keywords must be strings; an explicit __init__ call and subclasses still accept them. Arity errors use the generated wording ("Element expected at most 2 arguments, got 3").

@text_signature keeps the documented (tag, /, attrib={}, **extra); the / is new in the signature, tag was already positional-only. attrib can still be passed by keyword, which the impl pops from extra.

Merging the keywords into the attributes now copies them with PyDict_Copy() instead of PyDict_New() plus PyDict_Update(), and pops attrib with _Py_ID(attrib) rather than creating a string per call; SubElement shares that helper.

pyperf microbenchmark (PGO+LTO, run serially):

Benchmark base Element
E('a', x='1') 121 ns 78.7 ns: 1.53x faster
E('a', attrib=d) 123 ns 86.1 ns: 1.43x faster
E('a') 41.9 ns 29.4 ns: 1.43x faster
E('a', d) 75.8 ns 55.6 ns: 1.36x faster
SE(E('p'), 'a', x='1') 161 ns 120 ns: 1.34x faster
build() 22.2 us 17.2 us: 1.29x faster
S('a') 49.6 ns 40.8 ns: 1.22x faster
E('a', d, y='2') 103 ns 85.4 ns: 1.21x faster

Not significant: fromstring(doc) (C parser, unchanged path).

Benchmark code:

import pyperf

runner = pyperf.Runner()
setup = """
from xml.etree.ElementTree import Element as E, SubElement as SE, fromstring
d = {'x': '1'}
class S(E): pass
def build():
    root = E('root')
    for i in range(100):
        c = SE(root, 'item', id='1')
        c.append(E('v', d))
    return root
doc = '<root>' + '<item id="1"><v x="1"/></item>' * 100 + '</root>'
"""
for stmt in ["E('a')", "E('a', d)", "E('a', x='1')", "E('a', d, y='2')",
             "E('a', attrib=d)", "S('a')", "SE(E('p'), 'a', x='1')",
             "build()", "fromstring(doc)"]:
    runner.timeit(stmt, stmt, setup)

This is a rebuild of gh-161597 on top of the landed AC VC generation work. cc: @asvetlov

cmaloney and others added 2 commits November 25, 2026 16:31
…Tree.Element

xml.etree.ElementTree.Element replaces PyArg_ParseTuple with Argument
Clinic and gains a generated vectorcall.  Keys that are not strings in
`**extra` now raise TypeError on a normal call, as the Python
implementation already did, because vectorcall keywords must be strings;
an explicit `__init__` call and subclasses still accept them.  Arity
errors use the generated wording ("Element expected at most 2 arguments,
got 3").

`@text_signature` keeps the documented `(tag, /, attrib={}, **extra)`;
the `/` is new in the signature, `tag` was already positional-only.
`attrib` can still be passed by keyword, which the impl pops from
`extra`.

Merging the keywords into the attributes now copies them with
PyDict_Copy() instead of PyDict_New() plus PyDict_Update(), and pops
`attrib` with `_Py_ID(attrib)` rather than creating a string per call;
SubElement shares that helper.

pyperf microbenchmark (PGO+LTO, run serially):

| Benchmark              | base    | Element               |
|------------------------|:-------:|:---------------------:|
| E('a', x='1')          | 121 ns  | 78.7 ns: 1.53x faster |
| E('a', attrib=d)       | 123 ns  | 86.1 ns: 1.43x faster |
| E('a')                 | 41.9 ns | 29.4 ns: 1.43x faster |
| E('a', d)              | 75.8 ns | 55.6 ns: 1.36x faster |
| SE(E('p'), 'a', x='1') | 161 ns  | 120 ns: 1.34x faster  |
| build()                | 22.2 us | 17.2 us: 1.29x faster |
| S('a')                 | 49.6 ns | 40.8 ns: 1.22x faster |
| E('a', d, y='2')       | 103 ns  | 85.4 ns: 1.21x faster |

Not significant: fromstring(doc) (C parser, unchanged path).

Benchmark code:

```python
import pyperf

runner = pyperf.Runner()
setup = """
from xml.etree.ElementTree import Element as E, SubElement as SE, fromstring
d = {'x': '1'}
class S(E): pass
def build():
    root = E('root')
    for i in range(100):
        c = SE(root, 'item', id='1')
        c.append(E('v', d))
    return root
doc = '<root>' + '<item id="1"><v x="1"/></item>' * 100 + '</root>'
"""
for stmt in ["E('a')", "E('a', d)", "E('a', x='1')", "E('a', d, y='2')",
             "E('a', attrib=d)", "S('a')", "SE(E('p'), 'a', x='1')",
             "build()", "fromstring(doc)"]:
    runner.timeit(stmt, stmt, setup)
```

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant