Skip to content

csv.reader calls the state machine for every character needlessly #138213

Open

Description

@maurycy

Bug report

Bug description:

The state machine:

parse_process_char(ReaderObj *self, _csvstate *module_state, Py_UCS4 c)

is called for every character processed by csv.reader:

cpython/Modules/_csv.c

Lines 969 to 974 in bbcb75c

while (linelen--) {
c = PyUnicode_READ(kind, data, pos);
if (parse_process_char(self, module_state, c) < 0) {
Py_DECREF(lineobj);
goto err;
}

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

Activity

  1. added
    type-featureA feature request or enhancement
    performancePerformance or resource usage
    stdlibStandard Library Python modules in the Lib/ directory
    and removed
    type-bugAn unexpected behavior, bug, or error
    on Aug 28, 2025
  2. added 2 commits that reference this issue on Sep 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirperformancePerformance or resource usagestdlibStandard Library Python modules in the Lib/ directorytype-featureA feature request or enhancement

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions