Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Roblox
GitHub Repository: Roblox/luau
Path: blob/master/bench/gc/test_GC_Boehm_Trees.lua
2725 views
1
--!nonstrict
2
local function prequire(name) local success, result = pcall(require, name); return success and result end
3
local bench = script and require(script.Parent.bench_support) or prequire("bench_support") or require("../bench_support")
4
5
local stretchTreeDepth = 18 -- about 16Mb
6
local longLivedTreeDepth = 16 -- about 4Mb
7
local arraySize = 500000 --about 4Mb
8
local minTreeDepth = 4
9
local maxTreeDepth = 16
10
11
-- Nodes used by a tree of a given size
12
function treeSize(i)
13
return bit32.lshift(1, i + 1) - 1
14
end
15
16
function getNumIters(i)
17
return 2 * treeSize(stretchTreeDepth) / treeSize(i)
18
end
19
20
-- Build tree top down, assigning to older objects.
21
function populate(depth, thisNode)
22
if depth <= 0 then
23
return
24
end
25
26
depth = depth - 1
27
thisNode.left = {}
28
thisNode.right = {}
29
populate(depth, thisNode.left)
30
populate(depth, thisNode.right)
31
end
32
33
-- Build tree bottom-up
34
function makeTree(depth)
35
if depth <= 0 then
36
return {}
37
end
38
39
return { left = makeTree(depth - 1), right = makeTree(depth - 1) }
40
end
41
42
function timeConstruction(depth)
43
local numIters = getNumIters(depth)
44
local tempTree = {}
45
46
for i = 1, numIters do
47
tempTree = {}
48
populate(depth, tempTree)
49
tempTree = nil
50
end
51
52
for i = 1, numIters do
53
tempTree = makeTree(depth)
54
tempTree = nil
55
end
56
end
57
58
function test()
59
-- Stretch the memory space quickly
60
local _tempTree = makeTree(stretchTreeDepth)
61
_tempTree = nil
62
63
-- Create a long lived object
64
local longLivedTree = {}
65
populate(longLivedTreeDepth, longLivedTree)
66
67
-- Create long-lived array, filling half of it
68
local array = {}
69
for i = 1, arraySize/2 do
70
array[i] = 1.0 / i
71
end
72
73
for d = minTreeDepth,maxTreeDepth,2 do
74
timeConstruction(d)
75
end
76
end
77
78
bench.runs = 6
79
bench.extraRuns = 2
80
81
bench.runCode(test, "GC: Boehm tree")
82
83