Skip to content

Coalesce Windows move/resize events and add fast paths for zero listeners and message dispatch - #96

Merged
lijy91 merged 1 commit into
mainfrom
fix/windows-resize-lag-issue-30
Nov 19, 2026
Merged

lijy91 merged 1 commit into
mainfrom
fix/windows-resize-lag-issue-30

Conversation

@lijy91

@lijy91 lijy91 commented Nov 19, 2026

Copy link
Copy Markdown
Member

Summary

Addresses all four Windows resize/drag performance root causes reported in libnativeapi/nativeapi#30:

  1. Lock-free zero-listener fast path (src/foundation/event_emitter.h, src/platform/windows/window_manager_windows.cpp):
    • Add an atomic listener_count_ and lock-free HasAnyListeners() to EventEmitter; short-circuit Emit() when zero listeners are registered.
    • Track g_has_window_listeners in window_manager_windows.cpp (refreshing baseline snapshots on 0→1 listener transition and clearing pending timers on 1→0). When zero listeners exist, WindowGeometryProc passes straight through to DefSubclassProc without locking, diffing snapshots, or constructing events.
  2. Coalesce WindowMovedEvent / WindowResizedEvent and remove redundant getter round-trips (src/platform/windows/window_manager_windows.cpp):
    • During interactive resize/move (WM_ENTERSIZEMOVE..WM_EXITSIZEMOVE) and rapid SetWindowPos bursts within 50 ms, defer geometry delivery to a low-priority 50 ms WM_TIMER instead of constructing and delivering events synchronously inside every WM_WINDOWPOSCHANGED; flush the final geometry immediately on WM_EXITSIZEMOVE.
    • Isolated moves/resizes outside a burst still emit immediately (0 ms delay).
    • Compute logical position and size directly from the already-captured WindowSnapshot instead of re-querying WindowRegistry + GetSize() / GetPosition() per event.
  3. WindowMessageDispatcher fast path with SetWindowSubclass + lock-free message bitmask (src/platform/windows/window_message_dispatcher.{h,cpp}, src/platform/windows/window_windows.cpp):
    • Switch hooking from SetWindowLongPtr(GWLP_WNDPROC) to SetWindowSubclass with per-window WindowState passed via dwRefData and a lock-free atomic message-interest bitmask (0..1023).
    • Add a message-filtered RegisterHandler(HWND, std::initializer_list<UINT>, handler) overload and switch minimumSize/maximumSize (WM_GETMINMAXINFO), aspectRatio (WM_SIZING), and alwaysOnBottom (WM_WINDOWPOSCHANGING) to it so unrelated messages bypass mutex locking, hash lookups, vector allocation, and sorting.
  4. Tests (tests/window_geometry_coalesce_windows_test.cpp):
    • Cover message-filtered dispatch, zero-listener fast path without stale moves, isolated immediate emit, 60-step rapid left-edge SetWindowPos burst coalescing, and WM_ENTERSIZEMOVE..WM_EXITSIZEMOVE session-edge flushing.

Related to libnativeapi/nativeapi#30.

Verification

  • Passed 10 Windows CTest suites: window_geometry_coalesce_windows_test, window_manager_ui_thread_windows_test, window_cross_thread_dialog_windows_test, window_property_changed_test, window_startup_maximize_windows_test, window_close_windows_test, view_updates_windows_test, tray_template_windows_test, window_popup_caption_windows_test, and window_maximize_button_test.

@lijy91
lijy91 force-pushed the fix/windows-resize-lag-issue-30 branch from f75d3f6 to 8bf490c Compare November 19, 2026 12:25
@lijy91
lijy91 merged commit 46bfe7b into main Nov 19, 2026
@lijy91
lijy91 deleted the fix/windows-resize-lag-issue-30 branch November 19, 2026 12:25
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