From 27ae4d128fd870a82b92770e4695c5b57dc8431d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20Grubbstr=C3=B6m=20=28Grubba=29?=
 <grubba@grubba.org>
Date: Fri, 21 Oct 2016 15:58:57 +0200
Subject: [PATCH] Mapping: Added flag MAPPING_FLAG_NO_SHRINK.

This is a flag that disables shrinking of the mapping_data hashtable
when the number of elements goes below the MIN_LINK_LENGTH threshold.
It is intended to be used by code that has mappings with a high
rotation of content (ie lots of mixed insertions and deletions).
---
 src/mapping.c | 20 ++++++++++++--------
 src/mapping.h |  1 +
 2 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/src/mapping.c b/src/mapping.c
index bf9881baf3..0628dfd839 100644
--- a/src/mapping.c
+++ b/src/mapping.c
@@ -1185,10 +1185,12 @@ PMOD_EXPORT void map_delete_no_free(struct mapping *m,
     m->debug_size--;
 #endif
 
-  if((md->size < md->hashsize * MIN_LINK_LENGTH) &&
-     (md->hashsize > AVG_LINK_LENGTH)) {
-    debug_malloc_touch(m);
-    rehash(m, MAP_SLOTS(m->data->size));
+  if (!(md->flags & MAPPING_FLAG_NO_SHRINK)) {
+    if((md->size < md->hashsize * MIN_LINK_LENGTH) &&
+       (md->hashsize > AVG_LINK_LENGTH)) {
+      debug_malloc_touch(m);
+      rehash(m, MAP_SLOTS(m->data->size));
+    }
   }
 
 #ifdef PIKE_DEBUG
@@ -1260,10 +1262,12 @@ PMOD_EXPORT void check_mapping_for_destruct(struct mapping *m)
     md->val_types = val_types;
     md->ind_types = ind_types;
 
-    if((MAP_SLOTS(md->size) < md->hashsize * MIN_LINK_LENGTH) &&
-       (md->hashsize > AVG_LINK_LENGTH)) {
-      debug_malloc_touch(m);
-      rehash(m, MAP_SLOTS(md->size));
+    if (!(md->flags & MAPPING_FLAG_NO_SHRINK)) {
+      if((MAP_SLOTS(md->size) < md->hashsize * MIN_LINK_LENGTH) &&
+	 (md->hashsize > AVG_LINK_LENGTH)) {
+	debug_malloc_touch(m);
+	rehash(m, MAP_SLOTS(md->size));
+      }
     }
 
 #ifdef PIKE_DEBUG
diff --git a/src/mapping.h b/src/mapping.h
index 0444d3d525..763ca2c01c 100644
--- a/src/mapping.h
+++ b/src/mapping.h
@@ -15,6 +15,7 @@
 #define MAPPING_WEAK_VALUES	4
 #define MAPPING_WEAK		6
 #define MAPPING_FLAG_WEAK	6 /* Compat. */
+#define MAPPING_FLAG_NO_SHRINK	0x1000
 
 struct keypair
 {
-- 
GitLab