Skip to content

gh-118761: Improve import time for csv #128858

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 2 commits into from
Jan 18, 2025

Conversation

picnixz
Copy link
Member

@picnixz picnixz commented Jan 15, 2025

Benchmarks are for a release non-PGO build (PR) against a PGO build (main). This will be the last PR for that issue.

Note

Considering that csv is used in many applications, I think this improvement makes sense (especially for data scientists).

PR

$ ./python -X importtime -c 'import csv'
import time: self [us] | cumulative | imported package
...
import time:       229 |        229 | linecache
import time:       331 |        331 |   types
import time:       175 |        175 |   _csv
import time:       629 |       1134 | csv
$ hyperfine --warmup 8 "./python -c 'import csv'"
Benchmark 1: ./python -c 'import csv'
  Time (mean ± σ):       5.9 ms ±   0.4 ms    [User: 5.0 ms, System: 1.0 ms]
  Range (min … max):     5.5 ms …  11.0 ms    452 runs

Main

$ ./python -X importtime -c 'import csv'
import time: self [us] | cumulative | imported package
...
import time:       227 |        227 | linecache
import time:       261 |        261 |       types
import time:      1419 |       1680 |     enum
import time:        62 |         62 |       _sre
import time:       218 |        218 |         re._constants
import time:       327 |        544 |       re._parser
import time:        79 |         79 |       re._casefix
import time:       287 |        971 |     re._compiler
import time:        90 |         90 |         itertools
import time:        86 |         86 |         keyword
import time:        46 |         46 |           _operator
import time:       187 |        233 |         operator
import time:       119 |        119 |         reprlib
import time:        45 |         45 |         _collections
import time:       733 |       1304 |       collections
import time:        39 |         39 |       _functools
import time:       498 |       1840 |     functools
import time:       114 |        114 |     copyreg
import time:       426 |       5029 |   re
import time:       107 |        107 |   _csv
import time:       575 |       5709 | csv
$ hyperfine --warmup 8 "./python -c 'import csv'"
Benchmark 1: ./python -c 'import csv'
  Time (mean ± σ):       9.2 ms ±   0.9 ms    [User: 8.0 ms, System: 1.2 ms]
  Range (min … max):     8.4 ms …  16.7 ms    297 runs

This reduces the import time of the `csv` module
by up to five times, by importing `re` on demand.

In particular, the `re` module is no more implicitly
exposed as `csv.re`.
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM

@picnixz
Copy link
Member Author

picnixz commented Jan 15, 2025

In a few hours, I will leave until Friday, but I'd like Adam or Hugo to review my English. Feel free to merge this PR and freely update it (otherwise, I'll do it once I'm back).

Copy link
Member

@hugovk hugovk left a comment

Choose a reason for hiding this comment

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

Looks good, thanks!

@picnixz picnixz merged commit d5e9aa6 into python:main Jan 18, 2025
44 checks passed
@picnixz picnixz deleted the perf/import/csv-118761 branch January 18, 2025 10:45
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Jan 21, 2025
This reduces the import time of the `csv` module by up to five times,
by importing `re` on demand.

In particular, the `re` module is no more implicitly exposed as `csv.re`.
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.

4 participants