more cache friendliness

This commit is contained in:
Simon Cruanes 2015-11-28 12:43:50 +01:00
parent 0ec5545564
commit 99919ae1d3

View file

@ -5,17 +5,20 @@
module Arr = struct module Arr = struct
type 'a t = { type 'a t = {
caches: 'a array array array; caches: 'a array array;
(* array of buckets, where each bucket is an array of arrays *) (* 2-dim array of cached arrays. The 2-dim array is flattened into
one dimension *)
max_buck_size: int; max_buck_size: int;
sizes: int array; (* number of cached arrays in each bucket *) (* number of cached arrays per length *)
sizes: int array;
(* number of cached arrays in each bucket *)
} }
let create ?(buck_size=16) n = let create ?(buck_size=16) n =
if n<1 then invalid_arg "AllocCache.Arr.create"; if n<1 then invalid_arg "AllocCache.Arr.create";
{ max_buck_size=buck_size; { max_buck_size=buck_size;
sizes=Array.make n 0; sizes=Array.make n 0;
caches=Array.init n (fun _ -> Array.make buck_size [||]); caches=Array.make (n * buck_size) [||];
} }
let make c i x = let make c i x =
@ -25,7 +28,7 @@ module Arr = struct
if bs = 0 then Array.make i x if bs = 0 then Array.make i x
else ( else (
(* remove last array *) (* remove last array *)
let ret = c.caches.(i).(bs-1) in let ret = c.caches.(i * c.max_buck_size + bs-1) in
c.sizes.(i) <- bs - 1; c.sizes.(i) <- bs - 1;
ret ret
) )
@ -37,7 +40,7 @@ module Arr = struct
let bs = c.sizes.(n) in let bs = c.sizes.(n) in
if bs < c.max_buck_size then ( if bs < c.max_buck_size then (
(* store [a] *) (* store [a] *)
c.caches.(n).(bs) <- a; c.caches.(n * c.max_buck_size + bs) <- a;
c.sizes.(n) <- bs + 1 c.sizes.(n) <- bs + 1
) )
) )