Skip to content

perf: append measurements to JSON and XML instead of rewriting - #86

Merged
firefly-cpp merged 1 commit into
mainfrom
perf/append-json-xml
Nov 21, 2026
Merged

firefly-cpp merged 1 commit into
mainfrom
perf/append-json-xml

Conversation

@firefly-cpp

Copy link
Copy Markdown
Owner

Second part after #85: JSON and XML were still read and written again as a whole on every POST /measure.

How it works

Both files are a single document, so a row cannot simply be added at the end. Instead:

  • JSON: the file is [ + records + ]. The new record is created with the same call as before (DataFrame.to_json(orient='records', indent=4)) and inserted before the closing ], separated by ,.
  • XML: the file is the declaration + <data> + <row> elements + </data>. The new <row> is created with DataFrame.to_xml(index=False) and inserted before the closing </data>.

The result is byte for byte the same file as writing everything again (tested, including values with ], </data>, <&>, quotes, non-ASCII characters and very long values), so nothing changes for anyone reading these files.

Only small parts of the file are read:

  • the first record for the column names (JSON: incremental raw_decode of the first object, up to 1 MB; XML: iterparse stopped after the first <row>);
  • the last 4 KB to find the closing ] / </data>.

Falls back to writing the whole file once (as before) when

  • the file is new or empty, or an empty JSON array [];
  • the configured columns are not all in the file (e.g. a column was added, or timestamps enabled later); a removed column is kept and left empty, without a rewrite;
  • the file does not start/end as expected (e.g. not a <data> document, no } / </row> before the closing token).

Compact JSON ([{...},{...}], e.g. written by another tool) is appended to as well, and stays valid. Line endings follow the file (\r\n if it was written on Windows).

Speed (per measurement)

Stored rows JSON XML
1,000 1.9 ms 4.1 ms
10,000 1.3 ms 3.3 ms
100,000 1.6 ms 3.7 ms
1,000,000 2.0 ms ~3 ms

Before: 45 ms (JSON) and 246 ms (XML) with only 10,000 rows. (A first XML run at 1M rows showed 84 ms; that was the benchmark itself keeping a 1M-row DataFrame alive, which made Python's garbage collector slow. Measured per request without it: 3–5 ms.)

Tests

  • tests/test_storage.py: shared tests for both formats (same file as a full write, stored data not read, files from older versions, new and removed columns, trailing whitespace, Windows line endings, empty file) plus JSON-specific (compact JSON, nested values with } in the first record, empty array) and XML-specific (file stays valid XML) tests. 9 of them fail on the previous code; the "same file" test fails there because the full rewrite re-formatted earlier values.
  • Full suite: 124 passed. ruff check passes.

Generated by Claude Code

JSON and XML files were still read and written again as a whole on
every measurement (after #85 changed CSV and SQLite).

- Insert the new record before the closing `]` (JSON) or `</data>`
  (XML), written with the same pandas functions as before, so the file
  stays valid and is byte for byte the same as if it had been written
  again; about 2 ms (JSON) and 3 ms (XML) per measurement with
  1,000,000 stored rows
- Only the first record (for the column names) and the end of the file
  are read; the XML file is parsed incrementally up to its first row
- Fall back to writing the whole file once when the file is new or
  empty, the configured columns changed, or the file does not end as
  expected; compact JSON written by other tools is also supported
- Keep the file's line endings (e.g. \r\n written on Windows)
- Document it and add tests

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VCvw8haRnmyrVKrLkz5Yf8
@firefly-cpp
firefly-cpp merged commit 9110746 into main Nov 21, 2026
16 checks passed
@firefly-cpp
firefly-cpp deleted the perf/append-json-xml branch November 21, 2026 08:44
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.

2 participants