Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/bench/gc/test_GC_Tree_Pruning_Gen.lua
2725 views
1
local function prequire(name) local success, result = pcall(require, name); return success and result end
2
local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../bench_support")
3
4
function test()
5
local count = 1
6
7
local function fill_tree(tree, levels)
8
if not tree.left then
9
tree.left = { id = count }
10
count = count + 1
11
end
12
13
if not tree.right then
14
tree.right = { id = count }
15
count = count + 1
16
end
17
18
if levels ~= 0 then
19
fill_tree(tree.left, levels - 1)
20
fill_tree(tree.right, levels - 1)
21
end
22
end
23
24
local function prune_tree(tree, level)
25
if tree.left then
26
if math.random() > 0.9 - level * 0.05 then
27
tree.left = nil
28
else
29
prune_tree(tree.left, level + 1)
30
end
31
end
32
33
if tree.right then
34
if math.random() > 0.9 - level * 0.05 then
35
tree.right = nil
36
else
37
prune_tree(tree.right, level + 1)
38
end
39
end
40
end
41
42
-- create a static tree
43
local tree = { id = 0 }
44
fill_tree(tree, 16)
45
46
for i = 1,100 do
47
local small_tree = { id = 0 }
48
49
fill_tree(small_tree, 8)
50
51
prune_tree(small_tree, 0)
52
end
53
end
54
55
bench.runCode(test, "GC: tree pruning (eager fill, gen)")
56
57