8000 py, gc: Only zero out the extra bytes at the end of the heap chunk. · lurch/micropython@32bef31 · GitHub
[go: up one dir, main page]

Skip to content

Commit 32bef31

Browse files
committed
py, gc: Only zero out the extra bytes at the end of the heap chunk.
This is a small optimisation to zero out only the additional bytes that the caller did not ask for.
1 parent dbc81df commit 32bef31

File tree

2 files changed

+6
-10
lines changed

2 files changed

+6
-10
lines changed

py/gc.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -366,17 +366,17 @@ void *gc_alloc(machine_uint_t n_bytes, bool has_finaliser) {
366366
// get pointer to first block
367367
void *ret_ptr = (void*)(gc_pool_start + start_block * WORDS_PER_BLOCK);
368368

369-
// zero out the newly allocated blocks
369+
// zero out the additional bytes of the newly allocated blocks
370370
// This is needed because the blocks may have previously held pointers
371371
// to the heap and will not be set to something else if the caller
372372
// doesn't actually use the entire block. As such they will continue
373373
// to point to the heap and may prevent other blocks from being reclaimed.
374-
memset(ret_ptr, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK);
374+
memset(ret_ptr + n_bytes, 0, (end_block - start_block + 1) * BYTES_PER_BLOCK - n_bytes);
375375

376376
#if MICROPY_ENABLE_FINALISER
377377
if (has_finaliser) {
378-
// clear type pointer in case it is never set (now done above in memset)
379-
//((mp_obj_base_t*)ret_ptr)->type = MP_OBJ_NULL;
378+
// clear type pointer in case it is never set
379+
((mp_obj_base_t*)ret_ptr)->type = MP_OBJ_NULL;
380380
// set mp_obj flag only if it has a finaliser
381381
FTB_SET(start_block);
382382
}
@@ -534,8 +534,8 @@ void *gc_realloc(void *ptr_in, machine_uint_t n_bytes) {
534534
ATB_FREE_TO_TAIL(bl);
535535
}
536536

537-
// zero out the newly allocated blocks (see comment above in gc_alloc)
538-
memset(ptr_in + n_blocks * BYTES_PER_BLOCK, 0, (new_blocks - n_blocks) * BYTES_PER_BLOCK);
537+
// zero out the additional bytes of the newly allocated blocks (see comment above in gc_alloc)
538+
memset(ptr_in + n_bytes, 0, new_blocks * BYTES_PER_BLOCK - n_bytes);
539539

540540
return ptr_in;
541541
}

py/malloc.c

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,9 @@ void *m_malloc_with_finaliser(int num_bytes) {
8888

8989
void *m_malloc0(int num_bytes) {
9090
void *ptr = m_malloc(num_bytes);
91-
#if MICROPY_ENABLE_GC
92-
// the GC already zeros out all memory
93-
#else
9491
if (ptr != NULL) {
9592
memset(ptr, 0, num_bytes);
9693
}
97-
#endif
9894
return ptr;
9995
}
10096

0 commit comments

Comments
 (0)
0