Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/bench/gc/test_GC_Tree_Pruning_Lazy.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
local left = tree.left;
9
local right = tree.right;
10
11
if not left then
12
left = { id = count }
13
count = count + 1
14
end
15
16
if not right then
17
right = { id = count }
18
count = count + 1
19
end
20
21
if levels ~= 0 then
22
fill_tree(left, levels - 1)
23
fill_tree(right, levels - 1)
24
end
25
26
tree.left = left;
27
tree.right = right;
28
end
29
30
local function prune_tree(tree, level)
31
if tree.left then
32
if math.random() > 0.9 - level * 0.05 then
33
tree.left = nil
34
else
35
prune_tree(tree.left, level + 1)
36
end
37
end
38
39
if tree.right then
40
if math.random() > 0.9 - level * 0.05 then
41
tree.right = nil
42
else
43
prune_tree(tree.right, level + 1)
44
end
45
end
46
end
47
48
local tree = { id = 0 }
49
50
for i = 1,100 do
51
fill_tree(tree, 10)
52
53
prune_tree(tree, 0)
54
end
55
end
56
57
bench.runCode(test, "GC: tree pruning (lazy fill)")
58
59