From fcbc847459c7b0a57aed81d20eeda373202e0c2c Mon Sep 17 00:00:00 2001 From: Vesa Karvonen Date: Tue, 21 Jan 2025 15:42:23 +0000 Subject: [PATCH] Ignore results from ops on `ref` benchmarks This prevents the `get` op from being optimized away and makes some other operations also more realistic. --- bench.Dockerfile | 2 +- bench/bench_atomic.ml | 16 +++++++--------- bench/bench_ref.ml | 15 +++++++-------- bench/bench_ref_mutex.ml | 15 +++++++-------- 4 files changed, 22 insertions(+), 26 deletions(-) diff --git a/bench.Dockerfile b/bench.Dockerfile index 1096dd9..944b71c 100644 --- a/bench.Dockerfile +++ b/bench.Dockerfile @@ -1,6 +1,6 @@ FROM ocaml/opam:debian-ocaml-5.3 +WORKDIR /bench-dir RUN sudo ln -sf /usr/bin/opam-2.1 /usr/bin/opam -WORKDIR bench-dir RUN sudo chown opam . COPY *.opam ./ RUN opam remote add origin https://github.com/ocaml/opam-repository.git && \ diff --git a/bench/bench_atomic.ml b/bench/bench_atomic.ml index 41af367..9389379 100644 --- a/bench/bench_atomic.ml +++ b/bench/bench_atomic.ml @@ -10,8 +10,7 @@ module Atomic = struct modify ~backoff:(Backoff.once backoff) x f end -type t = - | Op : string * int * 'a * ('a Atomic.t -> unit) * ('a Atomic.t -> unit) -> t +type t = Op : string * int * 'a * ('a Atomic.t -> _) * ('a Atomic.t -> _) -> t let run_one ~budgetf ?(n_iter = 500 * Util.iter_factor) (Op (name, extra, value, op1, op2)) = @@ -23,8 +22,8 @@ let run_one ~budgetf ?(n_iter = 500 * Util.iter_factor) let work _ () = let rec loop i = if i > 0 then begin - op1 loc; - op2 loc; + op1 loc |> ignore; + op2 loc |> ignore; loop (i - 2) end in @@ -36,18 +35,17 @@ let run_one ~budgetf ?(n_iter = 500 * Util.iter_factor) let run_suite ~budgetf = [ - (let get x = Atomic.get x |> ignore in + (let get x = Atomic.get x in Op ("get", 10, 42, get, get)); (let incr x = Atomic.incr x in Op ("incr", 1, 0, incr, incr)); (let push x = Atomic.modify x (fun xs -> 101 :: xs) and pop x = Atomic.modify x (function [] -> [] | _ :: xs -> xs) in Op ("push & pop", 2, [], push, pop)); - (let cas01 x = Atomic.compare_and_set x 0 1 |> ignore - and cas10 x = Atomic.compare_and_set x 1 0 |> ignore in + (let cas01 x = Atomic.compare_and_set x 0 1 + and cas10 x = Atomic.compare_and_set x 1 0 in Op ("cas int", 1, 0, cas01, cas10)); - (let xchg1 x = Atomic.exchange x 1 |> ignore - and xchg0 x = Atomic.exchange x 0 |> ignore in + (let xchg1 x = Atomic.exchange x 1 and xchg0 x = Atomic.exchange x 0 in Op ("xchg int", 1, 0, xchg1, xchg0)); (let swap x = Atomic.modify x (fun (x, y) -> (y, x)) in Op ("swap", 2, (4, 2), swap, swap)); diff --git a/bench/bench_ref.ml b/bench/bench_ref.ml index 93ed928..ab9e323 100644 --- a/bench/bench_ref.ml +++ b/bench/bench_ref.ml @@ -26,7 +26,7 @@ module Ref = struct modify ~backoff:(Backoff.once backoff) x f end -type t = Op : string * int * 'a * ('a Ref.t -> unit) * ('a Ref.t -> unit) -> t +type t = Op : string * int * 'a * ('a Ref.t -> _) * ('a Ref.t -> _) -> t let run_one ~budgetf ?(n_iter = 500 * Util.iter_factor) (Op (name, extra, value, op1, op2)) = @@ -38,8 +38,8 @@ let run_one ~budgetf ?(n_iter = 500 * Util.iter_factor) let work _ () = let rec loop i = if i > 0 then begin - op1 loc; - op2 loc; + op1 loc |> ignore; + op2 loc |> ignore; loop (i - 2) end in @@ -51,18 +51,17 @@ let run_one ~budgetf ?(n_iter = 500 * Util.iter_factor) let run_suite ~budgetf = [ - (let get x = Ref.get x |> ignore in + (let get x = Ref.get x in Op ("get", 10, 42, get, get)); (let incr x = Ref.incr x in Op ("incr", 1, 0, incr, incr)); (let push x = Ref.modify x (fun xs -> 101 :: xs) and pop x = Ref.modify x (function [] -> [] | _ :: xs -> xs) in Op ("push & pop", 2, [], push, pop)); - (let cas01 x = Ref.compare_and_set x 0 1 |> ignore - and cas10 x = Ref.compare_and_set x 1 0 |> ignore in + (let cas01 x = Ref.compare_and_set x 0 1 + and cas10 x = Ref.compare_and_set x 1 0 in Op ("cas int", 1, 0, cas01, cas10)); - (let xchg1 x = Ref.exchange x 1 |> ignore - and xchg0 x = Ref.exchange x 0 |> ignore in + (let xchg1 x = Ref.exchange x 1 and xchg0 x = Ref.exchange x 0 in Op ("xchg int", 1, 0, xchg1, xchg0)); (let swap x = Ref.modify x (fun (x, y) -> (y, x)) in Op ("swap", 2, (4, 2), swap, swap)); diff --git a/bench/bench_ref_mutex.ml b/bench/bench_ref_mutex.ml index 73d815d..34f1898 100644 --- a/bench/bench_ref_mutex.ml +++ b/bench/bench_ref_mutex.ml @@ -18,7 +18,7 @@ module Ref = struct before end -type t = Op : string * 'a * ('a Ref.t -> unit) * ('a Ref.t -> unit) -> t +type t = Op : string * 'a * ('a Ref.t -> _) * ('a Ref.t -> _) -> t (** For some reason allocating the mutex inside [run_one] tends to cause performance hiccups, i.e. some operations appear to be 10x slower than @@ -34,10 +34,10 @@ let run_one ~budgetf ?(n_iter = 250 * Util.iter_factor) let rec loop i = if i > 0 then begin Mutex.lock mutex; - op1 loc; + op1 loc |> ignore; Mutex.unlock mutex; Mutex.lock mutex; - op2 loc; + op2 loc |> ignore; Mutex.unlock mutex; loop (i - 2) end @@ -50,18 +50,17 @@ let run_one ~budgetf ?(n_iter = 250 * Util.iter_factor) let run_suite ~budgetf = [ - (let get x = !x |> ignore in + (let get x = !x in Op ("get", 42, get, get)); (let incr x = x := !x + 1 in Op ("incr", 0, incr, incr)); (let push x = x := 101 :: !x and pop x = match !x with [] -> () | _ :: xs -> x := xs in Op ("push & pop", [], push, pop)); - (let cas01 x = Ref.compare_and_set x 0 1 |> ignore - and cas10 x = Ref.compare_and_set x 1 0 |> ignore in + (let cas01 x = Ref.compare_and_set x 0 1 + and cas10 x = Ref.compare_and_set x 1 0 in Op ("cas int", 0, cas01, cas10)); - (let xchg1 x = Ref.exchange x 1 |> ignore - and xchg0 x = Ref.exchange x 0 |> ignore in + (let xchg1 x = Ref.exchange x 1 and xchg0 x = Ref.exchange x 0 in Op ("xchg int", 0, xchg1, xchg0)); (let swap x = let l, r = !x in