Created
April 24, 2025 10:40
-
-
Save RunsFor/622459f5e1b8e81f202a9a0d78e0bc4d to your computer and use it in GitHub Desktop.
This gist shows benefits of table buckets preallocation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
jit.on() | |
local stopwatch = require('common.stopwatch') | |
local table_new = table.new | |
local attempts = 20 | |
local keys = 100 | |
local results = {} | |
local cached_keys = require('fun').range(keys):map(tostring):totable() | |
local function test1(iterations) | |
local t = table_new(0, keys) | |
for _ = 1, iterations do | |
for j = 1, keys do | |
t[cached_keys[j]] = j | |
end | |
end | |
return t | |
end | |
local function test2(iterations) | |
local t = nil | |
for _ = 1, iterations do | |
t = {} | |
for j = 1, keys do | |
t[cached_keys[j]] = j | |
end | |
end | |
return t | |
end | |
local function test3(iterations) | |
local t = nil | |
for _ = 1, iterations do | |
t = table_new(0, 0) | |
for j = 1, keys do | |
t[cached_keys[j]] = j | |
end | |
end | |
return t | |
end | |
local function bench(iterations) | |
results = table_new(attempts, 0) | |
for _ = 1, attempts do | |
table.insert(results, stopwatch.measure(function() test1(iterations) end, {format = 'ms'})) | |
end | |
table.sort(results) | |
local median = results[math.floor(attempts / 2)] | |
local operation_time = (median / iterations) * 1e6 | |
print(string.format("test1 - t = table.new(0, %d) median: %.2fms, %d insertions - %.2fns/op ", keys, median, keys, operation_time)) | |
results = table_new(attempts, 0) | |
for _ = 1, attempts do | |
table.insert(results, stopwatch.measure(function() test2(iterations) end, {format = 'ms'})) | |
end | |
table.sort(results) | |
median = results[math.floor(attempts / 2)] | |
operation_time = (median / iterations) * 1e6 | |
print(string.format("test2 - t = {} median: %.2fms, %d insertions - %.2fns/op", median, keys, operation_time)) | |
results = table_new(attempts, 0) | |
for _ = 1, attempts do | |
table.insert(results, stopwatch.measure(function() test3(iterations) end, {format = 'ms'})) | |
end | |
table.sort(results) | |
median = results[math.floor(attempts / 2)] | |
operation_time = (median / iterations) * 1e6 | |
print(string.format("test3 - t = table.new(0, 0) median: %.2fms, %d insertions - %.2fns/op", median, keys, operation_time)) | |
end | |
bench(1e5) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$ tarantool bench.lua | |
test1 - t = table.new(0, 100) median: 12.05ms, 100 insertions - 120.50ns/op | |
test2 - t = {} median: 122.67ms, 100 insertions - 1226.68ns/op | |
test3 - t = table.new(0, 0) median: 124.47ms, 100 insertions - 1244.68ns/op |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment