Skip to content

gh-137627: Make csv.Sniffer.sniff() delimiter detection 1.5x faster #137628

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

maurycy
Copy link
Contributor

@maurycy maurycy commented Aug 11, 2025

The basic idea is not to iterate over all 127 ASCII characters and count their frequency on each line in _guess_delimiter but only over present characters, and just backfill zeros.

Benchmark

There is no csv.Sniffer benchmark in pyperformance, so I constructed a simple benchmark with:

import csv, pathlib, pyperf
def sniff(s): csv.Sniffer()._guess_delimiter(s, None)
r = pyperf.Runner()
sizes = [1024, 2048, 4096]
for file in pathlib.Path("/home/maurycy/CSVsniffer/CSV/").glob("*.csv"):
    for s in sizes:
        with file.open() as f:
            try:
                r.bench_func(f"csv_sniff({file.name}, {s})", sniff, f.read(s))
            except UnicodeDecodeError:
                pass

using all 149 files from CSVSniffer (MIT License), reading only the sample, as recommended in docs.python.org example. That's what real users do, too.

Results

Geometric mean: 1.50x faster

The full results:

Environment

% ./python -c "import sysconfig; print(sysconfig.get_config_var('CONFIG_ARGS'))"
'--with-lto' '--enable-optimizations'

sudo ./python -m pyperf system tune ensured.

Notes

  • The optimization is in csv.Sniffer()._read_delimiter() which runs only if regular expressions in csv.Sniffer()._guess_quote_and_delimiter() failed, so there's no guarantee that csv.Sniffer().sniff() will always be faster
  • My original patch relied on confusing set operations.

@maurycy maurycy changed the title gh-137627: Make csv.Sniffer._guess_delimiter() 2x faster gh-137627: Make csv.Sniffer.sniff() 2x faster Aug 11, 2025
@maurycy maurycy requested a review from AA-Turner August 11, 2025 05:37
Copy link
Member

@picnixz picnixz left a comment

Choose a reason for hiding this comment

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

Are the benchmarks done with a POG+LTO build?

Copy link
Member

@AA-Turner AA-Turner left a comment

Choose a reason for hiding this comment

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

I'm not a CSV expert, but here is a cursory review of the set logic. You should provide a (range of) benchmarks to back up the claim that it is twice as fast, though, ideally using pyperformance.

@maurycy
Copy link
Contributor Author

maurycy commented Aug 11, 2025

@picnixz @AA-Turner I really appreciate your feedback! It's great. I will provide more benchmarks, including with enabled optimizations and ideally with pyperformance, rephrase NEWS, and add in a whatsnew.

@picnixz
Copy link
Member

picnixz commented Aug 12, 2025

Benchmarks without optimizations are not relevant so just run those with.

maurycy added a commit to maurycy/cpython-benchmarks that referenced this pull request Aug 13, 2025
@maurycy maurycy changed the title gh-137627: Make csv.Sniffer.sniff() 2x faster gh-137627: Make csv.Sniffer.sniff() delimiter detection 1.5x faster Aug 13, 2025
@maurycy
Copy link
Contributor Author

maurycy commented Aug 13, 2025

@picnixz @AA-Turner @ZeroIntensity

Thank you for all the comments:

  • There is nothing about csv.Sniffer() in pyperformance, so I was a bit stuck here.
  • I created a much more rigorous benchmark than before, see the results: gh-137627: Make csv.Sniffer.sniff() delimiter detection 1.5x faster #137628 The benchmark is now on par with how people use the class.
  • I've changed the approach, ditching confusing set operations. The speed up now comes mostly from not iterating over ascii over and over, and more efficient zero backfilling.
  • I updated the docs.

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

Successfully merging this pull request may close these issues.

4 participants