Skip to content

perf: decode workbook data rows compactly, without structpb - #10282

Open
nishantmonu51 wants to merge 5 commits into
nishant/workbook-stream-dashboard-sheetsfrom
nishant/workbook-compact-row-decode
Open

nishantmonu51 wants to merge 5 commits into
nishant/workbook-stream-dashboard-sheetsfrom
nishant/workbook-compact-row-decode

Conversation

@nishantmonu51

Copy link
Copy Markdown
Collaborator

A workbook export's data rows and totals are now decoded into a compact form instead of structpb, so decoding a body takes about as much memory as the body itself. The pre-count of values and objects over the whole body is removed; the caps that remain count only what protojson still decodes.

Stacked on #10281, which is stacked on #10275. The first commit is the change; the others add the parity and fuzz tests, store numbers as float64s, and stop decoding at the first value past a cap.

Decoding

  • workbook.Unmarshal decodes each data sheet's rows and totals itself and blanks them out of the body; protojson then decodes the rest, as before. Line breaks are kept, so protojson's error positions still match the body.
  • A sheet's rows are one string (see rows.go):
    • each cell is a tag byte followed by the number's float64 bits or the string's bytes;
    • each row's end is stored in 4 bytes.
  • Numbers are parsed once, with strconv.ParseFloat, as protojson parses them.
  • Unmarshal returns a workbook.Request (the proto plus the compact data sheets), and Write takes it. Rows are read through dataSheet.row and dataRow.cells.
  • sheetFormats (from fix: bound the CPU cost of conditional formats in canvas and Explore Excel exports #10275) reads the rows once for all the columns it scans. Reading one column at a time through rowValue would cost rows × columns².
  • The protos and the wire format are unchanged.

Caps

  • checkJSON, the pre-count over the whole body, is removed.
  • Decoding stops at the first row or cell past MaxCells (1M), with validate's message, and at 1M row number formats.
  • Values and keys outside the rows, totals and row number formats are capped at 500k. Before, 2.5M were allowed over the whole body, 2M of it to leave room for cells. Protojson still decodes these parts, at about 80 bytes per value.
  • Objects are still capped at 100k.

Behaviour changes to check

  • A row or totals cell holding an array or an object now gets a 400 (failed to parse request: a cell of row N of data sheet M holds an array or an object, which a workbook cell can't hold). Before, it was written as JSON text. The web app never sends one: jsonCell in web-common/src/features/exports/xlsx/collect.ts turns anything that isn't a scalar into a string.
  • A body with more than 500k values outside its rows is now rejected even when it has few rows. Before, up to 2.5M passed. Requests from the web app hold a few thousand. Raising the cap back to 2.5M would restore the old behaviour, at the cost of the worst such body going back to about 230 MB.
  • A syntax error inside the rows or totals now reads failed to parse request: invalid JSON at byte N: … instead of protojson's message. Every other error is unchanged.

Before/after. This PR against #10281, on a shared host. Heap is measured above the body already read.

Request #10281 This PR
1M cells in one-column rows (11.4 MB), the largest request the caps allow: decode 176 MB peak, 154 MB held, 427 ms 12 MB peak, 12 MB held, 76 ms
The same, through Unmarshal and Write 384 MB peak 159 MB peak
1M cells in ten-column rows (8.8 MB), through Unmarshal and Write 244 MB peak 105 MB peak
A realistic canvas export (4.3 MB), through Unmarshal and Write 91 MB peak 42 MB peak
495k KPI values plus 1M cells (64 MB): decode 218 MB peak 122 MB peak, 61 MB of it a title the request keeps
64 MB of number cells, past the caps 400 in 70 ms 400 in 6 ms
64 MB of empty-list cells decoded, 295 MB peak 400 in under 1 ms
1,000 conditional formats over 5,000 × 200 cells: Write 369 ms, 213 MB peak 335 ms, 174 MB peak

Tests

  • TestUnmarshalParity decodes 95 bodies with both protojson and Unmarshal. They must agree on what they accept, and on every value bit for bit. The bodies cover:
    • camelCase and proto names, and escaped keys;
    • every string escape, surrogate pairs, and invalid UTF-8;
    • numbers past 2^53, and -0;
    • duplicate and unknown fields, and truncated bodies.
  • FuzzUnmarshal runs the same check from those seeds; CI runs only the seeds. Three 60 s runs locally, about 4.2M executions in all, found nothing.
  • TestUnmarshal:
    • bodies past each cap, up to 64 MB, are rejected without allocating;
    • the largest requests decode under allocation ceilings: 12.5 MB for the 11.4 MB body.
  • The existing Write tests now decode their requests through Unmarshal.

Checklist:

  • Covered by tests
  • Ran it and it works as intended
  • Reviewed the diff before requesting a review
  • Checked for unhandled edge cases
  • Linked the issues it closes
  • Checked if the docs need to be updated. If so, create a separate Linear DOCS issue
  • Intend to cherry-pick into the release branch
  • I'm proud of this work!

Protojson holds every cell of a data row as a structpb.Value of tens of bytes,
so a body of data rows decoded to many times its size,
and Unmarshal counted the body's values before decoding it to bound that.

Unmarshal now decodes the rows and totals itself,
into one string per data sheet with a tag byte per cell and a 4-byte end per row,
blanks them out of the body, and lets protojson decode the rest.
It decodes cells as protojson's JSON decoder does,
and rejects a cell holding an array or an object, which the browser never sends.
It stops at the first row or cell past MaxCells, which validate rejects anyway,
so the caps on values and objects now count only what protojson decodes.

Write takes the decoded Request; the row readers go through dataSheet.row and dataRow.cells.

sheetFormats reads the rows once for all the columns whose numbers set a color scale or data bar's ends,
since rowValue walks a row up to its column, and scanning column by column would cost rows × columns².
A number cell held its JSON text, which every read parsed again:
the data sheet reads each cell twice and a conditional format's domain once more.
It now holds its float64 bits after its tag, parsed once when the body is decoded.
A typical number takes about as many bytes as its JSON text did.
A row, an array of row number formats or a value outside the rows was scanned to its end before its count was checked,
so a 64 MB body of one of them took up to a quarter of a second to reject.
@nishantmonu51 nishantmonu51 added Area:Performance Type:Improvement Size:L Large change: 500-1,999 lines Area:Canvas Canvas dashboards labels Dec 5, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Area:Canvas Canvas dashboards Area:Performance Size:L Large change: 500-1,999 lines Type:Improvement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant