mirror of
https://github.com/c-cube/ocaml-containers.git
synced 2025-12-05 19:00:31 -05:00
Merge pull request #399 from c-cube/wip-ccvector-resize
change growth strategy for Vector
This commit is contained in:
commit
bc8b7b168b
9 changed files with 26 additions and 53 deletions
|
|
@ -79,9 +79,13 @@ let init n f = {
|
|||
}
|
||||
|
||||
(* is the underlying array empty? *)
|
||||
let array_is_empty_ v =
|
||||
let[@inline] array_is_empty_ v =
|
||||
Array.length v.vec = 0
|
||||
|
||||
(* next capacity, if current one is [n] *)
|
||||
let[@inline] next_grow_ n =
|
||||
min Sys.max_array_length (n + n lsr 1 + 2)
|
||||
|
||||
(* resize the underlying array using x to temporarily fill the array *)
|
||||
let resize_ v newcapacity x =
|
||||
assert (newcapacity >= v.size);
|
||||
|
|
@ -128,7 +132,7 @@ let grow_with_ v ~filler:x =
|
|||
fill_with_junk_ v.vec 0 len;
|
||||
) else (
|
||||
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";
|
||||
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 (
|
||||
() (* nothing to do *)
|
||||
) else (
|
||||
let n = ref (max 8 (Array.length v.vec)) in
|
||||
while !n < size do n := min Sys.max_array_length (2* !n) done;
|
||||
let n = ref (Array.length v.vec) in
|
||||
while !n < size do n := next_grow_ !n done;
|
||||
resize_ v !n v.vec.(0)
|
||||
)
|
||||
|
||||
|
|
@ -161,7 +165,7 @@ let ensure v size =
|
|||
ensure_assuming_not_empty_ v ~size
|
||||
)
|
||||
|
||||
let clear v =
|
||||
let[@inline] clear v =
|
||||
v.size <- 0
|
||||
|
||||
(*$R
|
||||
|
|
@ -188,9 +192,9 @@ let clear_and_reset v =
|
|||
OUnit.assert_equal None (Weak.get a 0);
|
||||
*)
|
||||
|
||||
let is_empty v = v.size = 0
|
||||
let[@inline] is_empty v = v.size = 0
|
||||
|
||||
let push_unsafe_ v x =
|
||||
let[@inline] push_unsafe_ v x =
|
||||
Array.unsafe_set v.vec v.size x;
|
||||
v.size <- v.size + 1
|
||||
|
||||
|
|
@ -296,11 +300,11 @@ let append a b =
|
|||
OUnit.assert_equal (Iter.to_array Iter.(6 -- 10)) (to_array b);
|
||||
*)
|
||||
|
||||
let get v i =
|
||||
let[@inline] get v i =
|
||||
if i < 0 || i >= v.size then invalid_arg "CCVector.get";
|
||||
Array.unsafe_get v.vec i
|
||||
|
||||
let set v i x =
|
||||
let[@inline] set v i x =
|
||||
if i < 0 || i >= v.size then invalid_arg "CCVector.set";
|
||||
Array.unsafe_set v.vec i x
|
||||
|
||||
|
|
@ -341,7 +345,7 @@ let remove_unordered v i =
|
|||
to_list v1 = (List.sort CCInt.compare (to_list v2)))
|
||||
*)
|
||||
|
||||
let append_iter a i = i (fun x -> push a x)
|
||||
let[@inline] append_iter a i = i (fun x -> push a x)
|
||||
|
||||
let append_seq a seq = Seq.iter (fun x -> push a x) seq
|
||||
|
||||
|
|
@ -477,12 +481,12 @@ let pop v =
|
|||
try Some (pop_exn v)
|
||||
with Empty -> None
|
||||
|
||||
let top v =
|
||||
if v.size = 0 then None else Some v.vec.(v.size-1)
|
||||
let[@inline] top v =
|
||||
if v.size = 0 then None else Some (Array.unsafe_get v.vec (v.size-1))
|
||||
|
||||
let top_exn v =
|
||||
let[@inline] top_exn v =
|
||||
if v.size = 0 then raise Empty;
|
||||
v.vec.(v.size-1)
|
||||
Array.unsafe_get v.vec (v.size-1)
|
||||
|
||||
(*$T
|
||||
1 -- 10 |> top = Some 10
|
||||
|
|
@ -490,7 +494,7 @@ let top_exn v =
|
|||
1 -- 10 |> top_exn = 10
|
||||
*)
|
||||
|
||||
let copy v = {
|
||||
let[@inline] copy v = {
|
||||
size = v.size;
|
||||
vec = Array.sub v.vec 0 v.size;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,6 @@
|
|||
(modules :standard \ mkshims)
|
||||
(flags :standard -warn-error -a+8 -w -32 -safe-string -strict-sequence -nolabels -open
|
||||
CCMonomorphic)
|
||||
(ocamlopt_flags (:include ../flambda.flags))
|
||||
(libraries seq either containers.monomorphic))
|
||||
|
||||
(ocamllex (modules CCSexp_lex))
|
||||
|
|
|
|||
|
|
@ -4,5 +4,4 @@
|
|||
(public_name containers-data)
|
||||
(wrapped false)
|
||||
(flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string -open CCShims_)
|
||||
(ocamlopt_flags :standard (:include ../flambda.flags))
|
||||
(libraries containers))
|
||||
|
|
|
|||
15
src/dune
15
src/dune
|
|
@ -1,15 +1,10 @@
|
|||
(executable
|
||||
(name mkflags)
|
||||
(modules mkflags)
|
||||
(libraries dune.configurator))
|
||||
|
||||
(env
|
||||
(_ (flags :standard -warn-error -3)))
|
||||
|
||||
(rule
|
||||
(targets flambda.flags)
|
||||
(mode fallback)
|
||||
(action (run ./mkflags.exe)))
|
||||
(_
|
||||
(flags :standard -warn-error -3 -color always)
|
||||
(ocamlopt_flags :standard -O3 -unbox-closures -unbox-closures-factor 20
|
||||
-inline 100)
|
||||
))
|
||||
|
||||
(executable
|
||||
(name mdx_runner)
|
||||
|
|
|
|||
|
|
@ -1,19 +0,0 @@
|
|||
|
||||
module C = Configurator.V1
|
||||
|
||||
let () =
|
||||
C.main ~name:"mkflags" (fun c ->
|
||||
let version = C.ocaml_config_var_exn c "version" in
|
||||
let major, minor =
|
||||
Scanf.sscanf version "%u.%u"
|
||||
(fun major minor -> major, minor)
|
||||
in
|
||||
let after_4_3 = (major, minor) >= (4, 3) in
|
||||
let sexp =
|
||||
if after_4_3 then (
|
||||
["-O3"; "-unbox-closures"; "-unbox-closures-factor"; "20"; "-color"; "always"]
|
||||
) else (
|
||||
[]
|
||||
) in
|
||||
C.Flags.write_sexp "flambda.flags" sexp
|
||||
)
|
||||
|
|
@ -14,5 +14,4 @@
|
|||
(public_name containers.monomorphic)
|
||||
(modules CCMonomorphic CCMonomorphicShims_)
|
||||
(wrapped false)
|
||||
(flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string)
|
||||
(ocamlopt_flags :standard (:include ../flambda.flags)))
|
||||
(flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string))
|
||||
|
|
|
|||
|
|
@ -5,6 +5,5 @@
|
|||
(wrapped false)
|
||||
(optional)
|
||||
(flags :standard -warn-error -a+8 -w -32 -safe-string -open CCShims_)
|
||||
(ocamlopt_flags :standard (:include ../flambda.flags))
|
||||
(libraries containers threads))
|
||||
|
||||
|
|
|
|||
|
|
@ -5,5 +5,4 @@
|
|||
(public_name containers.top)
|
||||
(wrapped false)
|
||||
(flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string)
|
||||
(ocamlopt_flags :standard (:include ../flambda.flags))
|
||||
(libraries compiler-libs.common containers containers.unix))
|
||||
|
|
|
|||
|
|
@ -5,6 +5,4 @@
|
|||
(wrapped false)
|
||||
(optional)
|
||||
(flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string)
|
||||
(ocamlopt_flags :standard (:include ../flambda.flags))
|
||||
(libraries unix)
|
||||
)
|
||||
(libraries unix))
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue