Repository navigation
perf(runtime): settle Promises through the event loop; check threads, not run loops - #608
Conversation
…mise proxy The Promise proxy decided whether to marshal a settlement by comparing CFRunLoopGetCurrent() against the runtime loop, both when a Promise was constructed and when it settled. CFRunLoopGetCurrent() creates a run loop for any thread that has none, so every GCD/NSOperationQueue thread that created or settled a Promise got a run loop allocated just to learn it was not the runtime thread. The binding now exposes isRuntimeThread() (pthread_equal against the runtime thread, captured alongside the runtime loop), and the builtin only calls CFRunLoopGetCurrent() on the runtime thread, once per isolate.
|
No actionable comments were generated in the recent review. Walkthrough
Merge Risk:
|
…over run-loop checks The Promise proxy marshaled runtime-thread settlements with CFRunLoopPerformBlock, which converted every bound resolve/reject into an ObjC block and kept a CFRunLoopRef wrapper per Promise. The builtin now queues settlements in a JS FIFO, and a native postToRuntimeThread() posts a capture-free internal-lane entry per settlement that runs the oldest one on the runtime thread. The builtin no longer touches CFRunLoop at all. Settlements now run in common modes (during UI tracking too), one per run-loop pass, each followed by the lane's microtask checkpoint. The other "is this the runtime thread" checks that compared CFRunLoopGetCurrent() against a stored loop are now thread checks too: ClassBuilder's retain/release hooks and GetNapiEnvIfAlive use Runtime::IsOnRuntimeThread(), and NapiEnv/threadsafe functions use a pthread_threadid_np id, which is never reused, so the tsfn no longer retains the run loop to keep its identity stable. Runtime::RuntimeLoop() and NapiEnv::RuntimeLoop() are gone, along with the vacuous RuntimeLoop() != nullptr guards.
What
Several places decided "is this the runtime thread?" by comparing
CFRunLoopGetCurrent()with a stored run loop.CFRunLoopGetCurrent()creates a run loop for any thread that lacks one, so every GCD /NSOperationQueuethread that hit one of these checks got a run loop allocated just to learn it wasn't the runtime thread. The Promise proxy was the worst case: it did this on every construct and every cross-thread settle. It also marshaled settlements withCFRunLoopPerformBlock, which meant converting each boundresolve/rejectinto an ObjC block and keeping aCFRunLoopRefwrapper per Promise.Promise proxy
isRuntimeThread(), which callsRuntime::IsOnRuntimeThread()(pthread_equal).postToRuntimeThread()posts a capture-freeEventLoop::PostInternalentry for each one. On the runtime thread, each entry calls the builtin's exportedrunNext(), which runs exactly one queued settlement. A post refused at shutdown doesn't enqueue, so posts and queued settlements always pair 1:1. The exported function lives in aStateForslot, and nov8::Globalis created per post.CFRunLoopGetCurrent,CFRunLoopPerformBlockorCFRunLoopWakeUp.Checks.h). Init failures (binding install, builtin run or export shape, state slot) areNS_CHECKs, nottns::Assert. A failedrunNextcall leaves its exception pending for the event loop entry, as the policy requires.Behaviour changes (intended):
kCFRunLoopDefaultModeonly, so a settlement from a background thread waited out UI tracking such as scrolling.Other run-loop identity checks → thread checks
ClassBuilder.mmretain/release hooks andRuntime::GetNapiEnvIfAlive→Runtime::IsOnRuntimeThread().NapiEnv/ threadsafe functions now use apthread_threadid_npID. Unlike apthread_t, it is never reused after the thread exits. That keepsnapi_would_deadlocksafe for producers that outlive the env's thread, so the tsfn no longer has toCFRetainthe run loop.Runtime::RuntimeLoop(),NapiEnv::RuntimeLoop()and theRuntimeLoop() != nullptrguards innapi_create_threadsafe_function/napi_create_async_work. Those guards were vacuous because the loop was always set in the constructor.The only
CFRunLoopGetCurrent()calls left are home-thread captures at init (Runtime::Init,EventLoop::BindToCurrentThread,WorkerWrapper).Testing
./run_tests.sh --temp-sim): 0 failures, 2280 specs.napi_would_deadlockhas no spec.napi_make_callbackis only exercised from the env thread, so the off-thread rejection branch is not covered.Summary by CodeRabbit