this repo has no description
1
fork

Configure Feed

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

Cache mode printing to avoid repeated Format.asprintf

OxCaml mode annotations (locality, uniqueness, linearity, etc.) were
printed via Format.asprintf per argument type, allocating a fresh
Format buffer each time. With 150K items × multiple args, this was
24% of allocation in statmemprof.

Cache the result per mode constant value (small finite set — ~16
entries). Reduces minor heap allocation by 20%.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

+14 -1
+14 -1
src/loader/cmi.cppo.ml
··· 495 495 #if defined OXCAML 496 496 (** Extract non-default mode strings from an OxCaml argument mode. 497 497 Replicates the logic from [Printtyp.tree_of_modes]. *) 498 + (* Cache for mode printing — mode constants are a small finite set so the 499 + cache stays tiny. Avoids allocating a fresh Format buffer per call. *) 500 + let mode_print_cache : (Obj.t, string) Hashtbl.t = Hashtbl.create 16 501 + 502 + let print_mode_cached (print : Format.formatter -> 'a -> unit) (v : 'a) : string = 503 + let key = Obj.repr v in 504 + match Hashtbl.find_opt mode_print_cache key with 505 + | Some s -> s 506 + | None -> 507 + let s = Format.asprintf "%a" print v in 508 + Hashtbl.replace mode_print_cache key s; 509 + s 510 + 498 511 let extract_arg_modes marg = 499 512 let modes = Mode.Alloc.zap_to_legacy marg in 500 513 let diff = Mode.Alloc.Const.diff modes Mode.Alloc.Const.legacy in ··· 520 533 | _, _ -> diff.portability 521 534 in 522 535 let print_opt print a = 523 - Option.map (fun v -> Format.asprintf "%a" print v) a 536 + Option.map (fun v -> print_mode_cached print v) a 524 537 in 525 538 List.filter_map Fun.id 526 539 [ print_opt Mode.Locality.Const.print diff.areality