patches: fix O(overlays) performance regression

Performance issue: editing large files (>~10KB, >2000 lines) caused
progressive slowdown regardless of VoiceOver status.

Root causes:
1. ns_zoom_find_overlay_candidate_line: called Foverlays_in on the
   entire visible buffer range on every redisplay when UAZoomEnabled().
   In files with many overlays (font-lock, hl-line, show-paren etc.)
   this was O(overlays) Lisp work per keystroke.

2. postAccessibilityNotificationsForFrame: when ns-accessibility-enabled
   is non-nil, checked BUF_OVERLAY_MODIFF every redisplay. font-lock
   bumps this on every redraw, triggering ns_ax_selected_overlay_text
   (another O(overlays) scan) for non-minibuffer windows.

Fix: Both scans now guard with MINI_WINDOW_P check. Overlay completion
frameworks (Vertico, Icomplete, Ivy) only display candidates in
minibuffer windows --- no completion framework puts selected-face
overlays in normal editing buffers. For non-minibuffer windows both
functions return immediately with zero Lisp calls.

Additionally: ns_zoom_find_child_frame_candidate is skipped when
f->child_frame_list is nil (no child frames = no Corfu popup).
This commit is contained in:
2026-03-01 04:26:12 +01:00
parent 84eb777065
commit 3d2fa7a54e
9 changed files with 87 additions and 69 deletions

View File

@@ -1,4 +1,4 @@
From a9ea010735a6839a9f5cceb728e1a072cf0b3bfb Mon Sep 17 00:00:00 2001
From 8c424f291b8a635e2b1cd7f3303bc726b5d21a8a Mon Sep 17 00:00:00 2001
From: Martin Sukany <martin@sukany.cz>
Date: Sat, 28 Feb 2026 14:46:25 +0100
Subject: [PATCH 8/9] ns: announce overlay completion candidates for VoiceOver
@@ -45,8 +45,8 @@ Key implementation details:
Independent overlay branch, BUF_CHARS_MODIFF gating, candidate
---
src/nsterm.h | 1 +
src/nsterm.m | 306 +++++++++++++++++++++++++++++++++++++++++++++------
2 files changed, 276 insertions(+), 31 deletions(-)
src/nsterm.m | 315 ++++++++++++++++++++++++++++++++++++++++++++++-----
2 files changed, 285 insertions(+), 31 deletions(-)
diff --git a/src/nsterm.h b/src/nsterm.h
index 6e830de..2102fb9 100644
@@ -61,10 +61,10 @@ index 6e830de..2102fb9 100644
@property (nonatomic, assign) BOOL cachedMarkActive;
@property (nonatomic, copy) NSString *cachedCompletionAnnouncement;
diff --git a/src/nsterm.m b/src/nsterm.m
index 93da88a..b3bdd42 100644
index c35db76..7e34286 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -7147,11 +7147,154 @@ Accessibility virtual elements (macOS / Cocoa only)
@@ -7156,11 +7156,154 @@ Accessibility virtual elements (macOS / Cocoa only)
/* ---- Helper: extract buffer text for accessibility ---- */
@@ -220,7 +220,7 @@ index 93da88a..b3bdd42 100644
static NSString *
ns_ax_buffer_text (struct window *w, ptrdiff_t *out_start,
ns_ax_visible_run **out_runs, NSUInteger *out_nruns)
@@ -7222,7 +7365,7 @@ Accessibility virtual elements (macOS / Cocoa only)
@@ -7231,7 +7374,7 @@ Accessibility virtual elements (macOS / Cocoa only)
/* Extract this visible run's text. Use
Fbuffer_substring_no_properties which correctly handles the
@@ -229,7 +229,7 @@ index 93da88a..b3bdd42 100644
include garbage bytes when the run spans the gap position. */
Lisp_Object lstr = Fbuffer_substring_no_properties (
make_fixnum (pos), make_fixnum (run_end));
@@ -7303,7 +7446,7 @@ Mode lines using icon fonts (e.g. doom-modeline with nerd-font)
@@ -7312,7 +7455,7 @@ Mode lines using icon fonts (e.g. doom-modeline with nerd-font)
return NSZeroRect;
/* charpos_start and charpos_len are already in buffer charpos
@@ -238,7 +238,7 @@ index 93da88a..b3bdd42 100644
charposForAccessibilityIndex which handles invisible text. */
ptrdiff_t cp_start = charpos_start;
ptrdiff_t cp_end = cp_start + charpos_len;
@@ -7782,6 +7925,7 @@ @implementation EmacsAccessibilityBuffer
@@ -7791,6 +7934,7 @@ @implementation EmacsAccessibilityBuffer
@synthesize cachedOverlayModiff;
@synthesize cachedTextStart;
@synthesize cachedModiff;
@@ -246,7 +246,7 @@ index 93da88a..b3bdd42 100644
@synthesize cachedPoint;
@synthesize cachedMarkActive;
@synthesize cachedCompletionAnnouncement;
@@ -7879,7 +8023,7 @@ - (void)ensureTextCache
@@ -7888,7 +8032,7 @@ - (void)ensureTextCache
NSTRACE ("EmacsAccessibilityBuffer ensureTextCache");
/* This method is only called from the main thread (AX getters
dispatch_sync to main first). Reads of cachedText/cachedTextModiff
@@ -255,7 +255,7 @@ index 93da88a..b3bdd42 100644
write section at the end needs synchronization to protect
against concurrent reads from AX server thread. */
eassert ([NSThread isMainThread]);
@@ -7892,16 +8036,15 @@ - (void)ensureTextCache
@@ -7901,16 +8045,15 @@ - (void)ensureTextCache
return;
ptrdiff_t modiff = BUF_MODIFF (b);
@@ -278,7 +278,7 @@ index 93da88a..b3bdd42 100644
&& cachedTextStart == BUF_BEGV (b)
&& pt >= cachedTextStart
&& (textLen == 0
@@ -7918,7 +8061,6 @@ - (void)ensureTextCache
@@ -7927,7 +8070,6 @@ - (void)ensureTextCache
[cachedText release];
cachedText = [text retain];
cachedTextModiff = modiff;
@@ -286,7 +286,7 @@ index 93da88a..b3bdd42 100644
cachedTextStart = start;
if (visibleRuns)
@@ -7983,7 +8125,7 @@ - (NSUInteger)accessibilityIndexForCharpos:(ptrdiff_t)charpos
@@ -7992,7 +8134,7 @@ - (NSUInteger)accessibilityIndexForCharpos:(ptrdiff_t)charpos
/* Binary search: runs are sorted by charpos (ascending). Find the
run whose [charpos, charpos+length) range contains the target,
or the nearest run after an invisible gap. O(log n) instead of
@@ -295,7 +295,7 @@ index 93da88a..b3bdd42 100644
NSUInteger lo = 0, hi = visibleRunCount;
while (lo < hi)
{
@@ -7996,7 +8138,7 @@ - (NSUInteger)accessibilityIndexForCharpos:(ptrdiff_t)charpos
@@ -8005,7 +8147,7 @@ - (NSUInteger)accessibilityIndexForCharpos:(ptrdiff_t)charpos
else
{
/* Found: charpos is inside this run. Compute UTF-16 delta
@@ -304,7 +304,7 @@ index 93da88a..b3bdd42 100644
NSUInteger chars_in = (NSUInteger)(charpos - r->charpos);
if (chars_in == 0 || !cachedText)
return r->ax_start;
@@ -8021,10 +8163,10 @@ - (NSUInteger)accessibilityIndexForCharpos:(ptrdiff_t)charpos
@@ -8030,10 +8172,10 @@ - (NSUInteger)accessibilityIndexForCharpos:(ptrdiff_t)charpos
/* Convert accessibility string index to buffer charpos.
Safe to call from any thread: uses only cachedText (NSString) and
@@ -317,7 +317,7 @@ index 93da88a..b3bdd42 100644
@synchronized (self)
{
if (visibleRunCount == 0)
@@ -8058,7 +8200,7 @@ - (ptrdiff_t)charposForAccessibilityIndex:(NSUInteger)ax_idx
@@ -8067,7 +8209,7 @@ - (ptrdiff_t)charposForAccessibilityIndex:(NSUInteger)ax_idx
return cp;
}
}
@@ -326,7 +326,7 @@ index 93da88a..b3bdd42 100644
if (lo > 0)
{
ns_ax_visible_run *last = &visibleRuns[visibleRunCount - 1];
@@ -8080,7 +8222,7 @@ - (ptrdiff_t)charposForAccessibilityIndex:(NSUInteger)ax_idx
@@ -8089,7 +8231,7 @@ - (ptrdiff_t)charposForAccessibilityIndex:(NSUInteger)ax_idx
deadlocking the AX server thread. This is prevented by:
1. validWindow checks WINDOW_LIVE_P and BUFFERP before every
@@ -335,7 +335,7 @@ index 93da88a..b3bdd42 100644
2. All dispatch_sync blocks run on the main thread where no
concurrent Lisp code can modify state between checks.
3. block_input prevents timer events and process output from
@@ -8434,6 +8576,50 @@ - (NSInteger)accessibilityInsertionPointLineNumber
@@ -8443,6 +8585,50 @@ - (NSInteger)accessibilityInsertionPointLineNumber
return [self lineForAXIndex:point_idx];
}
@@ -386,7 +386,7 @@ index 93da88a..b3bdd42 100644
- (NSRange)accessibilityRangeForLine:(NSInteger)line
{
if (![NSThread isMainThread])
@@ -8656,7 +8842,7 @@ - (NSRect)accessibilityFrame
@@ -8665,7 +8851,7 @@ - (NSRect)accessibilityFrame
/* ===================================================================
@@ -395,7 +395,7 @@ index 93da88a..b3bdd42 100644
These methods notify VoiceOver of text and selection changes.
Called from the redisplay cycle (postAccessibilityUpdates).
@@ -8671,7 +8857,7 @@ - (void)postTextChangedNotification:(ptrdiff_t)point
@@ -8680,7 +8866,7 @@ - (void)postTextChangedNotification:(ptrdiff_t)point
if (point > self.cachedPoint
&& point - self.cachedPoint == 1)
{
@@ -404,7 +404,7 @@ index 93da88a..b3bdd42 100644
[self invalidateTextCache];
[self ensureTextCache];
if (cachedText)
@@ -8690,7 +8876,7 @@ - (void)postTextChangedNotification:(ptrdiff_t)point
@@ -8699,7 +8885,7 @@ - (void)postTextChangedNotification:(ptrdiff_t)point
/* Update cachedPoint here so the selection-move branch does NOT
fire for point changes caused by edits. WebKit and Chromium
never send both ValueChanged and SelectedTextChanged for the
@@ -413,7 +413,7 @@ index 93da88a..b3bdd42 100644
self.cachedPoint = point;
NSDictionary *change = @{
@@ -9023,14 +9209,72 @@ - (void)postAccessibilityNotificationsForFrame:(struct frame *)f
@@ -9032,14 +9218,81 @@ - (void)postAccessibilityNotificationsForFrame:(struct frame *)f
BOOL markActive = !NILP (BVAR (b, mark_active));
/* --- Text changed (edit) --- */
@@ -450,6 +450,14 @@ index 93da88a..b3bdd42 100644
+ {
+ self.cachedOverlayModiff = BUF_OVERLAY_MODIFF (b);
+
+ /* Overlay completion candidates (Vertico, Icomplete, Ivy) are
+ displayed in the minibuffer. In normal editing buffers,
+ font-lock and other modes change BUF_OVERLAY_MODIFF on
+ every redisplay, triggering O(overlays) work per keystroke.
+ Restrict the scan to minibuffer windows. */
+ if (!MINI_WINDOW_P (w))
+ goto skip_overlay_scan;
+
+ int selected_line = -1;
+ NSString *candidate
+ = ns_ax_selected_overlay_text (b, BUF_BEGV (b), BUF_ZV (b),
@@ -481,6 +489,7 @@ index 93da88a..b3bdd42 100644
+ }
+ }
}
+ skip_overlay_scan:;
/* --- Cursor moved or selection changed ---
- Use 'else if' — edits and selection moves are mutually exclusive
@@ -488,7 +497,7 @@ index 93da88a..b3bdd42 100644
per the WebKit/Chromium pattern. */
else if (point != self.cachedPoint || markActive != self.cachedMarkActive)
{
@@ -9200,7 +9444,7 @@ - (NSRect)accessibilityFrame
@@ -9209,7 +9462,7 @@ - (NSRect)accessibilityFrame
/* ===================================================================
@@ -497,7 +506,7 @@ index 93da88a..b3bdd42 100644
=================================================================== */
/* Scan visible range of window W for interactive spans.
@@ -9408,7 +9652,7 @@ - (void) setAccessibilityFocused: (BOOL) focused
@@ -9417,7 +9670,7 @@ - (void) setAccessibilityFocused: (BOOL) focused
dispatch_async (dispatch_get_main_queue (), ^{
/* lwin is a Lisp_Object captured by value. This is GC-safe
because Lisp_Objects are tagged integers/pointers that
@@ -506,7 +515,7 @@ index 93da88a..b3bdd42 100644
Emacs. The WINDOW_LIVE_P check below guards against the
window being deleted between capture and execution. */
if (!WINDOWP (lwin) || NILP (Fwindow_live_p (lwin)))
@@ -9434,7 +9678,7 @@ - (void) setAccessibilityFocused: (BOOL) focused
@@ -9443,7 +9696,7 @@ - (void) setAccessibilityFocused: (BOOL) focused
@end
@@ -515,7 +524,7 @@ index 93da88a..b3bdd42 100644
Methods are kept here (same .m file) so they access the ivars
declared in the @interface ivar block. */
@implementation EmacsAccessibilityBuffer (InteractiveSpans)
@@ -12159,7 +12403,7 @@ - (int) fullscreenState
@@ -12168,7 +12421,7 @@ - (int) fullscreenState
if (WINDOW_LEAF_P (w))
{
@@ -524,7 +533,7 @@ index 93da88a..b3bdd42 100644
EmacsAccessibilityBuffer *elem
= [existing objectForKey:[NSValue valueWithPointer:w]];
if (!elem)
@@ -12193,7 +12437,7 @@ - (int) fullscreenState
@@ -12202,7 +12455,7 @@ - (int) fullscreenState
}
else
{
@@ -533,7 +542,7 @@ index 93da88a..b3bdd42 100644
Lisp_Object child = w->contents;
while (!NILP (child))
{
@@ -12305,7 +12549,7 @@ - (void)postAccessibilityUpdates
@@ -12314,7 +12567,7 @@ - (void)postAccessibilityUpdates
accessibilityUpdating = YES;
/* Detect window tree change (split, delete, new buffer). Compare
@@ -542,7 +551,7 @@ index 93da88a..b3bdd42 100644
Lisp_Object curRoot = FRAME_ROOT_WINDOW (emacsframe);
if (!EQ (curRoot, lastRootWindow))
{
@@ -12314,12 +12558,12 @@ - (void)postAccessibilityUpdates
@@ -12323,12 +12576,12 @@ - (void)postAccessibilityUpdates
}
/* If tree is stale, rebuild FIRST so we don't iterate freed