summaryrefslogtreecommitdiff
path: root/bench/copious-allocs.myr
blob: ca450a76da91b20cd7077726662c890be3158a62 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
use std
use testr

type blob = struct
	x : int[10]
;;

const main = {
	testr.bench([
		[.name="alloc-one", .fn=alloc_one],
		[.name="allocmany-fwdfwd", .fn=alloc_fwdfwd],
		[.name="allocmany-fwdrev", .fn=alloc_fwdrev],
		[.name="allocmany-fwdrand", .fn=alloc_fwdrand],
	][:])
}

const alloc_one = {ctx
	var a : int#
	a = std.alloc()
	std.free(a)
}

const alloc_fwdfwd = {ctx
	var a : blob#[10000]

	/* alloc forwards, dealloc forwards */
	for var i = 0; i < a.len; i++
		a[i] = std.alloc()
	;;
	for var i = 0; i < a.len; i++
		std.free(a[i])
	;;

}

const alloc_fwdrev = {ctx
	var a : blob#[10000]

	/* alloc forwards, dealloc backwards */
	for var i = 0; i < a.len; i++
		a[i] = std.alloc()
	;;
	for var i = a.len; i > 0; i--
		std.free(a[i - 1])
	;;
}

const alloc_fwdrand = {ctx
	var a : blob#[10000]

	/* alloc forwards, dealloc randomly */
	for var i = 0; i < a.len; i++
		a[i] = std.alloc()
	;;
	shuffle(a[:])
	for var i = a.len; i > 0; i--
		std.free(a[i - 1])
	;;
}

const shuffle = {a
	var t
	var rng
	var j

	/* we want determinism for benchmarking */
	rng = std.mksrng(123)
	for var i = 0; i < a.len - 1; i++
		j = std.rngrand(rng, i, a.len)
		t = a[j]
		a[j] = a[i]
		a[i] = t
	;;
}