Skip to content

Commit 4510379

Browse files
committed
Optimise rendering of background colours
Instead of processing all regions, just draw the ones that are visible.
1 parent 37e2f25 commit 4510379

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

lib/render.ml

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
let rec find_first_aux arr test low high =
2+
if low = high then high
3+
else (
4+
let mid = (low + high) / 2 in
5+
let diff = test mid in
6+
if diff >= 0. then find_first_aux arr test low mid
7+
else find_first_aux arr test (mid + 1) high
8+
)
9+
10+
(* Binary search. Return the index of the first element where [test i] is true.
11+
If [test] isn't true for any element, returns the length of the array.
12+
Assumes that if [test i] then it's true for all later entries. *)
13+
let find_first ?(start = 0) arr test =
14+
find_first_aux arr test start (Array.length arr)
15+
116
module type CANVAS = sig
217
type context
318

@@ -217,12 +232,16 @@ module Make (C : CANVAS) = struct
217232

218233
let iter_gc_spans v fn ring =
219234
let arr = ring.Layout.Ring.events in
220-
(* todo: binary search *)
221-
for i = 0 to Array.length arr - 1 do
235+
let time i = fst arr.(i) in
236+
let start = max 0 (find_first arr (fun i -> time i -. v.View.start_time) - 1) in
237+
let stop_time = View.time_of_x v v.width in
238+
let stop = find_first arr (fun i -> time i -. stop_time) ~start in
239+
for i = start to min stop (Array.length arr - 1) do
222240
let time, e = arr.(i) in
223241
fn (time, e)
224242
done;
225-
fn (v.View.layout.duration, [])
243+
if stop = Array.length arr then
244+
fn (v.View.layout.duration, [])
226245

227246
let render_gc_events v cr (ring : Layout.Ring.t) layer =
228247
let y = y_of_row v ring.y in

0 commit comments

Comments
 (0)