Skip to content

Commit 20917cc

Browse files
committed
Address review comments
1 parent c816f7e commit 20917cc

File tree

1 file changed

+10
-26
lines changed

1 file changed

+10
-26
lines changed

Python/instrumentation.c

Lines changed: 10 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -342,20 +342,12 @@ static inline int
342342
get_line_delta(_PyCoLineInstrumentationData *line_data, int index)
343343
{
344344
uint8_t *ptr = &line_data->data[index*line_data->bytes_per_entry+1];
345-
uint32_t value = *ptr;
346345
assert(line_data->bytes_per_entry >= 2);
347-
if (line_data->bytes_per_entry > 2) {
346+
uint32_t value = *ptr;
347+
for (int idx = 2; idx < line_data->bytes_per_entry; idx++) {
348348
ptr++;
349-
value = (value << 8) | *ptr;
350-
if (line_data->bytes_per_entry > 3) {
351-
ptr++;
352-
value = (value << 8) | *ptr;
353-
if (line_data->bytes_per_entry > 4) {
354-
assert(line_data->bytes_per_entry == 5);
355-
ptr++;
356-
value = (value << 8) | *ptr;
357-
}
358-
}
349+
int shift = (idx-1)*8;
350+
value |= ((uint32_t)(*ptr)) << shift;
359351
}
360352
assert(value < INT_MAX);
361353
/* NO_LINE is stored as zero. */
@@ -369,22 +361,14 @@ set_line_delta(_PyCoLineInstrumentationData *line_data, int index, int line_delt
369361
assert(line_delta >= NO_LINE);
370362
uint32_t adjusted = line_delta - NO_LINE;
371363
uint8_t *ptr = &line_data->data[index*line_data->bytes_per_entry+1];
372-
assert(adjusted < (1ULL << (line_data->bytes_per_entry*8)));
364+
assert(adjusted < (1ULL << ((line_data->bytes_per_entry-1)*8)));
373365
assert(line_data->bytes_per_entry >= 2);
374-
if (line_data->bytes_per_entry > 2) {
375-
if (line_data->bytes_per_entry > 3) {
376-
if (line_data->bytes_per_entry > 4) {
377-
assert(line_data->bytes_per_entry == 5);
378-
*ptr = adjusted >> 24;
379-
ptr++;
380-
}
381-
*ptr = (adjusted >> 16) & 255;
382-
ptr++;
383-
}
384-
*ptr = (adjusted >> 8) & 255;
366+
*ptr = adjusted & 0xff;
367+
for (int idx = 2; idx < line_data->bytes_per_entry; idx++) {
385368
ptr++;
369+
adjusted >>= 8;
370+
*ptr = adjusted & 0xff;
386371
}
387-
*ptr = adjusted & 255;
388372
}
389373

390374
#ifdef INSTRUMENT_DEBUG
@@ -1731,7 +1715,7 @@ update_instrumentation_data(PyCodeObject *code, PyInterpreterState *interp)
17311715
else {
17321716
bytes_per_entry = 5;
17331717
}
1734-
code->_co_monitoring->lines = PyMem_Malloc(1 + code_len *bytes_per_entry);
1718+
code->_co_monitoring->lines = PyMem_Malloc(1 + code_len * bytes_per_entry);
17351719
if (code->_co_monitoring->lines == NULL) {
17361720
PyErr_NoMemory();
17371721
return -1;

0 commit comments

Comments
 (0)