Skip to content

Commit

Permalink
Merge pull request #56 from dinosaure/remove
Browse files Browse the repository at this point in the history
Implement Rowex.remove and apply ocamlformat.0.26.2
  • Loading branch information
dinosaure authored Aug 5, 2024
2 parents 70be87b + f15c2cb commit b3aa677
Show file tree
Hide file tree
Showing 43 changed files with 5,840 additions and 4,534 deletions.
3 changes: 1 addition & 2 deletions .ocamlformat
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
version=0.25.1
disable=true
version=0.26.2
18 changes: 9 additions & 9 deletions atomic/atomic.mli
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,34 @@
backward-compatible with older versions of OCaml without having to
import additional compatibility layers. *)

type (* ! *) 'a t
(** An atomic (mutable) reference to a value of type ['a]. *)
type (* ! *)'a t

(** Create an atomic reference. *)
val make : 'a -> 'a t
(** Create an atomic reference. *)

(** Get the current value of the atomic reference. *)
val get : 'a t -> 'a
(** Get the current value of the atomic reference. *)

(** Set a new value for the atomic reference. *)
val set : 'a t -> 'a -> unit
(** Set a new value for the atomic reference. *)

(** Set a new value for the atomic reference, and return the current value. *)
val exchange : 'a t -> 'a -> 'a
(** Set a new value for the atomic reference, and return the current value. *)

val compare_and_set : 'a t -> 'a -> 'a -> bool
(** [compare_and_set r seen v] sets the new value of [r] to [v] only
if its current value is physically equal to [seen] -- the
comparison and the set occur atomically. Returns [true] if the
comparison succeeded (so the set happened) and [false]
otherwise. *)
val compare_and_set : 'a t -> 'a -> 'a -> bool

val fetch_and_add : int t -> int -> int
(** [fetch_and_add r n] atomically increments the value of [r] by [n],
and returns the current value (before the increment). *)
val fetch_and_add : int t -> int -> int

(** [incr r] atomically increments the value of [r] by [1]. *)
val incr : int t -> unit
(** [incr r] atomically increments the value of [r] by [1]. *)

(** [decr r] atomically decrements the value of [r] by [1]. *)
val decr : int t -> unit
(** [decr r] atomically decrements the value of [r] by [1]. *)
11 changes: 5 additions & 6 deletions atomic/atomic_pre412.ml
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ external ( == ) : 'a -> 'a -> bool = "%eq"
external ( + ) : int -> int -> int = "%addint"
external ignore : 'a -> unit = "%ignore"

type 'a t = {mutable v: 'a}
type 'a t = { mutable v : 'a }

let make v = {v}
let make v = { v }
let get r = r.v
let set r v = r.v <- v

Expand All @@ -36,14 +36,13 @@ let[@inline never] compare_and_set r seen v =
if cur == seen then (
r.v <- v;
(* END ATOMIC *)
true
) else
false
true)
else false

let[@inline never] fetch_and_add r n =
(* BEGIN ATOMIC *)
let cur = r.v in
r.v <- (cur + n);
r.v <- cur + n;
(* END ATOMIC *)
cur

Expand Down
114 changes: 68 additions & 46 deletions bench/bench_find.ml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,19 @@ external random_seed : unit -> int array = "caml_sys_random_seed"

let seed = "1NYFZiWgaFHf7EhBe6QhvABW5lKcCYs5vcnFi3YsqOU="
let seed = Base64.decode_exn seed

let seed =
let res = Array.make (String.length seed / 2) 0 in
for i = 0 to (String.length seed / 2) - 1
do res.(i) <- (Char.code seed.[i * 2] lsl 8) lor (Char.code seed.[i * 2 + 1]) done ;
for i = 0 to (String.length seed / 2) - 1 do
res.(i) <- (Char.code seed.[i * 2] lsl 8) lor Char.code seed.[(i * 2) + 1]
done;
res

let () =
let random_seed = seed in
Random.full_init random_seed

let ( <.> ) f g = fun x -> f (g x)

let ( <.> ) f g x = f (g x)
let random_max = 16.

let random_normal n =
Expand All @@ -27,55 +28,74 @@ let random_normal n =
for i = 0 to (m / 2) - 1 do
let x = ref 0. and y = ref 0. and rsq = ref 0. in
while
x := (Random.float random_max /. random_max *. 2.0) -. 1. ;
y := (Random.float random_max /. random_max *. 2.0) -. 1. ;
rsq := (!x *. !x) +. (!y *. !y) ;
x := (Random.float random_max /. random_max *. 2.0) -. 1.;
y := (Random.float random_max /. random_max *. 2.0) -. 1.;
rsq := (!x *. !x) +. (!y *. !y);
!rsq >= 1. || !rsq = 0.
do () done ;
do
()
done;
let f = sqrt (-2.0 *. log !rsq /. !rsq) in
values.(i * 2) <- !x *. f ;
values.(i * 2) <- !x *. f;
values.((i * 2) + 1) <- !y *. f
done ;
done;
Array.map (abs <.> Float.to_int <.> ( *. ) random_max) values

let random_string ln =
let rs = Bytes.create ln in
let ic = open_in "/dev/urandom" in
really_input ic rs 0 ln ;
close_in ic ;
for i = 0 to ln - 1 do if Bytes.get rs i = '\000' then Bytes.set rs i '\001' done ;
really_input ic rs 0 ln;
close_in ic;
for i = 0 to ln - 1 do
if Bytes.get rs i = '\000' then Bytes.set rs i '\001'
done;
Bytes.unsafe_to_string rs

let db = Array.map (fun v -> random_string (succ v), v) (random_normal 1000)

let db = Array.map (fun v -> (random_string (succ v), v)) (random_normal 1000)
let art = Art.make ()
let () = Array.iter
(fun (k, v) -> Art.insert art (Art.unsafe_key k) v ;
match Art.find art (Art.unsafe_key k) with
| v' -> assert (v = v')
| exception Not_found -> assert false) db

let () =
Array.iter
(fun (k, v) ->
Art.insert art (Art.unsafe_key k) v;
match Art.find art (Art.unsafe_key k) with
| v' -> assert (v = v')
| exception Not_found -> assert false)
db

let tbl = Hashtbl.create 0x100
let () = Array.iter (fun (k, v) -> Hashtbl.add tbl k v) db

let test0 =
Test.make ~name:"art" @@ Staged.stage @@ fun () ->
Array.iter (fun (k, _) ->
let _ = Art.find art (Art.unsafe_key k) in ()) db
Test.make ~name:"art" @@ Staged.stage
@@ fun () ->
Array.iter
(fun (k, _) ->
let _ = Art.find art (Art.unsafe_key k) in
())
db

let test1 =
Test.make ~name:"hashtbl" @@ Staged.stage @@ fun () ->
Array.iter (fun (k ,_) -> let _ = Hashtbl.find tbl k in ()) db
Test.make ~name:"hashtbl" @@ Staged.stage
@@ fun () ->
Array.iter
(fun (k, _) ->
let _ = Hashtbl.find tbl k in
())
db

let test = Test.make_grouped ~name:"find" [ test0; test1; ]
let test = Test.make_grouped ~name:"find" [ test0; test1 ]

let benchmark () =
let ols =
Analyze.ols ~bootstrap:0 ~r_square:true ~predictors:Measure.[| run |] in
Analyze.ols ~bootstrap:0 ~r_square:true ~predictors:Measure.[| run |]
in
let instances =
Instance.[ minor_allocated; major_allocated; monotonic_clock ] in
Instance.[ minor_allocated; major_allocated; monotonic_clock ]
in
let cfg =
Benchmark.cfg ~limit:3000 ~quota:(Time.second 2.0) ~kde:(Some 1000) () in
Benchmark.cfg ~limit:3000 ~quota:(Time.second 2.0) ~kde:(Some 1000) ()
in
let raw_results = Benchmark.all cfg instances test in
let results =
List.map (fun instance -> Analyze.all ols instance raw_results) instances
Expand All @@ -93,21 +113,23 @@ let () =
let results = benchmark () in
match Sys.argv with
| [| _; "cli" |] ->
let open Notty_unix in
List.iter
(fun v -> Bechamel_notty.Unit.add v (Measure.unit v))
Instance.[ minor_allocated; major_allocated; monotonic_clock ] ;
let window =
match winsize Unix.stdout with
| Some (w, h) -> { Bechamel_notty.w; h }
| None -> { Bechamel_notty.w = 80; h = 1 } in
let results, _ = benchmark () in
img (window, results) |> eol |> output_image
let open Notty_unix in
List.iter
(fun v -> Bechamel_notty.Unit.add v (Measure.unit v))
Instance.[ minor_allocated; major_allocated; monotonic_clock ];
let window =
match winsize Unix.stdout with
| Some (w, h) -> { Bechamel_notty.w; h }
| None -> { Bechamel_notty.w = 80; h = 1 }
in
let results, _ = benchmark () in
img (window, results) |> eol |> output_image
| [| _; "json" |] | _ ->
let results =
let open Bechamel_js in
emit ~dst:(Channel stdout) nothing ~compare:String.compare ~x_label:Measure.run
~y_label:(Measure.label Instance.monotonic_clock)
results in
Rresult.R.failwith_error_msg results

let results =
let open Bechamel_js in
emit ~dst:(Channel stdout) nothing ~compare:String.compare
~x_label:Measure.run
~y_label:(Measure.label Instance.monotonic_clock)
results
in
Rresult.R.failwith_error_msg results
79 changes: 45 additions & 34 deletions bench/bench_insert.ml
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
open Bechamel
open Toolkit

let ( <.> ) f g = fun x -> f (g x)

