Skip to content

pptx: write the XML parts without text/template (binaries about 3 MB smaller) - #3000

Merged
alixander merged 3 commits into
masterfrom
perf/pptx-xml-writers
Nov 22, 2026
Merged

alixander merged 3 commits into
masterfrom
perf/pptx-xml-writers

Conversation

@alixander

@alixander alixander commented Nov 22, 2026 •

Copy link
Copy Markdown
Collaborator

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/template files. text/template looks 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=-dumpdep shows 26 <ReflectMethod> marks on master and none with this change.

What changes (one commit each)

  1. lib/pptx/parts.go: one function per XML part (contentTypesXML, slideRelsXML, slideXML, presentationRelsXML, presentationXML, coreXML, appXML) builds it with fmt.Appendf. The format strings are the template text, so the XML reads as before. Strings go through escapeXML, still xml.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.
  2. internal/testutil/imagediff parses its HTML report template when it builds a report instead of at package initialization. The d2 binary links this package (through the deprecated pptx.Validate and xgif.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).
  3. A changelog entry.

Output is unchanged. parts_test.go executes 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 in parts.go fails 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.sh builds them, with -buildvcs=false):

target master this PR change
darwin/amd64 44,404,144 41,198,928 -3,205,216
linux/amd64 43,471,008 40,423,584 -3,047,424
windows/amd64 44,274,688 41,115,648 -3,159,040
darwin/arm64 41,179,362 38,138,946 -3,040,416
linux/arm64 40,042,656 37,159,072 -2,883,584
windows/arm64 40,410,624 37,489,664 -2,920,960

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 test of 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/template and text/template still initialize in the d2 binary (about 55 KB), because the deprecated pptx.Validate and xgif.Validate import internal/testutil. That goes when those helpers are removed.

🤖 Generated with Claude Code

alixander and others added 3 commits November 22, 2026 11:33
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]>
@alixander
alixander merged commit d827485 into master Nov 22, 2026
14 checks passed
@alixander
alixander deleted the perf/pptx-xml-writers branch November 22, 2026 23:02
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant