Repository navigation
performance: speed up isometric SVG export, repeated renders and the e2e suite - #2992
Merged
Merged
Conversation
diff.Testdata writes a .got file and runs git diff for every golden, which is 2,801 processes in one run of the suite. Nearly every golden matches, so compare the bytes first and call diff.Testdata only when they differ. A mismatch is reported, and accepted with TESTDATA_ACCEPT, exactly as before. The cases of a group already ran in parallel, but the twelve groups ran one after another, each waiting for its slowest case. Start the groups together so the run is bounded by its total work. With -failfast, the groups after a failing one are no longer skipped, because their cases already exist when the first failure is reported. Co-Authored-By: Claude Fable 5.2 <[email protected]>
LayoutNested logs every nested graph before and after its layout. The attribute was g.PrintString(), which builds the absolute ID of every object, and Go evaluated it whether or not debug logging was on. Pass a value that prints the graph when a handler formats it. It has both String, for log.PrettyHandler, and LogValue, for slog's handlers, so the logged text is unchanged. Co-Authored-By: Claude Fable 5.2 <[email protected]>
A Ruler measured all 352 runes of every font and size it met, and each compile, legend, tooltip and Markdown block creates its own Ruler. One run of the e2e suite built 2,429 such atlases for 78 distinct ones. Keep the glyph map and line metrics of a bundled font in a process-wide table keyed by font file, size and rune set. The map is never written after it is built, and every Ruler keeps its own face. The table stops growing at 256 atlases, about 10 MB. Only the font files compiled into D2 are shared. internal/bundledfont recognises them by backing array, so a font registered at run time is measured as before and is never kept alive by the table. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Cutting a font down to the characters of a diagram, compressing it to WOFF and encoding it is most of the cost of embedding fonts in an SVG, and it was repeated for every render. The result depends only on the font file and the characters, so remember it for the bundled fonts. The cache is guarded by FontFamiliesMu and emptied when its keys and values pass 8 MB. Fonts registered at run time are not cached. Co-Authored-By: Claude Fable 5.2 <[email protected]>
embedFonts ran a regular expression over the whole SVG to collect the text of natively rendered Markdown. A match needs the literal md-text class, which most diagrams never contain, so look for that first. Co-Authored-By: Claude Fable 5.2 <[email protected]>
The solver subtracts every nearer face from every fragment of a face. It allocated three slices per clipped edge, clipped each overlapping pair twice, kept its fragment index in maps, sorted each face's occluders with a comparator, and asked the context whether it was canceled on every unit of work: 1.9 billion times in one run of the e2e suite. Keep the algorithm, its output and its work accounting, and change how it is carried out: - Clip a fragment against a cut once. The second pass repeated the first to collect the outside pieces; collect them in the first and charge the budget for the pass that is no longer run. - Take polygon storage from a per-solve pool with free lists, and copy what survives into slabs. The scratch is reused between renders unless it has grown past 8 MB. - Rank occluders once per scene; a face sorts the ranks of its candidates. - Index fragments with slices and stamps instead of maps. - Charge the budget with one inlined comparison, and consult the context every 4,096 units of work. On a generated scene of 3,000 faces this takes 44 ms instead of 110 ms and allocates 1 MB instead of 41 MB. svg_visibility_reference_test.go freezes the previous solver as an oracle. The new tests require bit-identical fragments, and identical errors wherever a limit stops the solve, on generated scenes. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Paint ordering clips pairs of polygons the way the visibility solver does. Use the solver's in-place helpers and two reused buffers instead of three slices and a closure per edge. svgFillPath numbers each vertex, so an edge is one word in its tables instead of two points, and keeps adjacency in flat arrays. Loops are walked in the same order, so the path text is unchanged. Canonical loops are rotated in place in a buffer shared across polygons. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Rasterized label and icon surfaces are converted to premultiplied textures. draw.Draw multiplies and divides every channel of every pixel, but 97.6% of the pixels in the e2e suite are fully transparent or fully opaque. Skip the former, copy the latter, and do the arithmetic for antialiased edges only. A test compares the result with draw.Draw for every channel value at every alpha. Co-Authored-By: Claude Fable 5.2 <[email protected]>
The isometric renders of the suite are bounded separately from go test's parallelism because of their memory use. A render now allocates far less, so allow four at once instead of two. Co-Authored-By: Claude Fable 5.2 <[email protected]>
A removed fragment kept its slot and its cell entries until the next face, so a face whose fragments are replaced again and again needed memory for every fragment it ever had: tens of megabytes where the previous solver, with maps of live fragments, needed a few. Renumber the survivors once removed fragments far outnumber them. The budget charges for live fragments only, so the accounting does not change. Return a worker that has outgrown the retention limit to the pool empty instead of dropping it. A pool that keeps its entries in a slice, as TinyGo's does, could go on referring to a dropped worker. The equivalence test's scene for the bounding-box tree ran out of work in both solvers, so only two error strings were compared. Use a scene that fits, and fail when a scene that is meant to complete does not. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Take one copy of Runes for the key and for the measurement, and say on Runes that a different set must be a new slice. Note in the two tests that modify a bundled font's bytes in place that nothing may measure or subset the font meanwhile, since those results are now shared. Co-Authored-By: Claude Fable 5.2 <[email protected]>
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Nothing failed if the e2e golden comparison stopped comparing, because the suite only hands it output that matches. Test both outcomes and the acceptance path directly. The order tree's tie-break test passed with the tie-break removed, because its input was already sorted, and nothing checked that nativePremultiply goes on consulting the context after its first row. svgAppendCanonicalLoop now clears its buffer itself instead of requiring an empty one. Co-Authored-By: Claude Fable 5.2 <[email protected]>
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.
This makes the e2e suite about 6x faster, makes slow isometric SVG exports about 4x faster, and removes text and font work that the renderer repeated for every diagram. No output changes: every e2e golden (SVG, board JSON and isometric SVG) is byte-identical.
go test ./e2etests(Apple M4, 10 cores)SKIP_ISOMETRIC_CHECK=1Wall times are the fastest of four runs of each test binary (six without isometric checks), taken in rotation; CPU time and peak RSS are medians of the same runs. Other work on the machine overlapped some runs: the whole-suite runs of this branch took between 12.3 s and 16.6 s, and master's between 76.1 s and 78.1 s.
Where the suite's time goes, adding the changes one group at a time:
SKIP_ISOMETRIC_CHECK=1The last two rows of the whole-suite column depend on their order. In the commits the rendering changes come first: they take the suite from 54.7 s to about 18.2 s, and four slots take it from there to about 12.2 s (the slot measurements under Notes for review).
Isometric SVG, one render of each of the suite's 870 isometric boards from its stored layout, one at a time, each compared with its golden: 104.1 s in total on master, 30.2 s on this branch. The 165 boards that took 100 ms or more on master are 3.8x faster at the median. The slowest board of each of the five slowest cases:
real_world/tpmjs_architecture(dagre)stable/us_map(dagre)regression/grid_oom(tala)stable/nesting_power(dagre)stable/arrowhead_scaling(tala)Ordinary (non-isometric) rendering of a single diagram changes little. See the last section.
Test harness
diff.Testdatawrites a.gotfile and startsgit difffor every golden: 2,801 processes per run. The harness now compares bytes first and callsdiff.Testdataonly on a mismatch, so failures print the same diff andTESTDATA_ACCEPTworks as before. Threegitprocesses are left per run, on purpose:TestIsometricSVGSnapshotwrites a golden into an empty directory, and a missing golden still goes throughdiff.Testdata.TestTestdataComparesGoldenshands the new comparison a mismatch twice.-failfaststops less. Every group has started before a case can fail, so it skips only the cases a group has not created yet, where it used to skip every later group.Rendering
Rulermeasured all 352 runes of each (font, size) it met, and the renderer creates more rulers than the one a compile is given: for a legend, for each positioned tooltip, for the appendix, and for a render that contains Markdown. The suite built 2,429 atlases per run; it now builds 78. The shared map is never written after it is built. Only the ten text fonts compiled into D2 are shared (the newinternal/bundledfontrecognises them by backing array); a font registered at run time is measured as before and is never retained. The table stops at 256 atlases, about 10 MB.LayoutNestedcalledg.PrintString()twice for every graph it laid out, whether or not debug logging was on.The first two help a process that renders more than once (watch mode, d2.js, the Go API, the tests) and a CLI run that needs a second
Ruler. The first render of a font and size costs what it did before. The last two save microseconds per render.Isometric SVG
Visibility solver. The same fragments, bit for bit, and the same work accounting, from the same subtraction of nearer faces:
On a generated 3,000-face scene: 98 ms and 41 MB allocated become 43 ms and 1 MB.
Path output numbers vertices so its edge tables hash one word, and reuses loop buffers.
Surface textures are premultiplied without
draw.Draw: transparent pixels are skipped and opaque ones copied, which is 97.6% of the pixels in the suite.The previous solver is kept as a test oracle in
svg_visibility_reference_test.go(804 lines, identifiers renamed, otherwise verbatim).TestSVGVisibilityMatchesReferenceSolverandTestSVGVisibilityLimitsMatchReferenceSolverrequire bit-identical fragments, and identical errors wherever a work, fragment, vertex or index limit stops the solve, on generated scenes including one large enough for the bounding-box tree. If you would rather not carry the oracle, it can go together with those two tests and thereferencerun ofBenchmarkSVGVisibility. The 870 isometric goldens still cover the output.Verification
./ci/test.shpasses (90 packages), and each of the first nine commits builds and passes the tests of the packages it touches. The four after them are review follow-ups, a changelog entry and tests.go test -racepasses for./e2etests, with and without isometric checks, and for the seven other packages the branch changes.GOOS=js GOARCH=wasm go build ./d2js/...,GOOS=windows go build ./..., andgo build ./...andgo vet ./...withGOOS=linux GOARCH=386are clean.Notes for review
go testruns at most four cases at once by default, so the slot limit no longer binds there.sync.Pool, which Go empties at garbage collection. As far as I can tell from its source, TinyGo'ssync.Poolnever drops entries, so d2.js would keep that scratch for the life of the instance.-failfastabove: a cancelled isometric render is noticed within 4,096 units of solver work (a few nanoseconds each) instead of at the next charge.LayoutNestedlogs a value that reads the graph when a handler formats it, so a handler has to format attributes insideHandle, asslog's handlers andlog.PrettyHandlerdo. A caller that editstextmeasure.Runesin place, instead of assigning a new slice, is served the bundled fonts' metrics as measured before the edit. The comment onRunessays so.ci/test-wasm.sh). CI ran it at the head of the branch and it passed.-failfastthe later groups' cases already exist when the first failure is reported. Some may not have been created yet, and those are still skipped. 47f6c5a quotes an earlier run of the 3,000-face benchmark (110 ms and 44 ms, where the figures here are 98 ms and 43 ms). 03db2bb says thatLayoutNestedlogs every nested graph, and it logs the root graph too. 203ebf2 and 47f6c5a write 8 MB for the two limits, which are 8 MiB.EdgeLengthfrom starting a goroutine per processor for small graphs. It saved CPU but made a single layout slower on an idle machine (up to 2x for that call on graphs with a few connections per node), so I took it out. A threshold based on work rather than node count might be worth a separate look.dagroandelk-go. With those two engines, 666 of the remaining 1,135 ms are spent outside the layout engines, in D2's own compiling and rendering. One CLI run gained under 1 ms on each of five test diagrams that take 15.0 to 47.3 ms. For a tiny diagram nearly all of the run is start-up:d2 --versiontakes 12.8 ms against 14.3 ms for the render in the same session, and about 7.0 ms of a run is package initialisation, 5.0 ms of it chroma's lexer and style registries.🤖 Generated with Claude Code