Skip to content

Commit

Permalink
Avoid needless hash table churn.
Browse files Browse the repository at this point in the history
When reallocating, check that the pointer changed before updating the
hash table.  Otherwise, we're just removing then re-adding the same
value.  This frequently happens when the allocation is shrinking, or
only growing by a few bytes.
  • Loading branch information
dag-erling committed Jul 25, 2024
1 parent 6db399f commit 946d695
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion lib/xmalloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ xrealloc_impl(void *ptr, size_t new_size, const char *file, int line,
xmalloc_fail_after--;

new_ptr = realloc(ptr, new_size);
if (new_ptr != NULL)
if (new_ptr != NULL && new_ptr != ptr)
{
hash_table_del(xmalloc_table, ptr);
hash_table_add(xmalloc_table, new_ptr, (int)new_size, file, line, func);
Expand Down

0 comments on commit 946d695

Please sign in to comment.