Repository navigation
Fix paragraphs that do not wrap: give each hyphenation point its own lb_info - #4744
Conversation
…ts own lb_info c87f087 ([6018], "atomic path array index") moved the per-position records of atomic break positions into an array and routed all lookups through get_lb_info. In doing so it replaced if (!best->contains (new_pos)) best (new_pos)= lb_info (); lb_info cur= best[new_pos]; by `get_lb_info (new_pos)`, which returns `best (pos)` for non-atomic paths. lb_info is a reference-counted handle and a hashmap creates a new entry by copying its default value, so every hyphenation point of a paragraph then shared one (prev, pen, pen_spc) record. The optimal-break chain got mixed up and a paragraph could come out as one overfull line that only breaks at a hyphen point near its end. Allocate a fresh lb_info for each new non-atomic position in test_better and read with best[pos] again, as before c87f087. Adds three regression tests that use real fonts and hyphenation patterns (a minimal English paragraph, a longer one, German compounds). Each sweeps 101 line widths and checks monotonic breaks, line count and line width. All three fail without the fix and pass with it. Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
|
Thank you for this excellent PR, @guraltsev! 🙏 The root-cause analysis is outstanding — the shared We verified everything locally (macOS arm64):
One housekeeping note: the Thanks again for the careful work, including the performance verification — it's much appreciated! |
This PR fixes #4743
Summary
A two-file change: a fix in
src/Typeset/Line/line_breaker.cpp(4 lines added, 1 removed), and three regressiontests in
tests/Typeset/Line/line_breaker_test.cpp(129 lines added, 2 removed). All three tests fail onupstream/mainand pass with the fix. A minimal reproduction document and its PDF exportsbefore and after are attached (see "Reproduction files").
What happened
Some ordinary paragraphs were not wrapped: the whole paragraph came out as one line wider than
the page, with a break only at a hyphenation point near its end (
bound-/aries). It dependson the exact text and line width. In a 93-page paper, paragraphs ended up outside the page.
Cause, and how it was introduced
c87f08744(PR #4711, [6018], "optimise the line-breaking algorithm: introduce an array indexfor atomic paths and avoid useless hyphenation of single characters") is a performance change.
Its description says line breaking was a main bottleneck when typesetting large documents: almost
every break position is an atomic path
path (i), and each test of a break cost heap allocationsof one-element list nodes and up to four hash-map reads/writes on the same key. On a document
with 89,000 paragraphs it reports
line_breaksfalling from 14.6 s to 3.6 s. It did two things:[17]) now livein an array,
atom_best, instead of the path-keyed hash map. Only hyphenation points(compound paths such as
[30, 4]) stay in the mapbest. The places that read or update aposition's record were changed to go through a new helper,
get_lb_info.can_hyphenatestops the breaker from generating hyphenation candidates inside asingle CJK character, whose internal form
<#4E00>is longer than the oldN (s) > 4limit.Before the commit,
test_bettercreated the record of a new position explicitly:The helper replaced both lines by
return best (pos)for non-atomic paths. That looks the same,but
best (pos)(non-constoperator()) creates a missing entry by copying the map's defaultvalue, and
lb_infois a reference-counted handle (CONCRETE), so the copy shares the defaultobject. After the commit every hyphenation position of a paragraph shares one record:
updating the best break of one hyphen point changed all of them, and the chain of breaks
followed
previnto a position that had never been proposed from there. Atomic positions arein
atom_best, an array of distinct objects, and were not affected.Neither the PR description nor the commit message discusses the dropped guard, and the PR has no
comments or reviews, so why it was dropped is a reading of the diff, not the author's stated
intent. The most likely reason is that the repeated hash-map accesses in
test_better(thecontains, the assignment and the read) were exactly what the PR set out to remove, and thecontainscheck was folded away together with them. For atomic positions that is correct, sinceatom_bestis pre-allocated. For hyphenation positions the check was doing real work.This change keeps the optimisation: atomic positions still use the array, with no hash-map
access. Only the first proposal for a hyphenation point pays one
containsand one assignment,as it did before #4711.
Why it was not noticed:
without a language, or CJK mock boxes for which
can_hyphenateis false.one, which depends on the exact text and width. Most paragraphs lay out normally.
Reproduction files (attach to the PR)
repro.tmu: the minimal document. One paragraph of plain text, stylegeneric, no math,no references,
page-medium = paper.repro-before.pdf:MoganSTEM -headless -c repro.tmuonupstream/main(c68a494fc). Thefirst line runs past the right margin and is clipped; only
ariesis on the second line.repro-after.pdf: the same command with this change. Three lines, wrapped at the margin(
... bound-/aries).Change
src/Typeset/Line/line_breaker.cpp:test_betterallocates a freshlb_infofor a position that is not atomic (or is out of theatomic range) and not yet in
best.get_lb_inforeads withbest[pos]again (no insertion on read), exactly the behaviour beforec87f08744. The atomic-path array andcan_hyphenatefrom that commit are unchanged.tests/Typeset/Line/line_breaker_test.cpp: three regression tests, described next.Speed
The fix must not give back what #4711 gained, so it was measured two ways. A generated
lorem-ipsum document with 89,000 paragraphs (30-130 words each, a few very long words) was used.
All builds are
releasedbgon the same machine, with their own settings and cache folders.Typesetting inside the application (Mogan's own benchmark line
typeset: br->typeset,headless PDF export, one run each):
br->typesetupstream/main(with [6018] 优化段落折行算法:引入原子路径数组索引并避免单个字符无效断字 #4711, without this fix)upstream/main+ this fixThe fixed build lays out 48 % more pages, because the unfixed build loses about 30 % of the
text off the page (see below), for 4 % more typesetting time.
line_breaksalone (a temporary benchmark, not part of this PR, callingline_breakson20,000 paragraphs built from real
text_boxitems; 3 runs each):upstream/main41.7-41.9 s,with this fix 40.2-41.5 s, so no measurable difference per call. The unfixed code returns
76,692 lines in total where the fixed code returns 174,392 for the same input: the unfixed
breaker collapses most paragraphs to too few lines.
Whole-document correctness on the same 89,000-paragraph document: the text extracted from the
PDF of the fixed build is identical (735,654 lines) to that of the build just before #4711
(
c87f08744^), while the unfixedupstream/mainyields 520,697 lines and 8,196 pages instead of12,104.
Regression tests
tests/Typeset/Line/line_breaker_test.cpp: three new tests that use realtext_boxitems withthe roman font and the real hyphenation patterns, as in the editor. A shared helper,
check_paragraph_wraps, sets up the paragraph, sweeps the line width over 101 values(paragraph width divided by 1.50 ... 4.50) and requires, for every width:
path (0), end atpath (n)and are monotonic;than
large_width.test_hyphenation_keeps_lines_within_widthtest_hyphenation_long_paragraphtest_hyphenation_german_compoundsThe monotonic-break and line-count checks are an extra net; the width check
is what detects the bug.
Verified (Windows 11,
releasedbg)the file; the 4 existing tests still pass). Without the change, the first failures are at
k=207(a line 1.40 times the limit),k=324(2.06 times) andk=264(1.36 times) for thethree tests;
kis 100 x paragraph width / line width.repro.tmu, headless PDF export): before, one 124-character firstline and a lone
aries; after,... along bound-/arieson proper lines.page before is now on the page; the document grows from 93 to 94 pages.
paragraph export byte-identical text before and after. The change does not touch any
direction logic: line breaking works on the logical item list, and
src/Typesetcontains nobidi code.
Not checked
(250 s, dominated by writing the PDF); that build has no
typeset:benchmark lines.uses the same typesetter).
pixel by pixel.
on the language).
romanat 10 pt, 600 dpi. Widths in other fonts differ, but the property testeddoes not depend on the font.
of the same size was used instead (see "Speed"), so the absolute numbers are not comparable
with those in [6018] 优化段落折行算法:引入原子路径数组索引并避免单个字符无效断字 #4711.
How this fix was made
Developed through human-aided debugging, code written using Claude Sonnet 5.5, reviewed and
tested by the user.
repro-after.pdf
repro-before.pdf
repro.tmu