Skip to content

Cut launch-time layout on long-line files (#106) - #107

Open
RobertoMachorro wants to merge 4 commits into
masterfrom
gutter-optimizations
Open

RobertoMachorro wants to merge 4 commits into
masterfrom
gutter-optimizations

Conversation

@RobertoMachorro

@RobertoMachorro RobertoMachorro commented Oct 31, 2026 •

Copy link
Copy Markdown
Owner

Refs #106 and #102. This removes the stack in the reporter's log and cuts Moped's own share of launch time on files with very long lines. It does not make a multi-megabyte single-line file open fast, because most of that time is now AppKit's own layout.

What changed

  • Line-number gutter. It used to run ensureLayout(forBoundingRect:) inside its own draw. That re-tiled the scroll view during the Core Animation commit, which is the stack in both issues' logs. Layout now happens in viewWillDraw(), and the draw path only reads existing layout.
    • viewWillDraw() lays out again if laying out moved the clip view, so the draw never numbers a band it didn't lay out.
    • The gutter is now sized when text is set, not one run-loop turn after its first draw. With wrapping on, that late resize changed the text view's width and a wrapped file was laid out twice.
  • Current-line highlight. It measured the caret's whole line with boundingRect(forGlyphRange:). TextKit 1 lays out a whole paragraph at a time, so on a one-line file this typeset the entire file on every caret move. It also did so once while the file loaded, before the view had its final width, so that work was thrown away and done again.
    • On caret moves it now only reads existing layout. It lays out only in drawBackground, where AppKit is about to lay out the same area anyway.
    • Visible change: on the last line of a file that ends in a newline, the highlight no longer spills onto the empty line below.
  • Status bar. updateCursorPosition counted line:column up to the caret before checking whether to defer. While a file loads, the caret briefly sits at the end of the text, so the count scanned the whole file (about 0.9 s at 15 MB) and then discarded the result. The check now comes first.
  • Shift at launch skips Reopen Previous. This is an escape hatch for a document that hangs on open. It's documented in DOCUMENTATION.md.

Measurements

Release builds, wrapping on, 20 s runs. Busy means CPU above 50%.

File Before After
5 MB line, gutter on ~11 s (3 runs) ~5.75 s (3 runs)
5 MB line, gutter off ~6 s ~4.25 s
2,000 short lines + one 5 MB line, gutter on ~11.5 s ~12.5 s (no change; gutter off is ~6.5 s)
15 MB one-line JSON pinned all 20 s still ~17 s, in AppKit's _ensureLayoutCompleteForVisibleRect

Testing

  • swiftlint --strict: 0 violations
  • swift test --package-path MopedEditor: 235 tests, 0 failures, none skipped
  • ./scripts/check_localized_strings.sh: passes
  • New tests:
    • LineNumberRulerTests.testLayoutHappensBeforeTheDrawAndNotInsideIt
    • CurrentLineHighlightTests, which covers the band shape through a real draw, checks that a caret move lays nothing out, and checks that it measures the line it arrives on
    • LineNumberRulerTests.testSettingTextSizesTheGutterRightAway
    • Each fails when the old behaviour is put back.

Not verified

  • On the 2,000-line variant the gutter still roughly doubles launch time. The time is in AppKit's NSScrollView tile → _setFrameSize:forceScroll: re-wrap, and the cause isn't known.
  • Holding Shift at a real launch.
  • The gutter and highlight in the running app, by eye.
  • The status bar change. There's no app test target, so it's covered only by the measurement.

🤖 Generated with Claude Code

- Line-number gutter: lay out in viewWillDraw() instead of inside its
  own draw, which is where the #106 and #102 launch hangs were stuck.
- Current-line highlight: only read existing layout on caret moves, and
  lay out only in drawBackground. TextKit 1 lays out a whole paragraph
  at a time, so measuring the caret's line typeset an entire one-line
  file on every caret move and once more while loading it.
- Status bar: defer the line:column count during view updates before
  counting, not after, so loading no longer scans the whole file.
- Holding Shift at launch skips Reopen Previous.
- Size the line-number gutter when text is set instead of a turn after
  its first draw. The late resize changed the text view's width, so a
  wrapped file was laid out twice; on the 5 MB-line test file, gutter-on
  launch drops from ~8.5 s to ~5.75 s busy.
- viewWillDraw lays out again if laying out moved the clip view, so the
  draw never numbers a band it did not lay out.
- caretLineRect no longer takes a layOut flag: drawBackground lays out
  itself, and only over the visible rect when the dirty rect touches it.
- Share the view-to-container rect translation between the gutter and
  the caret highlight.
- Tests: a caret move measures the line it arrives on; setting text sizes
  the gutter right away; the no-resize-mid-draw test still exercises a
  stale gutter.
- A resize queued by an edit now measures the width when its turn comes,
  so it can no longer put back an older document's width after
  invalidateLineNumbers() sized the gutter for new text.
- Size the gutter immediately only before the editor is in a window; a
  reload during a SwiftUI view update waits a turn like any other resize.
- showsLineNumberGutter and editorFont skip unchanged values, so a
  preference change no longer recounts (and re-attributes) every open
  document.
- The caret band treats a dirty rect that only touches the visible rect
  as adjacent, so an overdraw strip no longer looks cut off.
- containerRect(forViewRect:) uses textContainerOrigin on both axes.
- Reword the caret-move comment as an assumption, not a verified fact.
- Tests: a queued resize does not undo a newer size; a font change sizes
  the gutter right away.
Copilot AI balanced review requested due to automatic review settings November 3, 2026 03:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The current-line highlight can extend vertically into blank space or other rows for horizontally clipped, unwrapped lines; both extensions need to be restricted to wrapped lines and covered by a regression test.

1 open issue
What changed in this PR

This PR reduces Moped’s launch-time layout work for long-line files, addressing paths implicated in #106 and #102 without claiming to eliminate AppKit’s remaining layout cost.

Changes:

  • Moves gutter layout before drawing and sizes the gutter earlier to avoid redundant wrapped-text layout.
  • Measures current-line highlights from existing layout and defers status-bar counting during view updates.
  • Adds Shift-at-launch reopening bypass, regression tests, and documentation.
File Description
MopedEditor/​Tests/​MopedEditorTests/​LineNumberRulerTests.swift Tests layout timing, immediate gutter sizing, and queued resize handling.
MopedEditor/​Tests/​MopedEditorTests/​CurrentLineHighlightTests.swift Tests highlight geometry and caret moves without forced layout.
MopedEditor/​Sources/​MopedEditor/​NSTextView+ContainerRect.swift Adds view-to-text-container coordinate conversion.
MopedEditor/​Sources/​MopedEditor/​MopedTextView+CurrentLine.swift Computes highlight bands from existing layout.
MopedEditor/​Sources/​MopedEditor/​MopedTextView.swift Skips redundant gutter and font assignments.
MopedEditor/​Sources/​MopedEditor/​LineNumberRulerView.swift Moves layout before drawing and updates gutter sizing.
Moped/​EditorState.swift Defers cursor counting before scanning text during view updates.
Moped/​AppDelegate.swift Allows Shift at launch to bypass reopening documents.
DOCUMENTATION.md Documents the Shift-launch recovery option.
CHANGELOG.md Records launch optimizations and visible behavior changes.

Review effort: Balanced

A review asked whether a horizontally clipped, unwrapped line could make
the band stretch over neighbouring rows. It cannot: the glyph query
returns whole line fragments regardless of the rect's x and width, so the
caret line's shown range is only ever cut short vertically. Add a
regression test for the three unwrapped cases (clipped end, scrolled
right on the long line, scrolled right past a short line) and a comment
at the query saying why.

This branch has not been deployed

No deployments
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.

2 participants