From 946d69561b50f788d6e43a172c565814005c99f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dag-Erling=20Sm=C3=B8rgrav?= Date: Thu, 25 Jul 2024 20:59:57 +0200 Subject: [PATCH] Avoid needless hash table churn. 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. --- lib/xmalloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/xmalloc.c b/lib/xmalloc.c index c445f67..a606d35 100644 --- a/lib/xmalloc.c +++ b/lib/xmalloc.c @@ -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);