Skip to content

perf(docx): stop reading a table's rows quadratically and stop XMLBeans throwing on every colour - #1084

Merged
DemchaAV merged 2 commits into
developfrom
perf/docx-table-export-speed
Nov 23, 2026
Merged

DemchaAV merged 2 commits into
developfrom
perf/docx-table-export-speed

Conversation

@DemchaAV

@DemchaAV DemchaAV commented Nov 23, 2026 •

Copy link
Copy Markdown
Owner

Why

A thousand-row table took 657 ms and allocated 232–234 MB to export as DOCX (DocxExportJmhBenchmark,
table1000), where the PDF render of the same document takes about a tenth of the CPU time (62 ms
against 688 ms, measured below).
A profile of DocxExportJmhBenchmark (table1000) put the cost in three places that are not the
writing of the table itself:

  • Rows read by index walk the whole table. POI's XWPFTable.getRow(int) and getNumberOfRows()
    call CTTbl.sizeOfTrArray() on every call, and that counts the table's w:tr elements. The export
    reads each row that way several times while writing it (writeTableRows, holdRowHeight,
    rulesBeyondOne, carryTheRowsRounding, breakRowsWhereTheLayoutDoes, carryDrawingsInRows), so
    the reads grew with the square of the rows: sizeOfTrArray was 18.7% of the export's CPU samples.
  • Every colour went in as a string. A Word colour attribute (w:color, w:fill) is a union of the
    word auto and three hex bytes. Given a string, XMLBeans tries auto first and throws, and catches,
    an XmlValueOutOfRangeException — stack trace and all — for every other colour; four times for each
    ruled cell. Of the export's allocation, 7.8% was the exceptions and 20% the string set that threw them.
  • Per-paragraph questions ran an XPath each. The passes that count exact lines and fill the closing
    spaces of shaded runs asked every paragraph and run for a few of its children with selectPath.

