Skip to content

gh-159675: Use lazy urllib imports in saxutils - #159676

Merged
StanFromIreland merged 2 commits into
python:mainfrom
jelly:saxutils-lazy-load-urllib
Oct 19, 2026
Merged

StanFromIreland merged 2 commits into
python:mainfrom
jelly:saxutils-lazy-load-urllib

Conversation

@jelly

@jelly jelly commented Oct 18, 2026 •

Copy link
Copy Markdown
Contributor

Both urllib imports are only used for prepare_input_source, normally not used when parsing or writing XML files.
A real life example of the benefit of lazy loading is the "firewalld" daemon on Fedora/RHEL Linux systems where lazy loading urllib shaves off 3MB memory.


Alternatively I can just move the imports to prepare_input_source if desired and drop the test case.

@python-cla-bot

python-cla-bot Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app bedevere-app Bot added the type-feature A feature request or enhancement label Oct 18, 2026
@bedevere-app

This comment was marked as resolved.

@jelly

jelly commented Oct 18, 2026

Copy link
Copy Markdown
Contributor Author

"Benchmarks":

Before:

[jelle@natrium][~/projects/cpython]%hyperfine -w 5 "./python -c 'import xml.sax.saxutils'"
Benchmark 1: ./python -c 'import xml.sax.saxutils' (85 runs)
                mean     ±       σ          min     …     max
  Wall Time     34.3 ms  ±     1.4 ms      32.6 ms  …    42.0 ms
  Memory        20.3 MiB ±     0.2 MiB     19.7 MiB …    20.6 MiB

After

[jelle@natrium][~/projects/cpython]%hyperfine -w 5 "./python -c 'import xml.sax.saxutils'"
Benchmark 1: ./python -c 'import xml.sax.saxutils' (271 runs)
                mean     ±       σ          min     …     max
  Wall Time     10.6 ms  ±     0.5 ms       9.1 ms  …    11.6 ms
  Memory        10.2 MiB ±     0.1 MiB      9.8 MiB …    10.4 MiB

Comment thread Lib/test/test_sax.py
Comment thread Lib/test/test_sax.py Outdated
Both urllib imports are only used for prepare_input_source, normally
not used when parsing or writing XML files.
A real life example of the benefit of lazy loading is the "firewalld"
daemon on Fedora/RHEL Linux systems where lazy loading urllib shaves off
3MB memory.
@jelly
jelly force-pushed the saxutils-lazy-load-urllib branch from c7f16ed to 66c3450 Compare October 18, 2026 18:07
@bedevere-app

This comment was marked as resolved.

@bedevere-app

This comment was marked as resolved.

@StanFromIreland StanFromIreland left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, this seems reasonable, and they are quite heavy modules.

I pushed a little commit tidying up a few more things, I hope you don't mind. I'll merge this whenever GitHub Actions come back to life.

@StanFromIreland
StanFromIreland merged commit 2dd5315 into python:main Oct 19, 2026
57 of 59 checks passed
@StanFromIreland

Copy link
Copy Markdown
Member

Merged, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip news type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants