GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-150942: Speed up ElementTree attribute parsing by deadlovelll · Pull Request #151209 · python/cpython

Conversation

@deadlovelll

@deadlovelll deadlovelll commented Jun 9, 2026 •
edited by bedevere-app Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

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          |
+------------------------------------+---------+-----------------------+
  • Issue: Improve performance by using reference stealing methods #150942

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@deadlovelll
pythongh-150942: Speed up ElementTree attribute parsing
8ac090f
@bedevere-app bedevere-app Bot added the awaiting review label Jun 9, 2026
@bedevere-app bedevere-app Bot mentioned this pull request Jun 9, 2026
Improve performance by using reference stealing methods #150942
Open
eendebakpt
eendebakpt approved these changes Jun 9, 2026
View reviewed changes

@eendebakpt eendebakpt left a comment

Copy link
Copy Markdown
Contributor

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!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@bedevere-app bedevere-app Bot added awaiting core review and removed awaiting review labels Jun 9, 2026
StanFromIreland
StanFromIreland reviewed Jun 9, 2026
View reviewed changes
Comment thread Misc/NEWS.d/next/Library/2026-06-09-23-04-44.gh-issue-150942.Iu583J.rst Outdated

@StanFromIreland StanFromIreland Jun 9, 2026

Copy link
Copy Markdown
Member

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.

All reactions

@deadlovelll deadlovelll Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

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.

All reactions
@deadlovelll
Remove news entry
09b5284
@eendebakpt eendebakpt added the skip news label Jun 9, 2026
@picnixz

picnixz commented Jun 9, 2026

Copy link
Copy Markdown
Member

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@vstinner

vstinner commented Jun 10, 2026

Copy link
Copy Markdown
Member

@picnixz:

I'm not very fond of using private functions in extension modules.

I replied on the issue instead: #150942 (comment)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions

github-actions Bot commented Sep 17, 2026

Copy link
Copy Markdown

This PR is stale because it has been open for 90 days with no activity.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@github-actions github-actions Bot added the stale Stale PR or inactive for long period of time. label Sep 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@StanFromIreland StanFromIreland StanFromIreland left review comments
@eendebakpt eendebakpt eendebakpt approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

awaiting core review skip news stale Stale PR or inactive for long period of time.

Projects

None yet

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.

5 participants

@deadlovelll @picnixz @vstinner @eendebakpt @StanFromIreland
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.