neomacs icon indicating copy to clipboard operation
neomacs copied to clipboard

[Bug]: When I click and drag to select text, the highlight lags way behind where the cursor is moving

Open definepi opened this issue 6 days ago • 3 comments

Bug summary

还是10月份的doom emacs配置(我觉得你用最新的来试也能复现),用鼠标来左键选择一片文本时,文本选中的进度远落后于光标移动的位置,体验很不好。

neomacs-debug.zip

Steps to reproduce

  1. 打开private config里的config.el
  2. 鼠标拖选文本,就能感觉到很慢了

Expected behavior

No response

Neomacs --version output

Neomacs 0.0.21
Git commit: 9ca34ffd1c9450183825ab916cac313729bdf419
Source date: 2026-11-26T04:07:21.000000000-05:00
Build: release for x86_64-unknown-linux-gnu with rustc 1.96.1
Standalone Rust binary for Neomacs (no C dependency)

Platform

Linux (Wayland)

Install source

Prebuilt release asset (downloaded from Releases)

Extra context

No response

definepi avatar Nov 26 '26 12:11 definepi

We reproduced this on Linux/Wayland (headless Sway with zwlr_virtual_pointer_v1, 1920×1080 window, 2500-line font-locked Elisp buffer with display-line-numbers-mode, global-hl-line-mode, show-paren-mode, and overlays) and measured all four candidate layers against GNU Emacs 31.1:

Configuration (1.03 s drag, 100 ms hold before release) Lisp motion events handled Redisplays gui-set-selection calls (total) Lag tail after button release
GNU Emacs 31.1 (500 Hz, select-active-regions=t, 0 ms extra hook) 135 / 500 123 152 (2.5 ms) +1.3 ms
GNU Emacs 31.1 (500 Hz, select-active-regions=t, 3 ms extra hook) 94 / 500 77 106 (0.7 ms) +15.8 ms
GNU Emacs 31.1 (1000 Hz, select-active-regions=t, 0 ms extra hook) 121 / 1000 142 145 (2.2 ms) +1.9 ms
Neomacs Wayland (500 Hz, select-active-regions=t, 0 ms hook) 501 / 500 83–255 500 (1646 ms, up to 41 ms/call) +1.8 s to +7.0 s
Neomacs Wayland (1000 Hz, select-active-regions=t, 0 ms hook) 1001 / 1000 550 1001 +23.0 s
Neomacs Wayland (500 Hz, select-active-regions=nil, 0 ms hook) 435–501 59 0 (0 ms) +280.6 ms
Neomacs Wayland (500 Hz, select-active-regions=nil, 3 ms Doom-like hook) 435–501 170 0 (0 ms) +3.1 s

Root cause identified

  1. Motion-event FIFO backlog & ignored mouse-fine-grained-tracking (primary root cause): GNU Emacs never enqueues mouse motion into a FIFO—it overwrites a single latest-position slot and suppresses sub-glyph motion when mouse-fine-grained-tracking is nil. Neomacs currently queues every Wayland PointerMoved event into its input FIFO without coalescing and delivers every pixel step to Elisp, so high-rate mice (500–1000 Hz) and Doom/Evil hooks build a multi-second replay backlog.
  2. Synchronous PRIMARY selection roundtrip through the render thread (co-primary Wayland amplifier): With select-active-regions enabled, each mouse-movement command calls gui-set-selection 'PRIMARY, which currently sends a RenderCommand::Clipboard through the render thread and synchronously blocks the evaluator until the render thread finishes its current frame and replies. Under rapid drags, this stalls input processing behind frame presentation and snowballs into multi-second lag.

We are implementing both fixes now (latest-wins motion coalescing + mouse-fine-grained-tracking, and asynchronous/render-decoupled PRIMARY selection updates) along with Wayland regression tests, and will follow up with post-fix measurements.

eval-exec avatar Nov 26 '26 12:11 eval-exec

We have implemented and verified both fixes (pending review/merge):

  1. Motion-event coalescing & mouse-fine-grained-tracking: Coalesced consecutive same-frame pointer motion events (preserving ordering barriers so the final motion before button release is never dropped) and implemented GNU-compatible glyph-box suppression (mouse-fine-grained-tracking / window-resize-pixelwise).
  2. Asynchronous, render-decoupled PRIMARY selection updates: Routed clipboard requests directly to the dedicated clipboard worker bypassing the render thread, and made PRIMARY selection updates asynchronous and coalesced (with causal flush-before-read ordering).

Before vs. after on Linux/Wayland (same headless Sway + zwlr_virtual_pointer_v1, 1920×1080 rich Elisp buffer, 1.03 s drag)

Configuration Lisp moves Redisplays gui-set-selection calls (total / max) Lag tail after release
GNU Emacs 31.1 (500 Hz, select-active-regions=t, 0 ms hook) 135 / 500 123 152 (2.5 ms) +1.3 ms
GNU Emacs 31.1 (500 Hz, select-active-regions=t, 3 ms hook) 94 / 500 77 106 (0.7 ms) +15.8 ms
GNU Emacs 31.1 (1000 Hz, select-active-regions=t, 0 ms hook) 121 / 1000 142 145 (2.2 ms) +1.9 ms
Neomacs Before (500 Hz, select-active-regions=t, 0 ms hook) 501 / 500 83–255 500 (1646 ms / 41.3 ms) +1.8 s to +7.0 s
Neomacs After (500 Hz, select-active-regions=t, 0 ms hook) 88 / 500 134 99 (2.5 ms / 0.14 ms) +6.4 ms
Neomacs Before (500 Hz, select-active-regions=t, 3 ms hook) 501 / 500 330 500 +7.2 s
Neomacs After (500 Hz, select-active-regions=t, 3 ms hook) 77 / 500 88 90 (2.7 ms / 0.16 ms) +15.5 ms
Neomacs Before (1000 Hz, select-active-regions=t, 0 ms hook) 1001 / 1000 550 1001 +23.0 s
Neomacs After (1000 Hz, select-active-regions=t, 0 ms hook) 99 / 1000 130 110 (3.3 ms / 0.18 ms) +8.5 ms
Neomacs Before (500 Hz, select-active-regions=nil, 3 ms hook) 435–501 170 0 (0 ms) +3.1 s
Neomacs After (500 Hz, select-active-regions=nil, 3 ms hook) 60 / 500 68 0 (0 ms) +28.0 ms

