Repository navigation
perf: decode workbook data rows compactly, without structpb - #10282
Open
nishantmonu51 wants to merge 5 commits into
Open
nishantmonu51 wants to merge 5 commits into
nishantmonu51 wants to merge 5 commits into
Conversation
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.
This branch has not been deployed
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.
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.Unmarshaldecodes each data sheet'srowsandtotalsitself 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.rows.go):strconv.ParseFloat, as protojson parses them.Unmarshalreturns aworkbook.Request(the proto plus the compact data sheets), andWritetakes it. Rows are read throughdataSheet.rowanddataRow.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 throughrowValuewould cost rows × columns².Caps
checkJSON, the pre-count over the whole body, is removed.MaxCells(1M), withvalidate's message, and at 1M row number formats.Behaviour changes to check
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:jsonCellinweb-common/src/features/exports/xlsx/collect.tsturns anything that isn't a scalar into a string.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.
UnmarshalandWriteUnmarshalandWriteUnmarshalandWriteWriteTests
TestUnmarshalParitydecodes 95 bodies with both protojson andUnmarshal. They must agree on what they accept, and on every value bit for bit. The bodies cover:-0;FuzzUnmarshalruns 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:Writetests now decode their requests throughUnmarshal.Checklist: