Skip to content

Speed up cursor column calculation in the status bar for long lines - #761

Open
KAMI911 wants to merge 1 commit into
linuxmint:masterfrom
KAMI911:build/perf/cursor-position-status-bar
Open

KAMI911 wants to merge 1 commit into
linuxmint:masterfrom
KAMI911:build/perf/cursor-position-status-bar

Conversation

@KAMI911

@KAMI911 KAMI911 commented Oct 3, 2026

Copy link
Copy Markdown

Motivation

The status bar position indicator computed the column number by stepping through a GtkTextIter one character at a time from the start of the current line to the cursor (gtk_text_iter_get_char + gtk_text_iter_forward_char + gtk_text_iter_equal on every step). This runs on essentially every cursor movement, including while typing, so on a very long line — a minified script or a large single-line JSON file — it added a noticeable delay on each keystroke, since the cost scales with the cursor's column position.

The loop now fetches the text between the start of the line and the cursor with a single gtk_text_iter_get_slice() call and walks the resulting plain UTF-8 string instead, avoiding the repeated iterator validation and buffer lookups of the old approach, while producing the exact same column number, including the same tab-expansion handling.

Result

Verified with a standalone GtkTextBuffer test harness comparing the old and new implementations side by side:

  • Correctness: identical output on 8 cases (empty line, plain ASCII, a single tab, a tab mid-line, multiple tabs, multibyte UTF-8, mixed tabs + UTF-8, tabs not on a tab-stop boundary).
  • Performance: on a synthetic 300,000-character line, the old per-iterator loop took 7.41 ms, the new slice-based walk took 1.27 ms — about 5.8× faster — while returning the same column number.

The status bar position indicator computed the column number by stepping through a GtkTextIter one character at a time from the start of the current line to the cursor, calling gtk_text_iter_get_char and gtk_text_iter_forward_char in a loop. This function runs on essentially every cursor movement, including while typing, so on a very long line, such as a minified script or a large single-line JSON file, it added a noticeable delay on each keystroke because the cost scaled with the cursor's column position. The loop now fetches the text between the start of the line and the cursor with a single gtk_text_iter_get_slice call and walks the resulting plain UTF-8 string instead, which avoids the repeated iterator validation and buffer lookups of the old approach while producing the exact same column number, including the same tab-expansion handling.
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