We also added unit tests and a headless Sway Wayland regression test (wayland_drag_selection) verifying bounded motion/selection updates and exact drag start/end preservation under a 300-event motion burst.

neomacs-agent[bot] avatar Nov 26 '26 15:11 neomacs-agent[bot]

We reproduced this on Linux/Wayland (headless Sway with zwlr_virtual_pointer_v1, 1920×1080 window, 2500-line font-locked Elisp buffer with display-line-numbers-mode, global-hl-line-mode, show-paren-mode, and overlays) and measured all four candidate layers against GNU Emacs 31.1:

Configuration (1.03 s drag, 100 ms hold before release) Lisp motion events handled Redisplays gui-set-selection calls (total) Lag tail after button release
GNU Emacs 31.1 (500 Hz, select-active-regions=t, 0 ms extra hook) 135 / 500 123 152 (2.5 ms) +1.3 ms
GNU Emacs 31.1 (500 Hz, select-active-regions=t, 3 ms extra hook) 94 / 500 77 106 (0.7 ms) +15.8 ms
GNU Emacs 31.1 (1000 Hz, select-active-regions=t, 0 ms extra hook) 121 / 1000 142 145 (2.2 ms) +1.9 ms
Neomacs Wayland (500 Hz, select-active-regions=t, 0 ms hook) 501 / 500 83–255 500 (1646 ms, up to 41 ms/call) +1.8 s to +7.0 s
Neomacs Wayland (1000 Hz, select-active-regions=t, 0 ms hook) 1001 / 1000 550 1001 +23.0 s
Neomacs Wayland (500 Hz, select-active-regions=nil, 0 ms hook) 435–501 59 0 (0 ms) +280.6 ms
Neomacs Wayland (500 Hz, select-active-regions=nil, 3 ms Doom-like hook) 435–501 170 0 (0 ms) +3.1 s

Root cause identified

  1. Motion-event FIFO backlog & ignored mouse-fine-grained-tracking (primary root cause): GNU Emacs never enqueues mouse motion into a FIFO—it overwrites a single latest-position slot and suppresses sub-glyph motion when mouse-fine-grained-tracking is nil. Neomacs currently queues every Wayland PointerMoved event into its input FIFO without coalescing and delivers every pixel step to Elisp, so high-rate mice (500–1000 Hz) and Doom/Evil hooks build a multi-second replay backlog.
  2. Synchronous PRIMARY selection roundtrip through the render thread (co-primary Wayland amplifier): With select-active-regions enabled, each mouse-movement command calls gui-set-selection 'PRIMARY, which currently sends a RenderCommand::Clipboard through the render thread and synchronously blocks the evaluator until the render thread finishes its current frame and replies. Under rapid drags, this stalls input processing behind frame presentation and snowballs into multi-second lag.

We are implementing both fixes now (latest-wins motion coalescing + mouse-fine-grained-tracking, and asynchronous/render-decoupled PRIMARY selection updates) along with Wayland regression tests, and will follow up with post-fix measurements.

neomacs-agent[bot] avatar Nov 26 '26 15:11 neomacs-agent[bot]

Fixed and landed on main in three commits:

  • 7ef90a89e0 — drag motion reaches Lisp once per glyph, as GNU: motion that stays inside the glyph last reported is suppressed (GNU x_note_mouse_movement against dpyinfo->last_mouse_glyph), and the rectangle is forgotten on the events GNU forgets it (button, leave/unmap, motion over another frame) — never on redisplay. read-char coalesces motion already waiting; the GUI input bridge coalesces consecutive moves. A 300-step X11 drag now delivers 58 motions for 85 glyph-cell changes (GNU 71; before: 301 motions, 297 region updates).
  • 8bb575394c — suppressed drag motion no longer wakes read_char into a redisplay: GNU's wait_reading_process_output keeps waiting when no event was queued and redisplays there only for timers. The X11 drag redisplays 63 times for 58 steps (before: 274 for 58), and button-release → mouse-set-region is 5–9 ms (GNU 4–11 ms; before 15–26 ms).
  • b9a0ab6bca — the Wayland drag row now waits for the editor itself to report the pointer at the drag start: a failing run's protocol trace showed the press 200 ms before wl_pointer.enter, so the press reached no client. (The earlier llvmpipe "product defect" reading was an artifact of that harness race and was retracted; the fixture, not the compositor, was at fault.)

last_mouse_glyph now lives per frame, as GNU's dpyinfo->last_mouse_glyph does.

Verification: the full union rows for the landing batch (45,447 passed; the only four failures are the pre-existing #886 font-stack rows, unrelated), the drag/X11 and Wayland GUI oracle rows against attested GNU 31.1, and the fix-forward stability row. Please retest on the next build and reopen if anything still trails the pointer.

neomacs-agent[bot] avatar Nov 28 '26 10:11 neomacs-agent[bot]