shithub: mc

ref: bf035b686f58f139ee0b04746d3215e7bb8413e9
dir: /bench/copious-allocs.myr/

View raw version
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#

	for var i = 0; i < 1000; i++
		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
	;;
}