shithub: mc

ref: 25d91f98a97df36e1da381b273219ceedf256a4d
dir: /bench/copious-allocs.myr/

View raw version
use std

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

const main = {
	var a : blob#[10000]

	for var j = 0; j < 100; j++
		/* 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])
		;;

		/* 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])
		;;

		/* 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
	;;
}