Skip to content

Commit 1874ce4

Browse files
committed
gh-153569: remove redundant reader counters
The reader stores two prefetched lines together 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 the read order or buffer ownership.
1 parent e9b7735 commit 1874ce4

2 files changed

Lines changed: 16 additions & 20 deletions

File tree

Parser/tokenizer/reader.c

Lines changed: 10 additions & 11 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);

Parser/tokenizer/reader_internal.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ typedef struct {
3939
} _PyTok_Chunk;
4040

4141
typedef struct _PyTok_Reader {
42-
_PyTok_ReaderKind kind;
4342
PyObject *readline;
4443
PyObject *decoder;
4544
const char *prompt;
@@ -50,19 +49,17 @@ 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;
65-
int stop_interactive;
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;
62+
unsigned char stop_interactive;
6663
} _PyTok_Reader;
6764

6865
struct tok_state;

0 commit comments

Comments
 (0)