What changed

  • rowOf(table, index) reads a row from POI's own list of the table's rows (getRows()), and
    rulesBeyondOne takes the count from it too. The loops that use it all run on the table
    writeTableRows has just created through POI. getRow itself reads that list; what it adds is
    the count of w:tr to check the index, and every index read here is in range. Row reads outside
    writeTableRows are left alone: elsewhere they are one getRow(0) a table, except in
    DocxTableSplit, which reads row by row too but runs only on a table cut where its pages'
    margins change or in a panel — off the path measured here.
  • DocxTranslucency.rgb(Color) gives a colour as its three bytes, and every RGB colour attribute
    the export writes now takes those (a chip's w:color="auto" stays a string: it matches the
    union's first member, and nothing is thrown): cell and paragraph borders (paintEdge), cell and chip shading, a
    run's colour, the Normal style's colour and the timeline rail's border. XMLBeans writes the bytes as
    upper-case hex, the text hex(Color) wrote. A run's colour is set on its w:color directly, as
    XWPFRun.setColor does, since that method takes only a string.
  • DocxXmlChildren.path(from, steps...) / first(...) walk an element's children by name and select
    what an XPath of child steps selects, in document order, typed and untyped XML alike.
    isWrittenExact, holdsTextOfItsOwn and DocxClosingSpaces (a paragraph's runs, a run's shading,
    a drawing's inline) use it. The whole-document .//w:p and .//w:t selections stay selectPath:
    measured on the same document, selectPath(".//w:p") took 12–13 ms against 17–20 ms for a cursor
    walk returning the same 5,006 paragraphs, so those passes were not the cost.

No public API changes; PDF and PPTX are untouched.

Measurements

JMH DocxExportJmhBenchmark.docx, -prof gc, gc.alloc.rate.norm per export — the number load
does not move (two runs of both builds, JDK 24, base = develop at the branch point; 3 forks × 5
iterations, then 4 interleaved forks × 5):

shape base alloc new alloc
small 1.86 / 1.88 MB 1.68 / 1.67 MB
report20 10.12 / 10.17 MB 9.47 / 9.46 MB
table1000 232.2 / 233.5 MB 159.1 / 158.5 MB
images20 3.02 / 2.96 MB 2.93 / 2.91 MB

Time was measured on a machine other sessions' Gradle builds were loading (up to 99% CPU), so
wall-clock means alone do not compare. Two measurements that do:

  • Interleaved JMH, one fork of each build a round, alternating which goes first, ms/op per round:

    shape base new
    table1000 1799, 1332, 1660, 788 (median 1496) 398, 498, 454, 914 (median 476)
    report20 17.9, 62.0, 54.5, 249.2 50.8, 45.1, 87.2, 60.0
    small 4.3, 12.8, 12.6, 21.6 10.2, 11.0, 12.5, 6.4
    images20 12.3, 11.2, 15.6, 12.3 9.2, 6.3, 4.7, 6.6

    table1000 is faster in three rounds of four (the fourth, 788 → 914, the other way); the
    smaller shapes move within the noise.

  • Thread CPU time per table1000 export (median of 20 after 8 warm-up, three rounds, alternating):
    1313 → 797, 984 → 891, 688 → 500 ms. In the last round the machine was quiet (wall ≈ CPU:
    697 → 544 ms). The PDF render of the same document: 62 ms CPU.

On a quiet machine before the change, JMH gave table1000 657 ± 250 ms/op.

Output unchanged

  • The 105-document DOCX fidelity corpus, exported before and after: every part of every document
    byte-identical, and the export report's 1,718 notes the same.
  • The benchmark's four shapes, exported by both builds: every part identical but
    docProps/core.xml, which holds the export's creation time.

Tests

  • DocxXmlChildrenTest (new): a path selects the same objects as its XPath, in the same order, over
    runs and links interleaved, and over untyped XML; first is the first in document order and null
    where the path ends in nothing.
  • DocxTranslucencyTest.aColourGivenItsBytesIsWrittenAsItsHexIs: w:color on a border, w:fill on
    a shading and w:val on a run colour write the same XML from the bytes as from the hex, for five
    colours (zero and full bytes, a leading-zero byte, a translucent colour).
  • DocxTranslucencyTest.anOpaqueBodyColourIsTheNormalStylesColour: the Normal style's colour, which
    no test read other than as black.
  • DocxClosingSpacesTest.aLinksAndAFieldsRunsAreReadInTheirPlacesAmongThePlainRuns: a plain run, a
    link, a simple field and a plain run, in that order, are read back from the end in the order they
    stand — the walk the hand-written run list replaces an XPath union for.
  • DocxExportReportTest.aParagraphWhoseTextIsAllInALinkIsCountedAmongTheExactOnes: the link branch
    of holdsTextOfItsOwn; the test asserts the fixture's paragraph has its text only inside
    w:hyperlink.
  • Seventeen sabotages, each breaking one rule of the change once, each caught by the test named for it
    (rows read from the first row, the last row never last, byte order, each colour site, a step taking
    only its first child, links and simple fields left out of a paragraph's runs or read out of their
    places, the shading and the
    spacing read off the wrong element, an inline drawing passed over, the run and link branches of the
    text check).
  • ./mvnw -B -ntp clean install -pl :graph-compose-render-docx -am: core 845, render-pdf 347,
    render-docx 2787 tests, 0 failures. ./mvnw -B -ntp test -f qa/pom.xml: 1926 tests, 0 failures.

…ytes

POI's XWPFTable.getRow and getNumberOfRows count the table's w:tr
elements on every call, and writing a table read each row that way
several times, so the reads grew with the square of its rows. The row
passes of writeTableRows now read POI's own list of the table's rows.

A Word colour attribute is a union of the word auto and three hex bytes.
Given a hex string, XMLBeans tried auto first and threw and caught an
XmlValueOutOfRangeException for every other colour, four for each ruled
cell. Every RGB colour the export writes is now given as its bytes
(DocxTranslucency.rgb), which XMLBeans writes as the same upper-case hex.

The exact-line count and the closing-space fill asked each paragraph and
run for a few children through an XPath each; DocxXmlChildren walks the
children instead and selects the same elements in the same order.

table1000 allocates 233 -> 158 MB per export. The DOCX corpus and the
benchmark documents are byte-identical before and after.
…run order

The CHANGELOG entry said table1000 was faster in each of four interleaved
rounds; it was faster in three. Its allocation figures are now the mean
of the two runs, 233 -> 159 MB per export for table1000, rounded alike
on both sides, and its colour sentence names the attributes the change
covers, w:color and w:fill, rather than every colour in the file.

rowOf's javadoc said the row list holds the same rows as the XML for a
table written through POI; getRow reads that same list itself, and only
its index check counts the rows, which is the reason the change is safe.

DocxClosingSpacesTest gains a paragraph of a run, a link, a simple field
and a run, read back from the end in the order they stand.
@DemchaAV
DemchaAV marked this pull request as ready for review November 23, 2026 17:15
@DemchaAV
DemchaAV merged commit b3e774d into develop Nov 23, 2026
13 checks passed
@DemchaAV
DemchaAV deleted the perf/docx-table-export-speed branch November 23, 2026 17:21
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