Skip to content

Commit fced7ab

Browse files
committed
gh-153569: streamline tokenizer buffer hot paths
1 parent 8c4469b commit fced7ab

3 files changed

Lines changed: 7 additions & 5 deletions

File tree

Parser/lexer/lexer.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ _PyToken_TextView(const struct tok_state *tok, const struct token *token,
1414
Py_ssize_t *length)
1515
{
1616
assert(length != NULL);
17-
if (!_PyTok_SpanIsValid(token->span)) {
17+
if (token->span.start < 0) {
18+
assert(token->span.start == -1 && token->span.end == -1);
1819
*length = 0;
1920
return "";
2021
}
22+
assert(_PyTok_SpanIsValid(token->span));
2123
assert(tok->buf != NULL);
2224
assert(tok->inp >= tok->buf);
2325
assert(token->span.start >= tok->buf_offset);

Parser/tokenizer/reader.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,11 @@ reserve_input_buffer(struct tok_state *tok, Py_ssize_t needed)
7878
assert(tok->inp - tok->buf <= reader->input_buffer_cap);
7979
_PyLexer_BufferPointers pointers;
8080
_PyLexer_SaveBufferPointers(tok, tok->buf, &pointers);
81-
char *buffer = tok->buf;
8281
if (reserve_buffer(
83-
&buffer, &reader->input_buffer_cap, needed) < 0) {
82+
&tok->buf, &reader->input_buffer_cap, needed) < 0) {
8483
return -1;
8584
}
86-
_PyLexer_RestoreBufferPointers(tok, buffer, &pointers);
85+
_PyLexer_RestoreBufferPointers(tok, tok->buf, &pointers);
8786
return 0;
8887
}
8988

Python/Python-tokenize.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ tokenizeriter_next(PyObject *op)
268268
}
269269
const char *token_start = NULL;
270270
PyObject *str;
271-
if (!_PyTok_SpanIsValid(token.span)) {
271+
if (token.span.start < 0) {
272+
assert(token.span.start == -1 && token.span.end == -1);
272273
str = Py_GetConstant(Py_CONSTANT_EMPTY_STR);
273274
}
274275
else {

0 commit comments

Comments
 (0)