Repository navigation
[shaping] reuse the break attributes of a paragraph across line wrappings - #304
Merged
Merged
Conversation
Iterators never modify the Segmenter, so any number of them can be created from one initialized Segmenter, which lets callers keep the break attributes of a text and segment it again without recomputing them. Len returns the number of runes the Segmenter was initialized with. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Prepare computes the UAX go-text#14 and go-text#29 break attributes of the whole paragraph every time, although they only depend on the text. A layout typically wraps the same paragraph several times, to an estimated width and then the allocated one, and again whenever the width changes. PrepareSegmented takes a segmenter.Segmenter already initialized with the paragraph, which the caller can keep with its shaped runs. Prepare is now Init followed by PrepareSegmented, so it behaves as before. BenchmarkWrappingWidths wraps a paragraph to three widths: 3x faster with PrepareSegmented for 100 and 1000 runes of Latin and Arabic text. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
WrapParagraphF initialized the segmenter twice with the same paragraph: once to look for mandatory breaks before taking the single run fast path, and again in Prepare. The wrapping now reuses the first one. BenchmarkWrapping: 25-37% faster for 100 and 1000 runes (geomean of all cases -29%). Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
rcoreilly
requested review from
andydotxyz,
benoitkugler and
whereswaldon
as code owners
October 21, 2026 16:36
whereswaldon
left a comment
Member
There was a problem hiding this comment.
Thank you for this optimization. It certainly has a large impact if you can afford to keep the segmenter around. My only concerns are minor documentation fixes.
whereswaldon
approved these changes
Nov 10, 2026
Member
|
@benoitkugler @andydotxyz Can a second maintainer review this? I think it's ready. |
benoitkugler
approved these changes
Nov 10, 2026
benoitkugler
left a comment
Contributor
There was a problem hiding this comment.
That looks good to me, nice optimization !
Contributor
|
@whereswaldon not sure whether squash or rebase is the best strategy here. |
Member
|
I prefer rebase since the commits are logically distinct |
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.
LineWrapper.PrepareandWrapParagraphrunSegmenter.Initon the paragraph every time, which computes the UAX #14 line breaks and UAX #29 grapheme and word breaks of every rune. These attributes depend only on the text. A GUI layout usually wraps the same paragraph several times, though: once to an estimated width and once to the allocated one, and again whenever the window width changes. In Cogent Core, finding the break attributes was about half the cost of wrapping a paragraph that had already been shaped. On the web build it also took about as much time as the rest of the wrapper.This PR adds
LineWrapper.PrepareSegmented(config, seg, runs). It takes asegmenter.Segmenterthat has already been initialized with the paragraph, so a caller can keep the segmenter next to its shaped runs and reuse it for every wrapping.Prepareis nowseg.Init(paragraph)followed byPrepareSegmented, so it behaves exactly as before. The wrapper only reads the segmenter. The segmenter must not be initialized again until the nextPrepare,PrepareSegmentedorWrapParagraph, as the doc comment says.To support this, the segmenter package now documents something that was already true: iterators never modify a
Segmenter, so any number of them can be created from one initializedSegmenter. An iterator and the slices it returns stay valid until the segmenter is initialized again. A newSegmenter.Lenreturns the number of runes of the paragraph, which the wrapper needs and previously took from the paragraph argument.A third commit fixes the same kind of waste inside
WrapParagraphF. It initialized the segmenter once to look for mandatory breaks before taking the single-run fast path, and then a second time inPrepare. It now reuses the first one.Tests:
TestPrepareSegmentedwraps three texts with a freshPrepareeach time and with one segmenter shared by all the wrappings, and checks that the lines are identical. The texts are Latin, Latin with\n\n,\r\nand U+2028 mandatory breaks, and Arabic RTL. Each is cut into 1 and 7 runs and wrapped with 8 configs (break policies, no trailing whitespace trim, truncation after 1-3 lines with and without truncator andTextContinues) at 66 widths from 0 to 1495, either constant or alternating between two widths line by line. A mutation check confirms the test fails when the segmenter is given a different text.TestWrapParagraphSegmentedInitchecks thatWrapParagraphgives the same lines and truncation asPrepareplusWrapNextLine, covering both the fast path and wrapping.TestIteratorsReuse(segmenter) creates line, grapheme and word iterators repeatedly, interleaved and from four goroutines, and checks they all give the same segments. It passes under-race.Benchmarks:
BenchmarkWrappingWidths(new) wraps a paragraph cut into 10 runs to widths 150, 300 and 600:The existing
BenchmarkWrapping(WrapParagraph, main vs this branch, n=6) is 25-37% faster for 100 and 1000 runes, for example 1000 runes Latin in 1 part goes from 94.7 µs to 59.7 µs. For 10 runes in 10 parts it is 25-29% faster. The geomean over all cases is -29%. The 10-rune single-run cases take the fast path, which does the same work as before, and are unchanged (519 vs 519 ns in an interleaved rerun). Allocations are unchanged.go test ./...,go vet ./...and staticcheck v0.8.0 (as in CI, with Go 1.26) pass. The shaping, segmenter and fontscan tests also pass with Go 1.22.12.🤖 Generated with Claude Code