Repository navigation
pptx: write the XML parts without text/template (binaries about 3 MB smaller) - #3000
Merged
Merged
Conversation
lib/pptx generated its XML parts by executing text/template files. text/template calls reflect.Value.MethodByName, and once that is reachable the linker keeps every exported method of every type converted to an interface, used or not. That was about 3 MB of each release binary. Each part is now written by a small function in parts.go. Its format strings are the template text, with each value where the template put it: strings escaped by escapeXML as before, integers in decimal (the templates printed them with fmt.Sprint, and digits need no escaping). The two parts without values are written as they are. The archive is byte for byte the same. parts_test.go executes the old templates with the old escaping function as the reference. It compares every part for the zero value and 2000 random values of its content: XML syntax, quotes, invalid UTF-8, characters XML cannot represent, empty and long strings, nil and long slices, negative and extreme integers. It also compares whole archives for 100 random presentations (0 to 142 slides, with and without navigation, with internal and external links). The entries, their order and their bytes are unchanged, and Go's DEFLATE writer compresses an entry in buffered blocks of up to 64 KiB, so writing the bytes in fewer Write calls does not change the compressed stream. Errors and panics happen at the same points as before, with the same bytes written. The template sources stay exported for one compatibility release, deprecated: changing them no longer has an effect. Release binaries (darwin/linux/windows amd64): 44,404,144 / 43,471,008 / 44,274,688 -> 41,378,544 / 40,591,520 / 41,291,264 bytes. Exporting a 30-slide deck takes 2.14 ms instead of 3.30 ms, with 6,376 allocations instead of 31,541. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The d2 binary links internal/testutil/imagediff: the deprecated pptx.Validate and the GIF validator in lib/xgif call into internal/testutil, which imports imagediff. imagediff parsed its HTML report template into a package variable, so every d2 start parsed it, and the binary kept the template parser. buildReport now parses the template, and d2 never calls it. The reports are unchanged: same template text, and byte-identical reports for four comparisons checked before and after. Release binaries (darwin/linux/windows amd64): 41,378,544 / 40,591,520 / 41,291,264 -> 41,198,928 / 40,423,584 / 41,115,648 bytes. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
alixander
added a commit
that referenced
this pull request
Nov 22, 2026
Brings in #3000 (PPTX export without text/template), which keeps the release binaries of this branch under the 45 MB limit. Co-Authored-By: Claude Opus 5.5 <[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.
d2's release binaries shrink by about 3 MB (7%), and PPTX output stays byte for byte the same.
Why. lib/pptx filled its XML parts by executing
text/templatefiles.text/templatelooks up methods by name (reflect.Value.MethodByName), and once such a call is reachable, Go's linker can no longer drop unused methods: it keeps every exported method of every type converted to an interface, anywhere in the program. In d2 that is about 3 MB of each binary.go build -ldflags=-dumpdepshows 26<ReflectMethod>marks on master and none with this change.What changes (one commit each)
lib/pptx/parts.go: one function per XML part (contentTypesXML,slideRelsXML,slideXML,presentationRelsXML,presentationXML,coreXML,appXML) builds it withfmt.Appendf. The format strings are the template text, so the XML reads as before. Strings go throughescapeXML, stillxml.EscapeText; integers are written in decimal. The two parts without values are written as they are. The seven exported template sources stay for one compatibility release, deprecated; changing them no longer has an effect.internal/testutil/imagediffparses its HTML report template when it builds a report instead of at package initialization. The d2 binary links this package (through the deprecatedpptx.Validateandxgif.Validate) but never builds a report, so every d2 start parsed the template for nothing, and the binary kept the template parser (about 0.17 MB).Output is unchanged.
parts_test.goexecutes the old templates with the old escaping as the reference. It compares every part for the zero value and 2,000 random contents (XML syntax, quotes, invalid UTF-8, characters XML cannot represent, empty and long strings, nil and long slices, extreme integers), and whole archives for 100 random presentations (0 to 142 slides, with and without navigation, internal and external links). Each of 15 deliberate mistakes inparts.gofails these tests. Side by side with master's exporter, 2,000 random archives were identical, and so were writers that fail after N bytes (the same errors and the same bytes written) and the panics of odd inputs such as a nil slide (the same value at the same point). The CLI's PPTX exports and imagediff's reports are byte-identical too. One contrived difference remains: if the destination writer panics in the middle of a part larger than 64 KiB and then accepts the zip writer's closing writes, the interrupted entry's recorded CRC-32 and size differ.Sizes (Go 1.27.4, built as
ci/release/_build.shbuilds them, with-buildvcs=false):With this change, #2999 (faster layout engines, which adds about 1.3 MB) fits the 45,000,000 B budget on every target with about 2.5 MB to spare.
Speed. Exporting a 30-slide deck takes 2.1 ms instead of 3.3 ms, with 6,400 allocations instead of 31,500. A d2 start (
d2 version, measured with #2999's branch) runs about 2.7% fewer instructions, since imagediff no longer parses its template.Checked: gofmt; vet, native and js/wasm;
go testof lib/pptx (also with-race), internal/testutil/..., lib/xgif, d2renderers/d2raster, d2cli, e2etests-cli (including its six PPTX cases) and the root package. Each commit builds and vets on its own.Not done here:
html/templateandtext/templatestill initialize in the d2 binary (about 55 KB), because the deprecatedpptx.Validateandxgif.Validateimport internal/testutil. That goes when those helpers are removed.🤖 Generated with Claude Code