Skip to content

Commit 33ffdca

Browse files
committed
Avoid broadcast in Bounded_q benchmark
This helps to avoid the thundering herd problem and improve thruput.
1 parent 5b91d9a commit 33ffdca

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

bench/bench_bounded_q.ml

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,33 +41,39 @@ end = struct
4141
let is_full_unsafe t = t.capacity <= Queue.length t.queue
4242

4343
let push t x =
44+
let was_full = ref false in
4445
Lock.acquire t.lock;
4546
match
4647
while is_full_unsafe t do
48+
was_full := true;
4749
Lock.Condition.wait t.not_full t.lock
4850
done
4951
with
5052
| () ->
5153
Queue.push x t.queue;
5254
let n = Queue.length t.queue in
5355
Lock.release t.lock;
54-
if n = 1 then Lock.Condition.broadcast t.not_empty
56+
if n = 1 then Lock.Condition.signal t.not_empty;
57+
if !was_full && n < t.capacity then Lock.Condition.signal t.not_full
5558
| exception exn ->
5659
Lock.release t.lock;
5760
raise exn
5861

5962
let pop t =
63+
let was_empty = ref false in
6064
Lock.acquire t.lock;
6165
match
6266
while Queue.length t.queue = 0 do
67+
was_empty := true;
6368
Lock.Condition.wait t.not_empty t.lock
6469
done
6570
with
6671
| () ->
6772
let n = Queue.length t.queue in
6873
let elem = Queue.pop t.queue in
6974
Lock.release t.lock;
70-
if n = t.capacity then Lock.Condition.broadcast t.not_full;
75+
if n = t.capacity then Lock.Condition.signal t.not_full;
76+
if !was_empty && 1 < n then Lock.Condition.signal t.not_empty;
7177
elem
7278
| exception exn ->
7379
Lock.release t.lock;
@@ -78,7 +84,7 @@ end = struct
7884
let n = Queue.length t.queue in
7985
let elem_opt = Queue.take_opt t.queue in
8086
Lock.release t.lock;
81-
if n = t.capacity then Lock.Condition.broadcast t.not_full;
87+
if n = t.capacity then Lock.Condition.signal t.not_full;
8288
elem_opt
8389
end
8490

0 commit comments

Comments
 (0)