My aggregated monorepo of OCaml code, automaintained
0
fork

Configure Feed

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

Buffer HTML output to avoid per-chunk channel mutex overhead

Format.formatter_of_out_channel writes to the channel on every buffer
flush. Each write takes the OCaml multicore channel mutex (trylock +
unlock), which callgrind showed at ~3% self-time on html-generate.

Accumulate output in a Buffer and write the whole file in one call
at the end. The mutex is taken once per page instead of many times.

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

+10 -2
+10 -2
odoc/src/utils/odoc_utils.ml
··· 57 57 let with_open_out_bin fname f = 58 58 _with_resource (open_out_bin fname) ~close:close_out_noerr f 59 59 60 - (** Like [with_open_out] but operate on a [Format] buffer. *) 60 + (** Like [with_open_out] but operate on a [Format] buffer. 61 + Uses a Buffer to accumulate output and writes it to the file in one 62 + go at the end. This avoids per-chunk channel mutex acquisition in 63 + the multicore runtime. *) 61 64 let with_formatter_out fname f = 62 - with_open_out fname (fun oc -> f (Format.formatter_of_out_channel oc)) 65 + let buf = Buffer.create 65536 in 66 + let fmt = Format.formatter_of_buffer buf in 67 + let result = f fmt in 68 + Format.pp_print_flush fmt (); 69 + with_open_out fname (fun oc -> Buffer.output_buffer oc buf); 70 + result 63 71 64 72 (** Shortcuts for composing [with_open_*] functions and [Marshal]. *) 65 73 let marshal fname v =