From c6b73c75b9ed90cf3d8da8cbd31b34709ba9c50e Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Sat, 7 Nov 2026 06:48:35 +0000 Subject: [PATCH] Start watch rebuilds once a save's events settle instead of after 50 ms Why: After the first file event of a save, the watcher always waited 50 ms to collect the rest of the burst an editor produces, which was most of the latency of an edit. The watcher now starts the rebuild once no event has arrived for 10 ms, and still waits at most 50 ms after the first event. Events arriving later start another build, as before; a clock set back ends the wait. Watch modified (time from saving a file in tests/tests until its JS contains the edit, median of 20): Before (text): plain write 65 ms atomic save 75 ms After (text): plain write 27 ms atomic save 38 ms Review: Can one save now cause two rebuilds? Only when its events arrive more than 10 ms apart, such as a format-on-save that writes the file again later; each save in the measurement above caused exactly one rebuild. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WZbCuRQ1Tn31bnKzLj5e72 Signed-off-by: Christoph Knittel --- changes/watch-settle.polish.md | 1 + rewatch-ocaml/watcher.ml | 23 +++++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 changes/watch-settle.polish.md diff --git a/changes/watch-settle.polish.md b/changes/watch-settle.polish.md new file mode 100644 index 0000000000..a65a1cb652 --- /dev/null +++ b/changes/watch-settle.polish.md @@ -0,0 +1 @@ +- `rescript watch` starts a rebuild as soon as the burst of file events from a save has settled, instead of always waiting 50 ms. diff --git a/rewatch-ocaml/watcher.ml b/rewatch-ocaml/watcher.ml index 8e2a0f0cee..821fffa4f0 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