From 4ab48edbf37a72162dd3e9143a9cd5d13cd82c56 Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Tue, 6 Feb 2024 13:15:38 -0500
Subject: [PATCH] rts/Hash: Don't iterate over chunks if we don't need to free
 data

When freeing a `HashTable` there is no reason to walk over the hash list
before freeing it if the user has not given us a `dataFreeFun`.

Noticed while looking at #24410.
---
 rts/Hash.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/rts/Hash.c b/rts/Hash.c
index 7139c9e01b0c..1a9f1c029d57 100644
--- a/rts/Hash.c
+++ b/rts/Hash.c
@@ -440,14 +440,15 @@ freeHashTable(HashTable *table, void (*freeDataFun)(void *) )
 
     /* Free table segments */
     while (segment >= 0) {
-        while (index >= 0) {
-            HashList *next;
-            for (HashList *hl = table->dir[segment][index]; hl != NULL; hl = next) {
-                next = hl->next;
-                if (freeDataFun != NULL)
+        if (freeDataFun) {
+            while (index >= 0) {
+                HashList *next;
+                for (HashList *hl = table->dir[segment][index]; hl != NULL; hl = next) {
+                    next = hl->next;
                     (*freeDataFun)((void *) hl->data);
+                }
+                index--;
             }
-            index--;
         }
         stgFree(table->dir[segment]);
         segment--;
-- 
GitLab