From ce1b818847d6e30f9ece9d241127158c655459dd Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Thu, 15 Oct 2026 19:21:11 +0000 Subject: [PATCH] Start watch rebuilds once a save's events settle instead of after 50 ms Waiting a fixed 50 ms after the first event added most of an edit's latency. The watcher now starts once no event has arrived for 10 ms, at most 50 ms after the first. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72 Signed-off-by: Christoph Knittel --- changes/ocaml-rewatch-watch-settle.polish.md | 1 + rewatch-ocaml/watcher.ml | 23 ++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 changes/ocaml-rewatch-watch-settle.polish.md diff --git a/changes/ocaml-rewatch-watch-settle.polish.md b/changes/ocaml-rewatch-watch-settle.polish.md new file mode 100644 index 0000000000..28301911d2 --- /dev/null +++ b/changes/ocaml-rewatch-watch-settle.polish.md @@ -0,0 +1 @@ +- OCaml rewatch: watch mode starts a rebuild as soon as the burst of events from a save has settled, instead of always waiting 50 ms. https://github.com/rescript-lang/rescript/pull/8873 diff --git a/rewatch-ocaml/watcher.ml b/rewatch-ocaml/watcher.ml index 79a9cae550..804c785727 100644 --- a/rewatch-ocaml/watcher.ml +++ b/rewatch-ocaml/watcher.ml @@ -211,8 +211,27 @@ let run_locked ~native_create ~report_native_fallback ~root ~prod ~features | Native_watcher.Failed message -> Some {message; scope; snapshot = previous} | Native_watcher.Changed events -> ( - delay 0.05; - let events = events @ Native_watcher.drain watcher in + (* Collect the rest of the burst an editor's save produces: wait until + no event has arrived for [quiet] seconds, at most [limit] seconds in + all. Changes arriving later start another build. A clock set back + ends the wait too. *) + let quiet = 0.01 + and limit = 0.05 in + let started = Unix.gettimeofday () in + let rec settle collected last_event = + let now = Unix.gettimeofday () in + if + now -. last_event >= quiet + || now -. started >= limit + || now < last_event + then collected + else ( + delay 0.002; + match Native_watcher.drain watcher with + | [] -> settle collected last_event + | more -> settle (collected @ more) (Unix.gettimeofday ())) + in + let events = settle events started in let direct = direct_content_changes watcher scope symlink_targets events in