Skip to content

Performance: O(1) app-level NSEvent monitor and synchronous CachedValue expiry #848

Description

@pacificleo

Is your feature request related to a problem? Please describe.

There are two per-keystroke/per-tick performance bottlenecks in GhosttySurfaceView:

  1. O(N) global event monitors: Every initialized GhosttySurfaceView registers its own NSEvent.addLocalMonitorForEvents. If a user restores a workspace with 30 terminal tabs, hitting a modifier key (or clicking) forces AppKit to iterate and execute 30 separate closures.
  2. Task allocation on cache miss: CachedValue (used for cachedScreenContents) spawns an async Task { try await sleep() } on every fetch miss just to clear the cache. This forces the Swift concurrency runtime to allocate and schedule a new Task continuously during high-frequency screen updates.

Describe the solution you'd like

  1. O(1) Shared Event Monitor: Register a single, static NSEvent.addLocalMonitorForEvents that maintains a weak registry (NSHashTable.weakObjects) of live surfaces. When an event fires, the single monitor checks the registry for the focused view and routes the event accordingly.
  2. Synchronous Cache Expiry: Refactor CachedValue to store a ContinuousClock.Instant. On access, synchronously compare now - fetchedAt < duration instead of allocating a Task to clear it.

Describe alternatives you've considered

No response

Are you planning to implement this yourself?

  • I intend to open a pull request once this is confirmed and marked ready.

Before submitting

  • I searched existing issues and this is not a duplicate.
  • I understand pull requests are closed until the issue is marked ready.
  • I agree to follow this project's Code of Conduct.

Activity

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