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.

Improve performance of Weak.add (#13737)

* Improve performance of Weak.add

Replace a recursive function by a while loop.
The recursive function was allocated for each element when resizing, contributing significantly
to the memory footprint of code using weak tables.

* More idiomatic code

* Changes

* nitpick from Gabriel

authored by

Vincent Laviron and committed by
GitHub
2da20f76 49baed77

+27 -27
+4
Changes
··· 122 122 (Christophe Raffalli, review by Nicolás Ojeda Bär and Gabriel Scherer and 123 123 Hugo Heuzard) 124 124 125 + - #13737: Avoid closure allocations in Weak.Make.add when resizing the 126 + table 127 + (Vincent Laviron, review by Gabriel Scherer and Daniel Bünzli) 128 + 125 129 ### Other libraries: 126 130 127 131 * #13376: Allow Dynlink.loadfile_private to load bytecode libraries with
+23 -27
stdlib/weak.ml
··· 238 238 let bucket = t.table.(index) in 239 239 let hashes = t.hashes.(index) in 240 240 let sz = length bucket in 241 - let rec loop i = 242 - if i >= sz then begin 243 - let newsz = 244 - Int.min (3 * sz / 2 + 3) (Sys.max_array_length - additional_values) 245 - in 246 - if newsz <= sz then failwith "Weak.Make: hash bucket cannot grow more"; 247 - let newbucket = weak_create newsz in 248 - let newhashes = Array.make newsz 0 in 249 - blit bucket 0 newbucket 0 sz; 250 - Array.blit hashes 0 newhashes 0 sz; 251 - setter newbucket sz d; 252 - newhashes.(sz) <- h; 253 - t.table.(index) <- newbucket; 254 - t.hashes.(index) <- newhashes; 255 - if sz <= t.limit && newsz > t.limit then begin 256 - t.oversize <- t.oversize + 1; 257 - for _i = 0 to over_limit do test_shrink_bucket t done; 258 - end; 259 - if t.oversize > Array.length t.table / over_limit then resize t; 260 - end else if check bucket i then begin 261 - loop (i + 1) 262 - end else begin 263 - setter bucket i d; 264 - hashes.(i) <- h; 241 + let i = ref 0 in 242 + while !i < sz && check bucket !i do incr i done; 243 + if !i < sz then begin 244 + setter bucket !i d; 245 + hashes.(!i) <- h; 246 + end else begin 247 + let newsz = 248 + Int.min (3 * sz / 2 + 3) (Sys.max_array_length - additional_values) 249 + in 250 + if newsz <= sz then failwith "Weak.Make: hash bucket cannot grow more"; 251 + let newbucket = weak_create newsz in 252 + let newhashes = Array.make newsz 0 in 253 + blit bucket 0 newbucket 0 sz; 254 + Array.blit hashes 0 newhashes 0 sz; 255 + setter newbucket sz d; 256 + newhashes.(sz) <- h; 257 + t.table.(index) <- newbucket; 258 + t.hashes.(index) <- newhashes; 259 + if sz <= t.limit && newsz > t.limit then begin 260 + t.oversize <- t.oversize + 1; 261 + for _i = 0 to over_limit do test_shrink_bucket t done; 265 262 end; 266 - in 267 - loop 0 268 - 263 + if t.oversize > Array.length t.table / over_limit then resize t; 264 + end 269 265 270 266 let add t d = 271 267 let h = H.hash d in