Skip to content

JavaScriptEventLoop: Read the clock less often with yieldInterval - #27

Closed
krodak wants to merge 1 commit into
mainfrom
kr/event-loop-yield-interval-adaptive-clock
Closed

krodak wants to merge 1 commit into
mainfrom
kr/event-loop-yield-interval-adaptive-clock

Conversation

@krodak

@krodak krodak commented Oct 16, 2026

Copy link
Copy Markdown
Collaborator

Overview

Follow-up to #26, from the review of swiftwasm#821. With yieldInterval set, the drain reads the clock after every job. ContinuousClock.now goes through the WASI shim to performance.now(), and for small jobs that read costs more than the job itself.

This PR reads the clock every N jobs instead. N starts at 1 and doubles (up to 256) while less than a quarter of the interval has passed, so slow jobs are still checked after every job, and long runs of small jobs pay for a clock read only now and then. With yieldInterval == nil nothing changes.

1M chained Task.yield() hops, Node.js, release nil yieldInterval set
Clock read after every job (#26) ~135 ns/job ~820 ns/job
This PR ~135 ns/job ~195 ns/job

Trade-off: if a drain switches from many small jobs to slow ones, up to N - 1 slow jobs can run before the next clock read, so the yield can come later than the interval. N goes back to 1 at the start of every drain.

Testing

  • make unittest
  • Embedded: JavaScriptEventLoop builds with the Embedded SDK.
  • Benchmark: 1M chained Task.yield() hops, 3 interleaved runs per build, median of 5 rounds each.
  • ./Utilities/format.swift applied.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant