From 135db46ff06870e763294bda0304eda705cc4f35 Mon Sep 17 00:00:00 2001
From: Per Hedbor <ph@opera.com>
Date: Tue, 31 Mar 2015 08:47:45 +0200
Subject: [PATCH] Optimized has_index for mappings and multiset

It is now way faster for mappings (in testing about 2x) and somewhat
faster for multisets. Since our developers have a tendency to write
if(has_index(map,key)) instead of if(mapping[key]) this can be a
noticeable gain.
---
 src/builtin_functions.c | 21 ++++++++++-----------
 1 file changed, 10 insertions(+), 11 deletions(-)

diff --git a/src/builtin_functions.c b/src/builtin_functions.c
index 3cdc997c0c..b6f58e72b9 100644
--- a/src/builtin_functions.c
+++ b/src/builtin_functions.c
@@ -1366,18 +1366,17 @@ PMOD_EXPORT void f_has_index(INT32 args)
       push_int(t);
       break;
       
-    case T_MULTISET:
     case T_MAPPING:
-      f_index(2);
-      f_zero_type(1);
-      
-#ifdef PIKE_DEBUG
-      if(TYPEOF(Pike_sp[-1]) != T_INT)
-	PIKE_ERROR("has_index",
-		   "Function `zero_type' gave incorrect result.\n", Pike_sp, args);
-#endif
-      Pike_sp[-1].u.integer = !Pike_sp[-1].u.integer;
-      break;
+        t=!!low_mapping_lookup( Pike_sp[-2].u.mapping, Pike_sp-1 );
+        pop_n_elems(2);
+        push_int(t);
+        break;
+
+    case T_MULTISET:
+        t = multiset_member( Pike_sp[-2].u.multiset, Pike_sp-1 );
+        pop_n_elems(2);
+        push_int(t);
+        break;
       
     case T_OBJECT:
     case T_PROGRAM:
-- 
GitLab