python / python/cpython

`csv.reader` calls the state machine for every character needlessly

Open
#138,213 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

extension-modules performance stdlib type-feature
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

The state machine:

https://github.com/python/cpython/blob/bbcb75c986c47887e6c0757e63d59cd7af544f39/Modules/_csv.c#L726

is called for every character processed by csv.reader:

https://github.com/python/cpython/blob/bbcb75c986c47887e6c0757e63d59cd7af544f39/Modules/_csv.c#L969-L974

Even putting aside sophisticated SIMD or branching optimizations, it could be more efficient.

Most time is likely to be spent in a field (IN_FIELD, IN_QUOTED_FIELD). It's more efficient to find interesting characters (ie: escapes, quotes) and just copy the whole slice in between.

CPython versions tested on:

CPython main branch

Operating systems tested on:

Linux

Linked PRs
  • gh-138214

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in Modules/_csv.c at the state machine around line 726 and the csv.reader processing around lines 969-974. Review linked PR gh-138214 before starting, then verify that any change preserves csv.reader behavior while avoiding needless per-character state-machine calls.

Written by the indexing model from the issue text.

Assessment

Tech stack
c, python
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.