Repository navigation
feat(layout): add one shared continuation-limit mechanism - #690
Merged
Merged
Conversation
The document now addresses each layout context to the renderer it calls and holds a per-placement session. A renderer whose new layout can differ from 2.3.3 reports an upper bound on its continuations; the document decides between the new layout and its twin at the first reporting call, probing layout-only when the bound passes the limit, and keeps the verdict per start. A renderer laid out under any context the document did not address to it (a caller's wrapper, WithArea, the public constructor) uses its twin. No renderer reports yet, so no output changes. Everything added is internal except prose on LayoutContext, IRenderer.Layout and Document.Add. The pieces that exist only to keep the 2.3.3 verdict are marked for removal in 3.0 (#688).
The shared mechanism now costs a document almost nothing until a renderer reports, and the decision is exact in more places. - The public XML-doc paragraphs on IRenderer.Layout, the LayoutContext constructor, WithArea and Document.Add(IRenderer) are gone. They described a rule no renderer applies yet, and an adopting change adds the sentence for its own renderer. - A verdict is dropped whenever a plan-dependent element resolves to a different text, because that changes the layout and not the start the verdict is kept under. - The twin is chosen when the new layout throws something other than the limit, so a document that 2.3.3 saved still saves. - The document reuses one session for its own placements, a context carries a single reference, and the loop reads one flag after a call. A built-in parent gets a view of the session through Address, and Addressed does the same for a re-layout in Draw. Address carries the fresh-page area and takes the page-top and line-count flags. - A call in which a child reported under a built-in parent and the parent did not is counted, and fails a Debug build. - Every compatibility device now names 3.0 and #688. Tests: a scripted renderer whose layouts differ in what they place, a start-dependent count, a plan-dependent element, two reporting elements at one start, a retry whose twin is the one that counts, and a parent that adopts the mechanism and one that does not.
…he loop (#686) A Full piece returns from the loop, so setting the mode there is never read.
…decided placements (#686) IsPlacedAsTwin was true whenever a placement's mode was Twin, which includes a placement the first-piece rule fixed with no decision. It now reads a flag that only a committed Twin decision sets and Reset clears, so it is true on every call of a decided placement, overflow pieces included, and false on a placement that never reported. A twin that throws, as well as one that reaches the limit, now chooses the twin, so the placement raises the twin's exception where 2.3.3 refused. Below its bound an adopter's new layout must refuse and save exactly where its twin does; the comments and tests state that. ContinuationMode now defaults to Twin, so a kept-mode field that was never set cannot select the new layout. Added comment lines are rewrapped to 100 columns, and tests assert that a child of a twin parent gets no session.
…ent (#686) The session is reused for each element, so a Reset that left the mark set would tell the next element's renderer that a decision chose its twin. The new test places an element whose decision chooses the twin and then one that never reports, and asserts none of the second's calls says so.
The loop offers the call after a retry the page-top box, which can be one rounding step taller than the box the retry got. A probe that started there could answer Clean for a layout whose retry had been refused, so a twin that fits only the taller box made a document save where 2.3.3 refuses, and a new layout that fits only the taller box made one refuse where 2.3.3 saves. A report from a retry that answered Nothing now chooses the twin without a probe, since the loop is about to refuse it and the twin decides that refusal. The twin probe calls the twin once on the retry's box first and counts a Nothing there as a fault. The memo key records the retry site, so a verdict from there is not reused by the loop. The tests use the geometry that gives the two boxes, and one more asserts that a placement decided for the new layout is not marked as a chosen twin.
It is false on the calls that precede the decision, the reporting call included, and true on every call the document makes after the decision chose the twin.
With the retry site in the key, the old count test no longer told a dropped count from a kept one: its two starts differed by site as well. The new pair uses a renderer that reports at a chosen box height, so two starts share the box and differ only by the count in one test and only by the site in the other.
The comment said the probes start at the retry's own box. Only the twin probe does, by one extra call; the new layout is probed after a retry that fit, from the call the real placement makes next.
This was referenced Nov 22, 2026
Tim81
added a commit
that referenced
this pull request
Nov 23, 2026
A table cell split its text on U+0020 only, so a tab or a line feed reached the font: glyph 0 in an embedded font, which fails PDF/A-2u clause 6.2.11.8, or a raw control byte in a Standard 14 font. A running band had the same fault for a form feed. A cell now reads CR LF, CR and LF as line ends and every other white-space control character as one space; an automatic column takes its width from its widest line. A running band, and the text a page label prints, draw those characters as spaces. Where reading a cell's breaks would leave a header row or a RowSpan group no room on a page, those rows keep the unbroken layout 2.3.3 measured, with the control characters left out of the drawing. A table that is refused when every cell is measured as one line stays refused. A table adopts the shared continuation-limit mechanism of #690: it reports a bound over both of its layouts, and when its breaks would pass the limit it is laid out unbroken and reported (301). A table that a caller's renderer holds, or that is laid out under WithArea or the public constructor, keeps main's layout, pages and refusals, and leaves the control characters out of the drawing. The decision keeps what each cell measures (widths per cell, line counts per width interval, the grid across pages), so the costly shapes run much faster than before: 20,000 rows of four line-break cells allocate 1.8 GB where main allocated 94 GB. The 2.x compatibility paths are marked for removal in 3.0 (#688). Under StrictDiagnostics a control white-space character in a cell or a band no longer reports NotdefGlyph or UnencodableCharacter, so a document refused only for those reports now saves; the output passes veraPDF 2u. PaginationDepthTests now counts PlaceLoop frames and PlaceLoop is kept out of line, because the probes read 0 on main when the whole suite ran in sequence. Closes #611
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.
Closes #686
One shared internal mechanism that keeps the continuation limit's verdict for an element whose layout differs from 2.3.3. It lands with no output change, because no renderer reports to it yet. The list fixes, the table fixes and the paragraph word split adopt it in their own PRs.
What it does
DocumentRenderer.CallLayoutmakes aLayoutContextthat carries one reference, the placement'sContinuationSession, and records the instance it calls insession.Called. It does this in both passes and in every keep simulation and probe.WithAreaand the public constructor leave the reference unset.LayoutContext.ModeFor(renderer)returns the document's mode whenrendereris the instance called andTwinunder any other context.IsPlacedAsTwin(renderer)is true when the document called that instance outside a probe in a placement where a decision committed the twin. The session carries that as a flag thatSettleCallsets andResetclears, so it is true on every call the document makes after the decision chose the twin, the twin's overflow pieces included, and false on the calls before the decision and where the mode became Twin only because the first piece was placed with no report. That is how a twin instance learns the document chose it.session.Report(this, bound)with an upper bound on the continuations of the rest of its placement. The bound must cover both layouts: a bound that holds for the twin only lets the new layout reach the limit with no probe. Contract: a call that made no report returns the twin's result. A report from an instance the document did not call, after the mode is decided, or inside a probe is dropped. Where the bound is within what is left of the limit (bound <= L', defined below), no probe runs, whatever the element's real count, and the document uses the new layout, except that a report from a retry that answered Nothing chooses the twin. An adopter must guarantee, wherebound <= L', that its new layout refuses exactly where its twin refuses, with the twin's exception type, and never refuses where its twin saves. A test documents what the document does when an adopter breaks that guarantee (it follows the new layout).PlaceRenderer, at the first reporting call of any kind (loop call, keep-together probe, retry afterNothing). WithL'= limit minus the continuations counted where the loop's next call would start:bound <= L'gives New and runs nothing. Otherwise the twin is placed layout-only, then the new layout, each with theRunHoldssave and restore and a non-throwing stop at the limit. At the retry site two things differ, because the loop offers the call after the retry the page-top box, which can be one rounding step taller than the box the retry got (top margin 33.3, bottom margin 10.1, page 100: 56.6 against 56.60000000000001). A report from a retry that answered Nothing chooses the twin without a probe, since the loop is about to refuse the element and the twin decides that refusal. The twin probe first calls the twin once on the retry's own box with the retry's flags, and a Nothing there counts as a fault. The twin is chosen when it reaches the limit or throws, which keeps a 2.3.3 refusal with the twin's own exception type whatever the new layout would do, and when the new layout reaches the limit or throws, because the twin is 2.3.3's own placement and saves what 2.3.3 saved. The new layout is chosen only when both probes finish cleanly, and the twin's probe decides alone when it does not. On Twin the call (loop and retry) or the iteration (keep-together probe) is made again with the twin. The first piece accepted with no report fixes the mode at Twin. A probe never decides. Out-of-memory and cancellation are not caught.(root instance, offered.Y, offered.Height, continuations so far, retry site or not), shared by both passes and keep simulations. Cleared byRunLayout, and again whenever a plan-dependent element'sResolvereturns true, because a resolve changes the layout and not the key.LayoutContext.Address(parent, child, area, atTop, lineCounts)makes the context a built-in parent hands a child it lays out inside its ownLayout. It carries the fresh-page area, takes the page-top and line-count flags (false, asWithArealeaves them, unless the parent passes its own) and gives the child the parent's mode.LayoutContext.Addressed(area, child, mode)does the same where the parent has no document context, which is itsDrawre-layout; the parent keeps the mode its own layout had. A child's report is never read, since the parent reports for itself. A call in which a child reported while the mode was undecided and the parent did not is counted inDocumentRenderer.UnreportedChildReportsand fails a Debug build (Debug.Assert), because the child laid out the new way and the first piece then fixes the twin: neither layout.internal int ContinuationLimit { get; init; }, read by the throw and the decision.internal interface IContinuationTwin { IRenderer Twin(); }.Removed in 3.0 (#688)
3.0 lays a built-in renderer out one way, so every compatibility device here goes. Each carries a comment naming 3.0 and #688:
CallLayout's addressing,SettleCall, the keep-together restart, the first-piece rule, the memo clear inMeasureUntilStable, and the decision and its probes.Session,ModeFor,IsPlacedAsTwin,AddressandAddressedonLayoutContext.ContinuationMode(defaultTwin),IContinuationTwinandContinuationSession(the whole new file).DocumentRenderer:DecideContinuation,ProbeContinuations,SettleCall,NoteStrays, the reused_session, the memo andContinuationKey, theContinuationProbes,ContinuationProbeFaultsandUnreportedChildReportscounters,AssertOnUnreportedChild, and the first-piece rule in the loop.PlaceLoopstays, as the plain loop with the mode plumbing deleted.ContinuationLimitcan stay as a test seam.Parents the adopting PRs must convert
A built-in parent keeps its child on the twin until it addresses the child. In this tree that is
HeadingRenderer.Layout(passes its context through, so its paragraph is addressed to the heading and takes the twin),ListRenderer.Layout(WithAreafor each item) andListRenderer.Draw(new LayoutContext(itemArea)), which has to useAddressedwith the mode its own layout had. I searchedsrcfor.Layout(,new LayoutContextandWithArea(; the table renderer lays out no child through a context. A converted parent also reports for itself, whichUnreportedChildReportschecks.No output change
2.3.3+345a521...and2.3.3+c76bda8..., a build of this branch; the two commits after it change a test file and one comment). 10,000 generated documents, seeds 0 to 9,999: paragraphs with keep and line-count rules, headings, ordered, unordered and nested lists, tables with header rows, header and footer bands with{page}and{pages}, mirrored margins, tagged output, conformance claims, images, page breaks, caller wrappers around paragraph, list and table renderers. PinnedTimestampandDocumentId, SHA-256 of the raw bytes. 7,788 saved documents and 2,212 refusals (135 of them the continuation limit), 0 differences: the refusals have the same exception type and message hash, and the two result files have the same SHA-256. 7,788 distinct saved outcomes.REQUIRE_ORACLES=1and the pinned veraPDF, qpdf and poppler: net10.0 and net11.0 each 2,298 run, 2,296 passed, 0 failed, 2 skipped (the two Linux pipe tests inSaveLifecycleTests). The existing goldens and byte-identity pins are in that count.Cost
Release, net10.0, alternating the base and this branch, four rounds, each the median of 9 saves after 2 warm-ups (15 for the mixed document); the time columns are the median of the four round medians. One reused session replaces the per-element session, and a context carries one reference, so the residual is one pointer per layout call, 8 bytes. Shared machine; the round medians of one variant spread by up to about 12 ms (the mixed document's base ran 68.0 to 80.0), so the time differences below are within that spread.
{page} of {pages}footer (two passes)LineSeparatorsOne base round of the mixed document allocated 44.242 MB, one round of this branch 44.318 MB, and one footer round of this branch 49.874 MB; the other rounds of each variant repeat their figure to the kilobyte, so these look like a collection landing inside the measured window. The allocation columns use the repeated figure. The 3.0 step is to remove the context reference: with no twin there is nothing to address. No technique removes it in 2.x, since a renderer reaches the document only through the context it is handed.
Mutations
Run in a detached worktree at 28f9b6a (the code is that of the head; the head's one later commit changes a comment), removed afterwards. Limit 300. Counts are failed cases of
ContinuationLimitTests(net10.0, Release, 171 cases). Each mutant fails at least one test except the one listed below the table.WithAreacopies the sessionMemo_isKeyedByTheCountAsWellAsTheStartMemo_isKeyedByTheRetrySiteAsWellAsTheStartMemo_isKeyedByTheElement_soTwoElementsAtTheSameStartDecideApartMemo_doesNotCarryAVerdictAcrossMeasuringRunsThatResolveToADifferentLayout,Probe_restoresTheCursorAndPageParityAddressgives the child no modeAddress_carriesWhatTheParentNamesAndWhatTheDocumentSet,Address_givesTheChildTheParentsModeAddressdrops the fresh-page area, the page-top flag, or the line-count flag (three mutants)Address_carriesWhatTheParentNamesAndWhatTheDocumentSeteach (2, 2, 2)Addresseddrops the modeAddressed_givesADrawTimeChildTheModeItsParentWasLaidOutInAddressedgives a twin parent's child a sessionAddressed_givesADrawTimeChildTheModeItsParentWasLaidOutInAddressunder a twin parent gives the child a sessionAddress_carriesWhatTheParentNamesAndWhatTheDocumentSet,Address_givesTheChildTheParentsModebound < L'Decision_boundWithinWhatIsLeft_runsNoProbe(3)Decision_firstPieceWithoutAReport_fixesTheTwin,Parent_thatAddressesAReportingChildAndDoesNotReport_isCountedCallLayoutleaves the session unset, or leavesCalledunsetDecision_picksTheLayoutThatKeepsTheVerdict(2)Decision_probesStartWhereThePlacementStarts_notAtThePageTop(2)Probe_outOfMemory_isNotCaught,Probe_cancellation_isNotCaughtDecision_twinChosenAtTheKeepTogetherProbe_redoesTheIterationWithTheTwinMemo_doesNotCarryAVerdictAcrossMeasuringRunsThatResolveToADifferentLayoutRetrySite_newLayoutThatFitsOnlyTheTallerPageTopBox_keepsTheTwinsSave(2)RetrySite_twinThatFitsOnlyTheTallerPageTopBox_keepsItsRefusalRetrySite_twinThatFitsOnlyTheTallerPageTopBox_keepsItsRefusalReport_fromAnInstanceTheDocumentDidNotCall_countsAsAStrayamong the failures (1, 2, 1)Parent_thatAddressesAReportingChildAndDoesNotReport_isCounted(1),Parent_thatReportsForItself_andAddressesItsChild_decidesLikeAnyRenderer(1)Resetleaves the modeResetleaves the twin-chosen markIsPlacedAsTwin_doesNotCarryFromOneElementToTheNextIsPlacedAsTwinignores a probeIsPlacedAsTwin_isTrueOnlyForTheDocumentsOwnCallOutsideAProbeIsPlacedAsTwinignores which instance the document calledIsPlacedAsTwin_isTrueOnlyForTheDocumentsOwnCallOutsideAProbeIsPlacedAsTwinreads the mode instead of the decisionIsPlacedAsTwin_doesNotCarryFromOneElementToTheNext,IsPlacedAsTwin_isFalseOnEveryCallOfAPlacementThatNeverReported,IsPlacedAsTwin_isTrueOnlyForTheDocumentsOwnCallOutsideAProbeIsPlacedAsTwin_doesNotCarryFromOneElementToTheNext,IsPlacedAsTwin_isFalseOnEveryCallOfAPlacementThatNeverReportedIsPlacedAsTwin_doesNotCarryFromOneElementToTheNext,IsPlacedAsTwin_tellsATwinTheDocumentChose_andNotOneAProbeOrAWrapperCalledDecision_aNewDecision_doesNotMarkThePlacementAsChosenTwinContinuationModeis not the twinContinuationMode_defaultsToTheTwinReport_fromAnInstanceTheDocumentDidNotCall_countsAsAStray,Report_isDroppedUnlessTheCalledInstanceMakesItWhileUndecided(2)Not killable, and why:
Tests
ContinuationLimitTestsandContinuationSupport, with an internal scripted renderer whose layouts differ in what they place (a piece count, or a content height that each piece fills the offered area with, so the count depends on where it starts), at a limit of 300:L'andL' + 1(zero probes against two); a probe that goes past the reported bound fails the test.WithArea, public constructor, laid out twice at half width, results cached by area) across six count pairs, against the same wrapper around a renderer that never reports; a container placing two elements.bound <= L': no probe runs, and both directions are pinned as the adopter's contract. The retry site with a page-top box one rounding step taller than the retry's: a twin that fits only the taller box keeps its refusal, and a new layout that fits only the taller box keeps the twin's save, with a bound above and withinL'.Addresscarries the fresh-page area, the page-top and line-count flags;Addressedgives a Draw-side child its mode;IsPlacedAsTwinis true only for the document's own call outside a probe after a decision chose the twin, on every piece including the twin's overflow, and false on a placement that never reported and on one a decision settled for the new layout; a child of a twin parent gets no session.WithAreaand constructor leave the session unset, the default limit of 50,000.Gates
Release build 0 warnings; Layout.Tests net10.0 and net11.0 as above;
ContinuationLimitTestsalso passes in a Debug build on both TFMs (171 of 171), where theDebug.Assertis live;dotnet format --verify-no-changes; clean-room check;generate-roadmap.py --check.CHANGELOG
None. A user cannot observe anything from this PR: no renderer reports, the output is byte-identical, and nothing public changes. The adopting PRs list their own changes.
Notes for the adopting PRs
bound <= L'(what is left of the limit), no probe runs, whatever the element's real count, and the new layout is used, except at a retry whose own call answered Nothing, which takes the twin at any bound. The adopter's new layout must then refuse exactly where its twin refuses, with the twin's exception type, and must never refuse where its twin saves. Wherebound > L'the probes enforce both: a twin that throws, or answers Nothing at a page top or at the retry's box, keeps its refusal, and a new layout that throws keeps the twin's save. A fix that has to change a refusal says so in its own PR.Address(inLayout) orAddressed(inDraw).context.IsPlacedAsTwin(this), and carries that to the pieces it returns. It is true on every call the document makes after the decision chose the twin, and false on the calls before the decision and where the first piece was placed with no report, so a renderer that records a fallback records it only for a placement a decision chose.Layoutto its twin and getsFullback is drawn itself, because the document draws the instance it called. ItsDrawmust forward to the twin, or a parent'sDrawthat re-lays a child out will lay it out the new way under a wrapper.Addressed.ContinuationModedefaults toTwin, so a field that was never set lays the child out as 2.3.3 did; a parent that wants the new layout must set the field from the call it was laid out in.TooManyContinuationsstill names 50,000 whateverContinuationLimitis.