patches: fix O(position) lag — O(1) fast path in charposForAccessibilityIndex:

charposForAccessibilityIndex: walked composed character sequences
from the start of a visible run to the target AX index.  For a run
covering an entire ASCII buffer, this is O(cursor_position): moving
to line 10,000 requires ~500,000 iterations per call.

The method is called on every SelectedTextChanged notification
response (accessibilityBoundsForRange: from the AX server for cursor
tracking), making cursor movement O(position) in large files.

Fix: when ax_length == length for a run (all characters are single
AX index units — true for all ASCII/Latin text), the charpos offset
is simply ax_idx - run.ax_start.  O(1) instead of O(position).

Multi-byte runs (emoji, CJK, non-BMP) fall back to the sequence walk,
bounded by run length (visible window size), not total buffer size.
This commit is contained in:
2026-03-01 09:03:01 +01:00
parent 31ad038360
commit fb68dd50ea
9 changed files with 73 additions and 66 deletions

View File

@@ -1,7 +1,7 @@
From 4a81923ad68bc5866f7343b37429a882f7a388dd Mon Sep 17 00:00:00 2001
From ae1c7d6451f5a6be397f50c314e03b43b4e47b5c Mon Sep 17 00:00:00 2001
From: Martin Sukany <martin@sukany.cz>
Date: Sat, 28 Feb 2026 12:58:11 +0100
Subject: [PATCH 6/9] ns: integrate accessibility with EmacsView and redisplay
Subject: [PATCH 5/8] ns: integrate accessibility with EmacsView and redisplay
Wire the accessibility infrastructure into EmacsView and the
@@ -51,7 +51,7 @@ index 80661a9..2b1f9e6 100644
** Re-introduced dictation, lost in Emacs v30 (macOS).
We lost macOS dictation in v30 when migrating to NSTextInputClient.
diff --git a/src/nsterm.m b/src/nsterm.m
index 1a21f2e..f8b7a8d 100644
index 98ff027..0a70f3e 100644
--- a/src/nsterm.m
+++ b/src/nsterm.m
@@ -1258,7 +1258,7 @@ If a completion candidate is selected (overlay or child frame),
@@ -157,7 +157,7 @@ index 1a21f2e..f8b7a8d 100644
static BOOL
ns_ax_find_completion_overlay_range (struct buffer *b, ptrdiff_t point,
ptrdiff_t *out_start,
@@ -8719,7 +8769,6 @@ - (NSRect)accessibilityFrame
@@ -8727,7 +8777,6 @@ - (NSRect)accessibilityFrame
@end
@@ -165,7 +165,7 @@ index 1a21f2e..f8b7a8d 100644
/* ===================================================================
EmacsAccessibilityBuffer (Notifications) — AX event dispatch
@@ -9264,7 +9313,6 @@ - (NSRect)accessibilityFrame
@@ -9272,7 +9321,6 @@ - (NSRect)accessibilityFrame
@end
@@ -173,7 +173,7 @@ index 1a21f2e..f8b7a8d 100644
/* ===================================================================
EmacsAccessibilityInteractiveSpan — helpers and implementation
=================================================================== */
@@ -9594,6 +9642,7 @@ - (void)dealloc
@@ -9602,6 +9650,7 @@ - (void)dealloc
[layer release];
#endif
@@ -181,7 +181,7 @@ index 1a21f2e..f8b7a8d 100644
[[self menu] release];
[super dealloc];
}
@@ -10942,6 +10991,32 @@ - (void)windowDidBecomeKey /* for direct calls */
@@ -10950,6 +10999,32 @@ - (void)windowDidBecomeKey /* for direct calls */
XSETFRAME (event.frame_or_window, emacsframe);
kbd_buffer_store_event (&event);
ns_send_appdefined (-1); // Kick main loop
@@ -214,7 +214,7 @@ index 1a21f2e..f8b7a8d 100644
}
@@ -12179,6 +12254,332 @@ - (int) fullscreenState
@@ -12187,6 +12262,332 @@ - (int) fullscreenState
return fs_state;
}
@@ -547,7 +547,7 @@ index 1a21f2e..f8b7a8d 100644
@end /* EmacsView */
@@ -14179,12 +14580,17 @@ Nil means use fullscreen the old (< 10.7) way. The old way works better with
@@ -14187,12 +14588,17 @@ Nil means use fullscreen the old (< 10.7) way. The old way works better with
ns_use_srgb_colorspace = YES;
DEFVAR_BOOL ("ns-accessibility-enabled", ns_accessibility_enabled,