diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-05-28 20:47:45 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-05-28 20:47:45 (GMT) |
commit | 0d0be6b3efeace4743329f81c08f9720cc221207 (patch) | |
tree | afeec3b81a48161eef6c675658a9e7aac55aaf36 /Parser | |
parent | c0e99617985d64e6134964f758ae0a1a20f9f433 (diff) | |
download | cpython-0d0be6b3efeace4743329f81c08f9720cc221207.zip cpython-0d0be6b3efeace4743329f81c08f9720cc221207.tar.gz cpython-0d0be6b3efeace4743329f81c08f9720cc221207.tar.bz2 |
[3.13] gh-119118: Fix performance regression in tokenize module (GH-119615) (#119682)
- Cache line object to avoid creating a Unicode object
for all of the tokens in the same line.
- Speed up byte offset to column offset conversion by using the
smallest buffer possible to measure the difference.
(cherry picked from commit d87b0151062e36e67f9e42e1595fba5bf23a485c)
Co-authored-by: Lysandros Nikolaou <lisandrosnik@gmail.com>
Co-authored-by: Pablo Galindo <pablogsal@gmail.com>
Diffstat (limited to 'Parser')
-rw-r--r-- | Parser/pegen.c | 25 | ||||
-rw-r--r-- | Parser/pegen.h | 1 |
2 files changed, 26 insertions, 0 deletions
diff --git a/Parser/pegen.c b/Parser/pegen.c index 3d3e645..2955eab 100644 --- a/Parser/pegen.c +++ b/Parser/pegen.c @@ -19,6 +19,31 @@ _PyPegen_interactive_exit(Parser *p) } Py_ssize_t +_PyPegen_byte_offset_to_character_offset_line(PyObject *line, Py_ssize_t col_offset, Py_ssize_t end_col_offset) +{ + const char *data = PyUnicode_AsUTF8(line); + + Py_ssize_t len = 0; + while (col_offset < end_col_offset) { + Py_UCS4 ch = data[col_offset]; + if (ch < 0x80) { + col_offset += 1; + } else if ((ch & 0xe0) == 0xc0) { + col_offset += 2; + } else if ((ch & 0xf0) == 0xe0) { + col_offset += 3; + } else if ((ch & 0xf8) == 0xf0) { + col_offset += 4; + } else { + PyErr_SetString(PyExc_ValueError, "Invalid UTF-8 sequence"); + return -1; + } + len++; + } + return len; +} + +Py_ssize_t _PyPegen_byte_offset_to_character_offset_raw(const char* str, Py_ssize_t col_offset) { Py_ssize_t len = strlen(str); diff --git a/Parser/pegen.h b/Parser/pegen.h index 57b45a5..32c64e7 100644 --- a/Parser/pegen.h +++ b/Parser/pegen.h @@ -148,6 +148,7 @@ int _PyPegen_fill_token(Parser *p); expr_ty _PyPegen_name_token(Parser *p); expr_ty _PyPegen_number_token(Parser *p); void *_PyPegen_string_token(Parser *p); +Py_ssize_t _PyPegen_byte_offset_to_character_offset_line(PyObject *line, Py_ssize_t col_offset, Py_ssize_t end_col_offset); Py_ssize_t _PyPegen_byte_offset_to_character_offset(PyObject *line, Py_ssize_t col_offset); Py_ssize_t _PyPegen_byte_offset_to_character_offset_raw(const char*, Py_ssize_t col_offset); |