diff --git a/src/mapping.c b/src/mapping.c
index bf9881baf36ae96b51c75a81cf34109bab691232..0628dfd8399b3fbbf75380b872047140ede83221 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 0444d3d5251acffe6886bde366c6f4a7d296e32c..763ca2c01c8fe51d70c7fc6b718be665241b4c88 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
 {