Repository navigation
Give every timed wait an exact deadline - #1357
Merged
Merged
Conversation
Timed waits other than Thread/sleep built their deadline as a whole millisecond rounded up from now, so a 1ms deref, poll, latch await, join, waitFor, core.async timeout or socket SO_TIMEOUT took ~2ms (tryLock 3.5ms) where the JDK takes ~1.25ms. mono-deadline is exact now, the poll loops (tryLock, waitFor, the poller) size their last nap by the time actually left, and tu->ms keeps sub-millisecond units instead of truncating them. One-shot waits take a timeout that starts at the first wait (jolt-timeout), so a wait that answers at once reads no clock; settled derefs and free tryLocks get ~40% cheaper. A timed tryLock now throws on an interrupted thread, as the JDK's does.
The old row needed under 500us between schedule and getDelay, which a cold JVM on CI does not manage. Compare two schedules instead: b is made and read after a, so on the JVM its delay is at least 999us more whatever the clock does, while the truncating conversion made both 1ms.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Stacked on #1340 (base is fix/sleep-overshoot; retarget to main once that merges).
#1340 made Thread/sleep exact. Every other timed wait still built its deadline as a whole millisecond rounded up from now, so a 1ms wait took ~2ms where the JDK takes ~1.25ms. This makes mono-deadline exact for all of them and fixes the places that compared or subtracted deadlines against the truncated mono-millis clock.
Sites: timed deref of promise/future, BlockingQueue poll/offer, CountDownLatch.await, ReentrantLock.tryLock, awaitTermination, Future.get, CompletableFuture.get, FutureTask.get, Thread.join, Object.wait, Process.waitFor, await-for, core.async timeout, scheduled executor delays/getDelay/keep-alive, and the io poller (socket SO_TIMEOUT on read/accept). jolt has no Semaphore or Condition, so those aren't covered.
Median of 101 x 1ms waits, macOS, in us:
Also fixed:
(.poll q 500 TimeUnit/MICROSECONDS)returned at once and a 1500us schedule delay was 1ms.One-shot waits now pass a timeout that starts at the first actual wait (jolt-timeout in locks.ss), so a wait that answers at once doesn't read the clock. Building an exact deadline costs a rational division, and skipping it makes the non-blocking paths faster. A/B/A, ns/op min (median):
Tests: async-timer-test section 11 times each site's 1ms wait interleaved with a sleep(1) from the same run, and retries a round that noise puts behind. All 15 rows fail on #1340's code. There's a socket-test row for SO_TIMEOUT, and corpus rows for interrupted tryLock, negative timeouts and sub-ms schedule delay, all checked against the clojure CLI.