From 84ffdbc0252545ebfc5594e8cc0aed85d6187def Mon Sep 17 00:00:00 2001 From: Steven Schveighoffer Date: Thu, 17 Oct 2024 07:19:27 -0400 Subject: [PATCH] Zero slabs that are freed that contain pointers, so when they are allocated next time, they are already zero. (#395) This isn't as performant as I would like it to be. I can't think of a good way to run both finalization and zeroing in the same pass. I also think there is an opportunity to memset batches of slabs, which might happen if you for instance free an entire graph of nodes at once. I might be able to get something like that working. --- sdlib/d/gc/tcache.d | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/sdlib/d/gc/tcache.d b/sdlib/d/gc/tcache.d index b7ca9b426..3220ed8fd 100644 --- a/sdlib/d/gc/tcache.d +++ b/sdlib/d/gc/tcache.d @@ -14,6 +14,8 @@ import sdc.intrinsics; enum DefaultEventWait = 65536; +enum ShouldZeroFreeSlabs = true; + alias RNode = Node!ThreadCache; alias ThreadRing = Ring!ThreadCache; @@ -412,7 +414,13 @@ private: // We trigger the de-allocation event first as it might // recycle the bin we are interested in, which increase // our chances that free works. - triggerDeallocationEvent(binInfos[sc].slotSize); + auto slotSize = binInfos[sc].slotSize; + triggerDeallocationEvent(slotSize); + + // If the allocation contains pointers, zero it before freeing it + if (ShouldZeroFreeSlabs && pd.containsPointers) { + memset(ptr, 0, slotSize); + } auto index = getBinIndex(sc, pd.containsPointers); auto bin = &bins[index];