The unpac monorepo manager self-hosting as a monorepo using unpac
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

Optimize Weak.find_aux (#13740)

Change the Stdlib Weak module's find_aux function that is used by find,
find_opt, mem, merge, and remove to avoid a closure allocation by converting
a nested recursive function to a while loop.

authored by

Josh Berdine and committed by
GitHub
8f2daefe 930d7e24

+21 -10
+3
Changes
··· 130 130 table 131 131 (Vincent Laviron, review by Gabriel Scherer and Daniel Bünzli) 132 132 133 + - #13740: Improve performance of Weak.find_aux 134 + (Josh Berdine, review by Gabriel Scherer) 135 + 133 136 ### Other libraries: 134 137 135 138 * #13376: Allow Dynlink.loadfile_private to load bytecode libraries with
+2
stdlib/.depend
··· 816 816 stdlib__Unit.cmi : unit.mli 817 817 stdlib__Weak.cmo : weak.ml \ 818 818 stdlib__Sys.cmi \ 819 + stdlib__Option.cmi \ 819 820 stdlib__Obj.cmi \ 820 821 stdlib__Int.cmi \ 821 822 stdlib__Hashtbl.cmi \ ··· 823 824 stdlib__Weak.cmi 824 825 stdlib__Weak.cmx : weak.ml \ 825 826 stdlib__Sys.cmx \ 827 + stdlib__Option.cmx \ 826 828 stdlib__Obj.cmx \ 827 829 stdlib__Int.cmx \ 828 830 stdlib__Hashtbl.cmx \
+16 -10
stdlib/weak.ml
··· 270 270 (* General auxiliary function for searching for a particular value 271 271 * in a hash-set, and acting according to whether or not it's found *) 272 272 273 - let find_aux t d found notfound = 273 + let find_aux t d k_found k_notfound = 274 274 let h = H.hash d in 275 275 let index = get_index t h in 276 276 let bucket = t.table.(index) in 277 277 let hashes = t.hashes.(index) in 278 278 let sz = length bucket in 279 - let rec loop i = 280 - if i >= sz then notfound h index 281 - else if h = hashes.(i) then begin 282 - match get bucket i with 283 - | Some v as opt when H.equal v d -> found bucket i opt v 284 - | _ -> loop (i + 1) 285 - end else loop (i + 1) 286 - in 287 - loop 0 279 + let found = ref None in 280 + let i = ref 0 in 281 + while !i < sz && Option.is_none !found do 282 + if h = hashes.(!i) then begin 283 + match get bucket !i with 284 + | Some v as opt -> 285 + if H.equal v d then 286 + found := opt 287 + else incr i 288 + | _ -> incr i 289 + end else incr i 290 + done; 291 + match !found with 292 + | Some v as opt -> k_found bucket !i opt v 293 + | None -> k_notfound h index 288 294 289 295 let find_opt t d = find_aux t d (fun _b _i o _v -> o) 290 296 (fun _h _i -> None)