Skip to content

Commit

Permalink
Use raise_without_backtrace in Map, Set
Browse files Browse the repository at this point in the history
Map and Set use some exceptions for control flow that were being raised
with plain `raise`. This meant that stack traces were being created for
these exception that couldn't escape their outer function causing an
unfortunate performance loss. Raising these exceptions with
`raise_without_backtrace` avoids the performance problem.

Signed-off-by: Geoff Reedy <[email protected]>
  • Loading branch information
greedy committed Jun 8, 2022
1 parent 8993e35 commit fc3ef01
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/map.ml
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ module Tree0 = struct
match t with
| Empty ->
(match f None with
| None -> raise Change_no_op (* equivalent to returning: Empty *)
| None -> Exn.raise_without_backtrace Change_no_op (* equivalent to returning: Empty *)
| Some data -> Leaf (key, data), length + 1)
| Leaf (v, d) ->
let c = compare_key key v in
Expand Down
12 changes: 6 additions & 6 deletions src/set.ml
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,13 @@ module Tree0 = struct
| Leaf v ->
let c = compare_elt x v in
if c = 0
then raise Same
then Exn.raise_without_backtrace Same
else if c < 0
then create (Leaf x) v Empty
else create Empty v (Leaf x)
| Node (l, v, r, _, _) ->
let c = compare_elt x v in
if c = 0 then raise Same else if c < 0 then bal (aux l) v r else bal l v (aux r)
if c = 0 then Exn.raise_without_backtrace Same else if c < 0 then bal (aux l) v r else bal l v (aux r)
in
try aux t with
| Same -> t
Expand Down Expand Up @@ -419,8 +419,8 @@ module Tree0 = struct
let remove t x ~compare_elt =
let rec aux t =
match t with
| Empty -> raise Same
| Leaf v -> if compare_elt x v = 0 then Empty else raise Same
| Empty -> Exn.raise_without_backtrace Same
| Leaf v -> if compare_elt x v = 0 then Empty else Exn.raise_without_backtrace Same
| Node (l, v, r, _, _) ->
let c = compare_elt x v in
if c = 0 then merge l r else if c < 0 then bal (aux l) v r else bal l v (aux r)
Expand All @@ -432,8 +432,8 @@ module Tree0 = struct
let remove_index t i ~compare_elt:_ =
let rec aux t i =
match t with
| Empty -> raise Same
| Leaf _ -> if i = 0 then Empty else raise Same
| Empty -> Exn.raise_without_backtrace Same
| Leaf _ -> if i = 0 then Empty else Exn.raise_without_backtrace Same
| Node (l, v, r, _, _) ->
let l_size = length l in
let c = Poly.compare i l_size in
Expand Down

0 comments on commit fc3ef01

Please sign in to comment.