Skip to content

Commit d26496a

Browse files
committed
gh-153569: remove redundant reader counters
The reader stores two prefetched lines with an index and count that only describe whether each slot is occupied. Its control flags also use full integers. Use the chunk slots as their own occupancy state and keep reader control flags byte-sized. This removes bookkeeping without changing read order or buffer ownership.
1 parent 41e14d8 commit d26496a

3 files changed

Lines changed: 21 additions & 23 deletions

File tree

Parser/tokenizer/decoder.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ _PyTok_NormalizeNewlines(const char *data, Py_ssize_t len, int preserve_crlf,
103103
}
104104
result[write] = '\0';
105105
*out_len = write;
106-
*implicit_newline = implicit;
106+
if (implicit_newline != NULL) {
107+
*implicit_newline = implicit;
108+
}
107109
return result;
108110
}
109111

Parser/tokenizer/reader.c

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,6 @@ initialize_file(struct tok_state *tok)
259259
if (result != _PYTOK_READ_LINE) {
260260
return -1;
261261
}
262-
reader->prefetched_count = 1;
263262
Py_ssize_t bom_len;
264263
_PyTok_EncodingResult detection = _PyTok_DetectEncoding(
265264
tok, &reader->prefetched_lines[0], NULL, 0, &bom_len);
@@ -277,16 +276,13 @@ initialize_file(struct tok_state *tok)
277276
reader->prefetched_lines[0].data = first;
278277
reader->prefetched_lines[0].ownership = _PYTOK_CHUNK_PYMEM;
279278
result = read_file_line(tok, &reader->prefetched_lines[1]);
280-
if (result == _PYTOK_READ_LINE) {
281-
reader->prefetched_count = 2;
282-
}
283-
else if (result == _PYTOK_READ_EOF) {
279+
if (result == _PYTOK_READ_EOF) {
284280
reader->file_eof = 1;
285281
}
286-
else {
282+
else if (result != _PYTOK_READ_LINE) {
287283
return -1;
288284
}
289-
_PyTok_Chunk *second = reader->prefetched_count == 2
285+
_PyTok_Chunk *second = reader->prefetched_lines[1].data != NULL
290286
? &reader->prefetched_lines[1] : NULL;
291287
detection = _PyTok_DetectEncoding(
292288
tok, &reader->prefetched_lines[0], second, 1, &bom_len);
@@ -356,10 +352,13 @@ next_file(struct tok_state *tok, _PyTok_Chunk *chunk)
356352
return _PYTOK_READ_LINE;
357353
}
358354
_PyTok_Chunk input = {0};
359-
if (reader->prefetched_index < reader->prefetched_count) {
360-
input = reader->prefetched_lines[reader->prefetched_index];
361-
reader->prefetched_lines[reader->prefetched_index++] =
362-
(_PyTok_Chunk){0};
355+
if (reader->prefetched_lines[0].data != NULL) {
356+
input = reader->prefetched_lines[0];
357+
reader->prefetched_lines[0] = (_PyTok_Chunk){0};
358+
}
359+
else if (reader->prefetched_lines[1].data != NULL) {
360+
input = reader->prefetched_lines[1];
361+
reader->prefetched_lines[1] = (_PyTok_Chunk){0};
363362
}
364363
else if (!reader->file_eof) {
365364
_PyTok_ReadResult result = read_file_line(tok, &input);
@@ -557,7 +556,7 @@ next_interactive(struct tok_state *tok, _PyTok_Chunk *chunk)
557556
}
558557
chunk->data = _PyTok_NormalizeNewlines(
559558
decoded.data, decoded.len, 0, 0,
560-
&chunk->len, &chunk->implicit_newline);
559+
&chunk->len, NULL);
561560
_PyTok_ChunkClear(&decoded);
562561
if (chunk->data == NULL) {
563562
PyErr_NoMemory();

Parser/tokenizer/reader_internal.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,13 @@ typedef enum {
3232

3333
typedef struct {
3434
char *data;
35-
Py_ssize_t len;
36-
int implicit_newline;
3735
PyObject *owner;
36+
Py_ssize_t len;
3837
_PyTok_ChunkOwnership ownership;
38+
unsigned char implicit_newline;
3939
} _PyTok_Chunk;
4040

4141
typedef struct _PyTok_Reader {
42-
_PyTok_ReaderKind kind;
4342
PyObject *readline;
4443
PyObject *decoder;
4544
const char *prompt;
@@ -50,18 +49,16 @@ typedef struct _PyTok_Reader {
5049
char *file_buffer;
5150
Py_ssize_t file_buffer_cap;
5251
_PyTok_Chunk prefetched_lines[2];
53-
int prefetched_index;
54-
int prefetched_count;
5552

5653
char *decoded;
5754
Py_ssize_t decoded_pos;
5855
Py_ssize_t decoded_len;
5956
Py_ssize_t decoded_cap;
60-
int decoded_tail_is_implicit;
61-
62-
int file_initialized;
63-
int file_eof;
64-
int decoder_finalized;
57+
_PyTok_ReaderKind kind;
58+
unsigned char decoded_tail_is_implicit;
59+
unsigned char file_initialized;
60+
unsigned char file_eof;
61+
unsigned char decoder_finalized;
6562
unsigned char stop_interactive;
6663
} _PyTok_Reader;
6764

0 commit comments

Comments
 (0)