[Tarantool-patches] [PATCH 9/9] util: introduce binary heap data structure

Vladislav Shpilevoy v.shpilevoy at tarantool.org
Sat Mar 6 01:03:52 MSK 2021


Applied this diff and force-pushed, in order to eliminate the
metatable and __index access.

Besides, each heap's metatable is different from the others
because the methods are closures, so there wouldn't be any
memory saving from using a metatable. It couldn't have been
shared between the heaps anyway.

====================
diff --git a/vshard/heap.lua b/vshard/heap.lua
index 78c600a..b125921 100644
--- a/vshard/heap.lua
+++ b/vshard/heap.lua
@@ -203,22 +203,21 @@ local function heap_new(is_left_above)
         return count
     end
 
-    return setmetatable({
+    return {
         -- Expose the data. For testing.
         data = data,
-    }, {
-        __index = {
-            push = heap_push,
-            update_top = heap_update_top,
-            remove_top = heap_remove_top,
-            pop = heap_pop,
-            update = heap_update,
-            remove = heap_remove,
-            remove_try = heap_remove_try,
-            top = heap_top,
-            count = heap_count,
-        }
-    })
+        -- Methods are exported as members instead of __index so as to save on
+        -- not taking a metatable and going through __index on each method call.
+        push = heap_push,
+        update_top = heap_update_top,
+        remove_top = heap_remove_top,
+        pop = heap_pop,
+        update = heap_update,
+        remove = heap_remove,
+        remove_try = heap_remove_try,
+        top = heap_top,
+        count = heap_count,
+    }
 end
 
 return {


More information about the Tarantool-patches mailing list