From bf25085c67fb279b06ef1f33645ad828c7799e1d 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:45:38 +0200
Subject: [PATCH] Mapping: Don't rehash in vain.

The non-empty mapping hashsize never shrinks below AVG_LINK_LENGTH,
so save some cpu cycles by not attempting to shrink it further.

Attempts to improve performance of encode_value() for objects,
programs and functions.
---
 src/mapping.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/src/mapping.c b/src/mapping.c
index a10284312b..bf9881baf3 100644
--- a/src/mapping.c
+++ b/src/mapping.c
@@ -1184,9 +1184,9 @@ PMOD_EXPORT void map_delete_no_free(struct mapping *m,
   if(m->data ==md)
     m->debug_size--;
 #endif
-  
-  if(md->size < (md->hashsize + 1) * MIN_LINK_LENGTH)
-  {
+
+  if((md->size < md->hashsize * MIN_LINK_LENGTH) &&
+     (md->hashsize > AVG_LINK_LENGTH)) {
     debug_malloc_touch(m);
     rehash(m, MAP_SLOTS(m->data->size));
   }
@@ -1260,8 +1260,8 @@ 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)
-    {
+    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));
     }
-- 
GitLab