Skip to content

Commit deca528

Browse files
committed
Ignore results from ops on ref benchmarks
This prevents the `get` op from being optimized away and makes some other operations also more realistic.
1 parent c4f3be5 commit deca528

File tree

4 files changed

+22
-26
lines changed

4 files changed

+22
-26
lines changed

bench.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
FROM ocaml/opam:debian-ocaml-5.3
2+
WORKDIR /bench-dir
23
RUN sudo ln -sf /usr/bin/opam-2.1 /usr/bin/opam
3-
WORKDIR bench-dir
44
RUN sudo chown opam .
55
COPY *.opam ./
66
RUN opam remote add origin https://github.com/ocaml/opam-repository.git && \

bench/bench_atomic.ml

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ module Atomic = struct
1010
modify ~backoff:(Backoff.once backoff) x f
1111
end
1212

13-
type t =
14-
| Op : string * int * 'a * ('a Atomic.t -> unit) * ('a Atomic.t -> unit) -> t
13+
type t = Op : string * int * 'a * ('a Atomic.t -> _) * ('a Atomic.t -> _) -> t
1514

1615
let run_one ~budgetf ?(n_iter = 500 * Util.iter_factor)
1716
(Op (name, extra, value, op1, op2)) =
@@ -23,8 +22,8 @@ let run_one ~budgetf ?(n_iter = 500 * Util.iter_factor)
2322
let work _ () =
2423
let rec loop i =
2524
if i > 0 then begin
26-
op1 loc;
27-
op2 loc;
25+
op1 loc |> ignore;
26+
op2 loc |> ignore;
2827
loop (i - 2)
2928
end
3029
in
@@ -36,18 +35,17 @@ let run_one ~budgetf ?(n_iter = 500 * Util.iter_factor)
3635

3736
let run_suite ~budgetf =
3837
[
39-
(let get x = Atomic.get x |> ignore in
38+
(let get x = Atomic.get x in
4039
Op ("get", 10, 42, get, get));
4140
(let incr x = Atomic.incr x in
4241
Op ("incr", 1, 0, incr, incr));
4342
(let push x = Atomic.modify x (fun xs -> 101 :: xs)
4443
and pop x = Atomic.modify x (function [] -> [] | _ :: xs -> xs) in
4544
Op ("push & pop", 2, [], push, pop));
46-
(let cas01 x = Atomic.compare_and_set x 0 1 |> ignore
47-
and cas10 x = Atomic.compare_and_set x 1 0 |> ignore in
45+
(let cas01 x = Atomic.compare_and_set x 0 1
46+
and cas10 x = Atomic.compare_and_set x 1 0 in
4847
Op ("cas int", 1, 0, cas01, cas10));
49-
(let xchg1 x = Atomic.exchange x 1 |> ignore
50-
and xchg0 x = Atomic.exchange x 0 |> ignore in
48+
(let xchg1 x = Atomic.exchange x 1 and xchg0 x = Atomic.exchange x 0 in
5149
Op ("xchg int", 1, 0, xchg1, xchg0));
5250
(let swap x = Atomic.modify x (fun (x, y) -> (y, x)) in
5351
Op ("swap", 2, (4, 2), swap, swap));

bench/bench_ref.ml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module Ref = struct
2626
modify ~backoff:(Backoff.once backoff) x f
2727
end
2828

29-
type t = Op : string * int * 'a * ('a Ref.t -> unit) * ('a Ref.t -> unit) -> t
29+
type t = Op : string * int * 'a * ('a Ref.t -> _) * ('a Ref.t -> _) -> t
3030

3131
let run_one ~budgetf ?(n_iter = 500 * Util.iter_factor)
3232
(Op (name, extra, value, op1, op2)) =
@@ -38,8 +38,8 @@ let run_one ~budgetf ?(n_iter = 500 * Util.iter_factor)
3838
let work _ () =
3939
let rec loop i =
4040
if i > 0 then begin
41-
op1 loc;
42-
op2 loc;
41+
op1 loc |> ignore;
42+
op2 loc |> ignore;
4343
loop (i - 2)
4444
end
4545
in
@@ -51,18 +51,17 @@ let run_one ~budgetf ?(n_iter = 500 * Util.iter_factor)
5151

5252
let run_suite ~budgetf =
5353
[
54-
(let get x = Ref.get x |> ignore in
54+
(let get x = Ref.get x in
5555
Op ("get", 10, 42, get, get));
5656
(let incr x = Ref.incr x in
5757
Op ("incr", 1, 0, incr, incr));
5858
(let push x = Ref.modify x (fun xs -> 101 :: xs)
5959
and pop x = Ref.modify x (function [] -> [] | _ :: xs -> xs) in
6060
Op ("push & pop", 2, [], push, pop));
61-
(let cas01 x = Ref.compare_and_set x 0 1 |> ignore
62-
and cas10 x = Ref.compare_and_set x 1 0 |> ignore in
61+
(let cas01 x = Ref.compare_and_set x 0 1
62+
and cas10 x = Ref.compare_and_set x 1 0 in
6363
Op ("cas int", 1, 0, cas01, cas10));
64-
(let xchg1 x = Ref.exchange x 1 |> ignore
65-
and xchg0 x = Ref.exchange x 0 |> ignore in
64+
(let xchg1 x = Ref.exchange x 1 and xchg0 x = Ref.exchange x 0 in
6665
Op ("xchg int", 1, 0, xchg1, xchg0));
6766
(let swap x = Ref.modify x (fun (x, y) -> (y, x)) in
6867
Op ("swap", 2, (4, 2), swap, swap));

bench/bench_ref_mutex.ml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ module Ref = struct
1818
before
1919
end
2020

21-
type t = Op : string * 'a * ('a Ref.t -> unit) * ('a Ref.t -> unit) -> t
21+
type t = Op : string * 'a * ('a Ref.t -> _) * ('a Ref.t -> _) -> t
2222

2323
(** For some reason allocating the mutex inside [run_one] tends to cause
2424
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)
3434
let rec loop i =
3535
if i > 0 then begin
3636
Mutex.lock mutex;
37-
op1 loc;
37+
op1 loc |> ignore;
3838
Mutex.unlock mutex;
3939
Mutex.lock mutex;
40-
op2 loc;
40+
op2 loc |> ignore;
4141
Mutex.unlock mutex;
4242
loop (i - 2)
4343
end
@@ -50,18 +50,17 @@ let run_one ~budgetf ?(n_iter = 250 * Util.iter_factor)
5050

5151
let run_suite ~budgetf =
5252
[
53-
(let get x = !x |> ignore in
53+
(let get x = !x in
5454
Op ("get", 42, get, get));
5555
(let incr x = x := !x + 1 in
5656
Op ("incr", 0, incr, incr));
5757
(let push x = x := 101 :: !x
5858
and pop x = match !x with [] -> () | _ :: xs -> x := xs in
5959
Op ("push & pop", [], push, pop));
60-
(let cas01 x = Ref.compare_and_set x 0 1 |> ignore
61-
and cas10 x = Ref.compare_and_set x 1 0 |> ignore in
60+
(let cas01 x = Ref.compare_and_set x 0 1
61+
and cas10 x = Ref.compare_and_set x 1 0 in
6262
Op ("cas int", 0, cas01, cas10));
63-
(let xchg1 x = Ref.exchange x 1 |> ignore
64-
and xchg0 x = Ref.exchange x 0 |> ignore in
63+
(let xchg1 x = Ref.exchange x 1 and xchg0 x = Ref.exchange x 0 in
6564
Op ("xchg int", 0, xchg1, xchg0));
6665
(let swap x =
6766
let l, r = !x in

0 commit comments

Comments
 (0)