GeezerVison is showing github.com — gh-150942: Speed up ElementTree attribute parsing by deadlovelll · Pull Request #151209 · python/cpython
Conversation
deadlovelll
commented
Jun 9, 2026
•
edited by bedevere-app
Bot
Loading
Uh oh!
There was an error while loading. Please reload this page.
expat_start_handler() in Modules/_elementtree.c calls a PyDict_SetItem() and two Py_DECREF() while building attrib dictionary. Replaced it with reference-stealing _PyDict_SetItem_Take2(), dropped the pair of Py_DECREF().
Benchmark:
import pyperf
import xml.etree.ElementTree as ET
def make_doc(n_elements, n_attrs):
attrs = " ".join(f'a{i}="v{i}"' for i in range(n_attrs))
body = "".join(f"<item {attrs}/>" for _ in range(n_elements))
return f"<root>{body}</root>"
DOCS = {
"many-attrs-per-elem": make_doc(n_elements=2000, n_attrs=16),
"few-attrs-many-elems": make_doc(n_elements=20000, n_attrs=2),
"wide-attrs": make_doc(n_elements=200, n_attrs=64),
}
if __name__ == "__main__":
runner = pyperf.Runner()
for name, xml in DOCS.items():
runner.bench_func(f"ET.fromstring {name}", ET.fromstring, xml)
Results:
+------------------------------------+---------+-----------------------+
| Benchmark | base | opt |
+====================================+=========+=======================+
| ET.fromstring many-attrs-per-elem | 9.07 ms | 8.51 ms: 1.07x faster |
+------------------------------------+---------+-----------------------+
| ET.fromstring few-attrs-many-elems | 20.3 ms | 19.6 ms: 1.03x faster |
+------------------------------------+---------+-----------------------+
| ET.fromstring wide-attrs | 3.29 ms | 3.08 ms: 1.07x faster |
+------------------------------------+---------+-----------------------+
| Geometric mean | (ref) | 1.06x faster |
+------------------------------------+---------+-----------------------+
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a pretty small optimisation, and the entry does not need internal details, so I suggest removing it.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback! News entry was removed
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
I'm not very fond of using private functions in extension modules. Is there a plan to make those _Py* functions in the public C API? This is one reason why we switched to PyUnicode_Writer while we could have kept the private implementation.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@picnixz:
I'm not very fond of using private functions in extension modules.
I replied on the issue instead: #150942 (comment)
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
This PR is stale because it has been open for 90 days with no activity.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Assignees
No one assigned
Milestone
No milestone
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|