Skip to content

Embedded macOS tick() starves Bun while waiting for AppKit #39

Closed

Description

@monotykamary

Summary

On macOS, GpuixRenderer.tick() can spend most of its time inside the embedded AppKit pump even when GPUI drawing is cheap. A Bun process that owns a continuously producing PTY then falls behind because PTY callbacks, timers, promises, and sockets cannot run while tick() is waiting for the next native wake.

Tested against @gpuix/[email protected] / @gpuix/[email protected] on Apple Silicon with Bun 1.4.0.

Reproduction and measurements

The reproducer uses a real GpuixRenderer, an 8 ms startFrameLoop, a real Bun PTY, and a deterministic DEC 2026 truecolor framebuffer producer.

With the current blocking pump, an 800×600 run measured approximately:

  • 15.6 ms median tick() latency;
  • 93% of wall time inside tick();
  • 23 PTY/service/native-stage frames per second;
  • about 0.30 ms GPUI draw p90.

The renderer was not the bottleneck; the JavaScript producer and PTY delivery were starved by the embedded pump.

A nonblocking drain on a fullscreen 1470×923 logical window (185×53 terminal grid) measured:

  • 58.7 PTY callbacks, service frames, and native stages per second, all 1:1;
  • 0.029 ms median / 0.064 ms p90 tick() latency;
  • 0.50% wall time inside tick();
  • 0.13 ms GPUI draw p90;
  • zero React commits during animation.

Proposed direction

For embedded polling, drain only work that is ready now:

  1. dequeue pending NSEvents with distantPast, bounded by a safety cap;
  2. dispatch those events and call updateWindows;
  3. run ready CoreFoundation sources with CFRunLoopRunInMode(..., 0.0, true);
  4. return control to Bun without entering NSApplication::run().

If changing the semantics of the existing embedded tick is too broad, a separate nonblocking poll API would also solve the integration problem.

Reference implementation

No PR is being opened, per the external-contributor policy.

Validation

  • GPUix native suite: 227 passed
  • GPUix React suite on the integration build: 377 passed
  • Downstream application suite: 319 passed
  • Fullscreen live PTY/native/GPUI telemetry as reported above

Would a nonblocking embedded poll be acceptable in gpui_macos, and should it replace the current tick behavior or be exposed separately?

Activity

  1. added a commit that references this issue on Sep 2, 2026
    9b1def2
  2. added a commit that references this issue on Sep 2, 2026
    5700c96
  3. added a commit that references this issue on Sep 14, 2026
    89b6799
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions