Repository navigation
Conversation
…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>
18 of 32 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
In
xml.etree.ElementTree.ElementreplacePyArg_ParseTuplewith Argument Clinic and add a generated vectorcall. Keys that are not strings in**extranow 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_signaturekeeps the documented(tag, /, attrib={}, **extra); the/is new in the signature,tagwas already positional-only.attribcan still be passed by keyword, which the impl pops fromextra.Merging the keywords into the attributes now copies them with PyDict_Copy() instead of PyDict_New() plus PyDict_Update(), and pops
attribwith_Py_ID(attrib)rather than creating a string per call; SubElement shares that helper.pyperf microbenchmark (PGO+LTO, run serially):
Not significant: fromstring(doc) (C parser, unchanged path).
Benchmark code:
This is a rebuild of gh-161597 on top of the landed AC VC generation work. cc: @asvetlov