shithub: mc

ref: 0b58f43116b059473c24999e488f6471123dcdf8
dir: /bench/copious-allocs.myr/

View raw version
use std

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

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

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

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

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

const shuffle = {a
	var t
	var rng
	var i, j

	rng = std.mksrng(123)
	for i = 0; i < a.len - 1; i++
		j = std.rngrand(rng, i, a.len)
		t = a[j]
		a[j] = a[i]
		a[i] = t
	;;
}