let ( <.> ) f g x = f (g x)
let random_max = 32767.

let random_normal n =
Expand All @@ -11,46 +10,55 @@ let random_normal n =
for i = 0 to (m / 2) - 1 do
let x = ref 0. and y = ref 0. and rsq = ref 0. in
while
x := (Random.float random_max /. random_max *. 2.0) -. 1. ;
y := (Random.float random_max /. random_max *. 2.0) -. 1. ;
rsq := (!x *. !x) +. (!y *. !y) ;
x := (Random.float random_max /. random_max *. 2.0) -. 1.;
y := (Random.float random_max /. random_max *. 2.0) -. 1.;
rsq := (!x *. !x) +. (!y *. !y);
!rsq >= 1. || !rsq = 0.
do () done ;
do
()
done;
let f = sqrt (-2.0 *. log !rsq /. !rsq) in
values.(i * 2) <- !x *. f ;
values.(i * 2) <- !x *. f;
values.((i * 2) + 1) <- !y *. f
done ;
done;
Array.map (abs <.> Float.to_int <.> ( *. ) random_max) values

let random_string ln =
let rs = Bytes.create ln in
let ic = open_in "/dev/urandom" in
really_input ic rs 0 ln ;
close_in ic ;
for i = 0 to ln - 1 do if Bytes.get rs i = '\000' then Bytes.set rs i '\001' done ;
really_input ic rs 0 ln;
close_in ic;
for i = 0 to ln - 1 do
if Bytes.get rs i = '\000' then Bytes.set rs i '\001'
done;
Bytes.unsafe_to_string rs

let db = Array.map (fun v -> random_string v, v) (random_normal 1000)
let db = Array.map (fun v -> (random_string v, v)) (random_normal 1000)

let test0 =
Test.make ~name:"art" @@ Staged.stage @@ fun () ->
Test.make ~name:"art" @@ Staged.stage
@@ fun () ->
let tree = Art.make () in
Array.iter (fun (k, v) -> Art.insert tree (Art.unsafe_key k) v) db

let test1 =
Test.make ~name:"hashtbl" @@ Staged.stage @@ fun () ->
Test.make ~name:"hashtbl" @@ Staged.stage
@@ fun () ->
let tbl = Hashtbl.create 0x100 in
Array.iter (fun (k, v) -> Hashtbl.add tbl k v) db

let test = Test.make_grouped ~name:"insert" [ test0; test1; ]
let test = Test.make_grouped ~name:"insert" [ test0; test1 ]

let benchmark () =
let ols =
Analyze.ols ~bootstrap:0 ~r_square:true ~predictors:Measure.[| run |] in
Analyze.ols ~bootstrap:0 ~r_square:true ~predictors:Measure.[| run |]
in
let instances =
Instance.[ minor_allocated; major_allocated; monotonic_clock ] in
Instance.[ minor_allocated; major_allocated; monotonic_clock ]
in
let cfg =
Benchmark.cfg ~limit:3000 ~quota:(Time.second 2.0) ~kde:(Some 1000) () in
Benchmark.cfg ~limit:3000 ~quota:(Time.second 2.0) ~kde:(Some 1000) ()
in
let raw_results = Benchmark.all cfg instances test in
let results =
List.map (fun instance -> Analyze.all ols instance raw_results) instances
Expand All @@ -68,20 +76,23 @@ let () =
let results = benchmark () in
match Sys.argv with
| [| _; "cli" |] ->
let open Notty_unix in
List.iter
(fun v -> Bechamel_notty.Unit.add v (Measure.unit v))
Instance.[ minor_allocated; major_allocated; monotonic_clock ] ;
let window =
match winsize Unix.stdout with
| Some (w, h) -> { Bechamel_notty.w; h }
| None -> { Bechamel_notty.w = 80; h = 1 } in
let results, _ = benchmark () in
img (window, results) |> eol |> output_image
let open Notty_unix in
List.iter
(fun v -> Bechamel_notty.Unit.add v (Measure.unit v))
Instance.[ minor_allocated; major_allocated; monotonic_clock ];
let window =
match winsize Unix.stdout with
| Some (w, h) -> { Bechamel_notty.w; h }
| None -> { Bechamel_notty.w = 80; h = 1 }
in
let results, _ = benchmark () in
img (window, results) |> eol |> output_image
| [| _; "json" |] | _ ->
let results =
let open Bechamel_js in
emit ~dst:(Channel stdout) nothing ~compare:String.compare ~x_label:Measure.run
~y_label:(Measure.label Instance.monotonic_clock)
results in
Rresult.R.failwith_error_msg results
let results =
let open Bechamel_js in
emit ~dst:(Channel stdout) nothing ~compare:String.compare
~x_label:Measure.run
~y_label:(Measure.label Instance.monotonic_clock)
results
in
Rresult.R.failwith_error_msg results
Loading

0 comments on commit b3aa677

Please sign in to comment.