mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-06 11:15:31 -05:00
perf(vector): less aggressive growth for internal resizing
This commit is contained in:
parent
2d30b2ae14
commit
f540a6d7e5
1 changed files with 8 additions and 4 deletions
|
|
@ -79,9 +79,13 @@ let init n f = {
|
||||||
}
|
}
|
||||||
|
|
||||||
(* is the underlying array empty? *)
|
(* is the underlying array empty? *)
|
||||||
let array_is_empty_ v =
|
let[@inline] array_is_empty_ v =
|
||||||
Array.length v.vec = 0
|
Array.length v.vec = 0
|
||||||
|
|
||||||
|
(* next capacity, if current one is [n] *)
|
||||||
|
let[@inline] next_grow_ n =
|
||||||
|
min Sys.max_array_length (n + n lsl 1 + 5)
|
||||||
|
|
||||||
(* resize the underlying array using x to temporarily fill the array *)
|
(* resize the underlying array using x to temporarily fill the array *)
|
||||||
let resize_ v newcapacity x =
|
let resize_ v newcapacity x =
|
||||||
assert (newcapacity >= v.size);
|
assert (newcapacity >= v.size);
|
||||||
|
|
@ -128,7 +132,7 @@ let grow_with_ v ~filler:x =
|
||||||
fill_with_junk_ v.vec 0 len;
|
fill_with_junk_ v.vec 0 len;
|
||||||
) else (
|
) else (
|
||||||
let n = Array.length v.vec in
|
let n = Array.length v.vec in
|
||||||
let size = min (2 * n + 3) Sys.max_array_length in
|
let size = next_grow_ n in
|
||||||
if size = n then invalid_arg "vec: can't grow any further";
|
if size = n then invalid_arg "vec: can't grow any further";
|
||||||
resize_ v size v.vec.(0)
|
resize_ v size v.vec.(0)
|
||||||
)
|
)
|
||||||
|
|
@ -143,8 +147,8 @@ let ensure_assuming_not_empty_ v ~size =
|
||||||
) else if size < Array.length v.vec then (
|
) else if size < Array.length v.vec then (
|
||||||
() (* nothing to do *)
|
() (* nothing to do *)
|
||||||
) else (
|
) else (
|
||||||
let n = ref (max 8 (Array.length v.vec)) in
|
let n = ref (Array.length v.vec) in
|
||||||
while !n < size do n := min Sys.max_array_length (2* !n) done;
|
while !n < size do n := next_grow_ !n done;
|
||||||
resize_ v !n v.vec.(0)
|
resize_ v !n v.vec.(0)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue