Skip to content

Lazy load urllib modules in saxutils #159675

Open

Description

@jelly

Feature or enhancement

import xml.sax.saxutils

Firewalld, a popular Linux firewall daemon uses xml.sax to read/write XML files, like most other consumers it does not call prepare_input_source() which is the only function which relies on the urllib imports.

Lazily loading the modules shaves off 3M memory usage (48M before -> 45M after), additionally it makes importing saxutils 3x faster but that is not super important.

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

Has this already been discussed elsewhere?

This is a minor feature, which does not need previous discussion elsewhere

Links to previous discussion of this feature:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytopic-lazy-importstype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions