Skip to content

gh-118761: Improve import time of tomllib #128907

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2025

Conversation

hukkin
Copy link
Contributor

@hukkin hukkin commented Jan 16, 2025

This is a port of two commits in Tomli repository (the diff of these commits being hukkin/tomli@42a570d...1da01ef).

This removes runtime overhead of string, typing, and tomllib._types imports from import tomllib.

main branch

$ hyperfine --warmup 8 "./python -c 'import tomllib'" "./python -c 'pass'"
Benchmark 1: ./python -c 'import tomllib'
  Time (mean ± σ):      70.3 ms ±   1.8 ms    [User: 66.0 ms, System: 4.3 ms]
  Range (min … max):    68.6 ms …  76.8 ms    41 runs
 
Benchmark 2: ./python -c 'pass'
  Time (mean ± σ):      26.2 ms ±   0.7 ms    [User: 23.3 ms, System: 3.0 ms]
  Range (min … max):    25.1 ms …  29.7 ms    110 runs
 
Summary
  './python -c 'pass'' ran
    2.69 ± 0.10 times faster than './python -c 'import tomllib''

PR branch

$ hyperfine --warmup 8 "./python -c 'import tomllib'" "./python -c 'pass'"
Benchmark 1: ./python -c 'import tomllib'
  Time (mean ± σ):      48.0 ms ±   0.7 ms    [User: 43.4 ms, System: 4.7 ms]
  Range (min … max):    46.8 ms …  50.2 ms    60 runs
 
Benchmark 2: ./python -c 'pass'
  Time (mean ± σ):      26.1 ms ±   0.4 ms    [User: 23.5 ms, System: 2.7 ms]
  Range (min … max):    25.3 ms …  27.4 ms    110 runs
 
Summary
  './python -c 'pass'' ran
    1.84 ± 0.04 times faster than './python -c 'import tomllib''

@vstinner
Copy link
Member

cc @encukou who added the module in Python 3.11 (PEP 680).

@gpshead gpshead merged commit ea6cc26 into python:main Jan 17, 2025
44 checks passed
@hukkin hukkin deleted the tomllib-importtime branch January 17, 2025 05:47
@encukou
Copy link
Member

encukou commented Jan 17, 2025

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants