Skip to content

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

Open
jelly wants to merge 1 commit into
python:mainfrom
jelly:saxutils-lazy-load-urllib
Open

jelly wants to merge 1 commit into
python:mainfrom
jelly:saxutils-lazy-load-urllib

Conversation

@jelly

@jelly jelly commented Oct 18, 2026 •

Copy link
Copy Markdown

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

bedevere-app Bot commented Oct 18, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@jelly

jelly commented Oct 18, 2026

Copy link
Copy Markdown
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
getattr(module, "version")
self.assertEqual(cm.filename, __file__)

class TestImportTime(unittest.TestCase):

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.

The class name is a little bit misleading, it doesn't test the import time at all. Maybe we can put the test function in TestModule in this file.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

This makes sense, adjusted and fixed the PEP8 issue

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

bedevere-app Bot commented Oct 18, 2026

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

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

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants