Skip to content
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

gh-119118: Fix performance regression in tokenize module #119615

Merged
merged 7 commits into from
May 28, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Get rid of strcmp
Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
  • Loading branch information
lysnikolaou and pablogsal committed May 28, 2024
commit 858eff15bd8e698c0e6c53ee3c16948bd9a1c770
12 changes: 5 additions & 7 deletions Python/Python-tokenize.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@ typedef struct
int done;

/* Needed to cache line for performance */
const char *last_line_start;
Py_ssize_t last_line_size;
PyObject *last_line;
Py_ssize_t last_lineno;
Py_ssize_t byte_col_offset_diff;
} tokenizeriterobject;

Expand Down Expand Up @@ -75,10 +74,9 @@ tokenizeriter_new_impl(PyTypeObject *type, PyObject *readline,
}
self->done = 0;

self->last_line_start = NULL;
self->last_line_size = 0;
self->last_line = NULL;
self->byte_col_offset_diff = 0;
self->last_lineno = 0;

return (PyObject *)self;
}
Expand Down Expand Up @@ -223,14 +221,12 @@ tokenizeriter_next(tokenizeriterobject *it)
size -= 1;
}

if (size != it->last_line_size || strcmp(line_start, it->last_line_start) != 0) {
if (it->tok->lineno != it->last_lineno) {
// Line has changed since last token, so we fetch the new line and cache it
// in the iter object.
Py_XDECREF(it->last_line);
line = PyUnicode_DecodeUTF8(line_start, size, "replace");
it->last_line = line;
it->last_line_start = line_start;
it->last_line_size = size;
it->byte_col_offset_diff = 0;
} else {
// Line hasn't changed so we reuse the cached one.
Expand All @@ -244,6 +240,8 @@ tokenizeriter_next(tokenizeriterobject *it)

Py_ssize_t lineno = ISSTRINGLIT(type) ? it->tok->first_lineno : it->tok->lineno;
Py_ssize_t end_lineno = it->tok->lineno;
it->last_lineno = lineno;

Py_ssize_t col_offset = -1;
Py_ssize_t end_col_offset = -1;
Py_ssize_t byte_offset = -1;
Expand Down
Loading