Skip to content

Commit dcc7e35

Browse files
cknittclaude
andauthored
Reuse one copy and comparison buffer per domain in OCaml rewatch (#8869)
Fresh 64 KiB buffers per copied file were the largest single source of allocation in a build. Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72 Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f36c206 commit dcc7e35

2 files changed

Lines changed: 11 additions & 4 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- OCaml rewatch: reuse one copy buffer per worker instead of allocating one per published file, which makes clean builds faster. https://github.com/rescript-lang/rescript/pull/8869

‎rewatch-ocaml/file_util.ml‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,13 @@ let write_file_atomic ?(ensure_parent = true) path contents =
109109
Sys.rename candidate path;
110110
published := true)
111111

112+
(* Copy and comparison buffers, one set per domain: allocating fresh 64 KiB
113+
buffers for every copied or compared file was the largest single source of
114+
allocation in a build. Neither function calls the other while it holds the
115+
buffers. *)
116+
let copy_buffer = Domain.DLS.new_key (fun () -> Bytes.create 65_536)
117+
let compare_buffer = Domain.DLS.new_key (fun () -> Bytes.create 65_536)
118+
112119
let copy_existing_file source destination =
113120
let input_descriptor =
114121
Unix.openfile source [Unix.O_RDONLY; Unix.O_CLOEXEC] 0
@@ -125,7 +132,7 @@ let copy_existing_file source destination =
125132
let output_channel = open_out_bin destination in
126133
try
127134
with_output_channel output_channel (fun output_channel ->
128-
let buffer = Bytes.create 65_536 in
135+
let buffer = Domain.DLS.get copy_buffer in
129136
let rec copy () =
130137
let count = input input_channel buffer 0 (Bytes.length buffer) in
131138
if count > 0 then (
@@ -173,9 +180,8 @@ let files_equal first second =
173180
Fun.protect
174181
~finally:(fun () -> close_in_noerr second_channel)
175182
(fun () ->
176-
let buffer_size = 65_536 in
177-
let first_buffer = Bytes.create buffer_size in
178-
let second_buffer = Bytes.create buffer_size in
183+
let first_buffer = Domain.DLS.get copy_buffer in
184+
let second_buffer = Domain.DLS.get compare_buffer in
179185
let rec read_chunk channel buffer offset =
180186
if offset = Bytes.length buffer then offset
181187
else

0 commit comments

Comments
 (0)