Repository navigation
perf(layout): open long documents on their first screens, lay out the rest in the background - #166
Merged
Merged
Conversation
… rest in the background A 2.5 MB file spent 285 ms laying out all 14,100 blocks before the first frame. Open now lays out the first 500 rows (about 4 ms) and hands the remaining blocks to a background thread that splits them across cores. Input, annotating, and any view past the ready rows wait for it, so G, paging, block counts, the rail and saved annotations see the whole document. Reflow on resize runs across cores too. --bench reports the first-screens time and the background time.
…n a layout panic A reload restarts the layout from the top. A resize before the rest arrived clamped the scroll to the rows ready so far, throwing a view at the end of a long document back near the top. Scroll clamping now waits for the real end, and a frame whose view, cursor, selection or pending range lies past the ready rows waits for the rest first. A panic on a layout thread ran ratatui's panic hook there, restoring the terminal while the main thread kept drawing, and the join then laid the same blocks out again. Layout threads now only note their panic; the waiting thread panics with that message, so the hook runs once, on the main thread. The event loop merges a finished background layout as soon as it is ready. An empty document's footer said "block 1/0"; it now says "empty document".
backnotprop
force-pushed
the
feat/fast-open
branch
from
November 20, 2026 01:23
3b91761 to
6143f06
Compare
This was referenced Nov 20, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Cause
On
samples/big.md(2.5 MB, 14,100 blocks),Open::newcalledDocLayout::build, which rendered, aligned and wrapped every block before the first frame. Timing spans in the release binary (120x40, private tmux):Document::of(parse/split)DocLayout::buildsrcmap::align+ decorateStore::load(no saved annotations)terminal.drawThe first draw's width matched the build width (81 at 120 columns), so there was no extra reflow at 120 columns. In folder mode or with
[ui] doc_widththere was one: another 60 ms.Fix
DocLayout::startlays out blocks from the top until 500 rows are ready (113 blocks, ~4 ms), then hands the remaining blocks to one background thread. That thread splits the work across cores withstd::thread::scope(layout/background.rs).Rows that are already laid out never move when the rest arrives: a block's rows depend only on that block, and
first_rowis assigned in order. A test checks that a lazy layout, reflowed before it finished, gives exactly the same rows (text and cell offsets) as a full build.The layout waits for the background result only where something needs the whole document:
App::handle_event: any key or mouse event, soG, paging, clicks and selection see every block.App::draw: only when the view reaches past the ready rows (finish_through).annotate(keys, Pi notes,--annotate) and the headless helpers (--blocks,--snapshot).When the result is already in, merging it is just appending the blocks. A first key pressed within ~40 ms of opening waits for the rest of that time.
reflow(resize) runs across cores. On a layout that is not complete yet, it rewraps only the blocks that are ready, and the background blocks are rewrapped when they arrive. A first frame at a different width (folder tree,doc_width) therefore stays cheap.DocLayout::buildstays the eager path for bench and tests, now parallel too.--benchalso prints the first-screens time and the time until the background finishes.Changed functions:
DocLayout::{build, reflow}, newDocLayout::{start, finish, finish_through, poll, is_complete, place}, newRenderedBlock::{new, wrap}(the old per-block bodies ofbuildandreflow),Open::new,App::{reload, annotate, describe_blocks, scroll_for_snapshot, select_quote_for_snapshot, scroll_by}, newApp::{settle_layout_for_view, poll_layout, layout_pending}, one line at the top ofApp::handle_event, one line inApp::draw, the footer's block count inApp::draw_footer,cli::{bench, with_terminal, event_loop}.Documentnow derivesClone. No new dependencies.Numbers
Real binary in a private tmux at 120x40, scratch
HOMEandPLANNOTATOR_DATA_DIR. Time fromnew-sessionuntil the first screen text shows, polled withcapture-pane. Medians of 5 runs, in two interleaved rounds. The first run after each fresh build is slow on macOS, about 0.3 to 0.6 s for every binary, and is left out.samples/folder mode, big.md firstInteractions on big.md. Time from the tmux action until the screen equals a reference capture; medians of 4:
Gafter 1 sGright at the first screen (waits for the background)* On main the first screen only appears after the full layout, so
Gis already free by then; the 0.3 s was paid before.--bench samples/big.md: first screens 4 ms; rest in background 40–47 ms; full build 34–40 ms (was 285); reflow 11–18 ms per width (was 53–64). Rows (57,515) and mapped cells (2,380,068 of 2,586,780) are identical to main.Tests
layout::tests::a_long_document_lays_out_its_first_screens_first_and_the_rest_matches_a_full_build: the regression guard. Opening lays out under a tenth of the blocks, and after a reflow plusfinishthe rows equal a full build.app::long_doc_tests::a_long_document_opens_lazily_and_g_and_saved_annotations_reach_its_end: 3,000 blocks with a saved annotation near the end. The reopened app has not laid out every block.Gselects the last block, and the screen shows the last paragraph,block 3000/3000and the restored annotation's bubble.cargo fmt --check,cargo clippy --locked --workspace --all-targets -- -D warningsandcargo test --locked --workspacepass locally on macOS (rustc 1.96). Windows and musl are left to CI: the change uses onlystd::thread,Arcandavailable_parallelism.Note: this app has no scrollbar or percentage today. The footer's
block N/Mcomes from the parsed document, which is complete at open.Review fixes (second commit)
reflow, thenscroll_by(0), which clamped the scroll to the ~500 ready rows.scroll_bynow waits for the real end before it clamps.settle_layout_for_viewwaits for the rest when the view, the cursor, a pending selection or the selected block lies past the ready rows.total_rowsand the block list:input.rs(the cursor and click clamps) andpage_by/select_blockall run insidehandle_event, which already waits for the whole layout.G, thenowith anEDITORthat appends and sleeps, and a resize to 100 columns during the edit. The old head shows "Paragraph 231…249"; main and this commit show "Paragraph 14981…14999", footerblock 15000/15001.rwith a resize at the same moment: 3/3 at the end.runtil the "reloaded" status shows (median of 4): main 0.55 s at the top and 0.49 s at the end; this PR 0.05 s and 0.06 s.layout/worker_panic.rs; installed afterratatui::init). The waiting thread then panics with that message, so ratatui's hook runs once, on the main thread: the terminal is restored and the message is printed after it. The blocks are not laid out a second time.each_in_paralleljoins its workers itself, soscopedoes not panic a second time. The event loop merges a finished background layout every iteration (25 ms poll while one is pending), so a panic ends the app even when no key is pressed.layout thread panicked at …: injected layout panic), exit 101, terminal back in cooked mode.block 1/0. It now saysempty document.New tests:
a_reload_deep_in_a_long_document_keeps_the_view_through_a_resize: after a reload, the first frame at the same and at a narrower width equals the frame of a fully laid-out reload. It fails without the fix.a_panic_on_the_layout_thread_continues_on_the_waiting_threada_panicking_worker_ends_each_in_parallel_with_its_own_panican_empty_document_says_so_instead_of_counting_blocksThe app tests moved to
app/doc_size_tests.rs. Rebased on main after #165 and #167. First screen on big.md after the rebase, on a loaded machine (load average ~40): 0.057–0.065 s against main's 0.33 s.