Skip to content

perf(writer): encode on all cores, gzip in parallel, share IRIs - #11

Merged
jgeluk merged 3 commits into
mainfrom
perf/benchmark
Nov 22, 2026
Merged

jgeluk merged 3 commits into
mainfrom
perf/benchmark

Conversation

@jgeluk

@jgeluk jgeluk commented Nov 20, 2026

Copy link
Copy Markdown
Member

Adds a reproducible benchmark and makes triplify 2.4–8× faster, with byte-identical output.

Results

scripts/bench.sh 1.0 on an Apple M3 Max with 16 cores:

Case Before After
CSV, 5M rows (215M quads) → stdout 81.7 s 11.5 s (18.7M quads/s)
same → .nq.gz 215.2 s 26.5 s
same, --profile 100.4 s 20.2 s
XLSX, 1M rows with formulas 16.5 s 5.9 s
Parquet, 10M rows (350M quads) 140.8 s 20.8 s
same, --profile 163.8 s 30.8 s
2,000 small CSVs in a git repo 0.96 s 0.42 s

What changed, in profiling order

  1. Buffered stdout. Without a buffer, every quad was a separate write system call. At 1% scale, CSV to stdout went from 39.7 s to 1.3 s.
  2. Fewer allocations. About 17 per cell before:
    • IRIs are shared Arc nodes, predicates are static, and the graph name is stored once per batch;
    • row and column nodes are cached;
    • a cell's UUIDv8 starts from a precomputed SHA-256 state for the view's prefix.
  3. Encoder thread pool. N-Quads/N-Triples are encoded by a pool of threads, so even one file uses every core.
    • The encoder writes bytes directly, without fmt.
    • A unit test checks it against oxrdf's Display, which oxttl uses as well, for every literal shape and every escaped character.
    • The writer now only moves bytes. TriG and --canonical keep the serializing writer.
  4. Parallel gzip. gzp compresses on all cores into one standard gzip member at level 6, like pigz. Files are within 0.7% of the previous size.

Verification

  • The old and new builds produce identical output: sorted N-Quads, run graph excluded, over CSV, XLSX with formulas, Parquet and the 2,000-file git repo, with --profile. That is 5,995,229 quads at 1% scale and 56,440,051 quads gzipped at 10% scale.
  • Gzip output is a single member: Python's zlib finds no trailing data, and the existing end-to-end test decodes it with flate2's single-member GzDecoder.
  • New tests for IRI equivalence on the fast paths and for encoder byte-equality. All 23 tests pass, and clippy (--lib, --bins, --tests) and fmt are clean.
  • An encoder thread panic fails the run instead of silently shortening the output.

Notes

  • New dependencies: flume (MPMC channels for sync and async producers), gzp with deflate_zlib (C zlib compiled via cc, no cmake), and itoa. The CI wheel matrix will confirm that they build on all six targets.
  • .nq.gz peak memory rose to 390 MiB, from gzp's per-thread buffers plus the queue.
  • The docs get a Performance page, and AGENTS.md asks for a benchmark plus the identical-output check on changes to the emit, encode or writer path.

🤖 Generated with Claude Code

jgeluk and others added 3 commits November 20, 2026 18:31
examples/bench_data generates deterministic CSV, XLSX, Parquet and
many-small-files inputs at a chosen scale; scripts/bench.sh reports wall
time, peak RSS and quads per second for each case.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Measured with scripts/bench.sh on an M3 Max, 5M-row CSV / 10M-row
Parquet:

- Buffer stdout: unbuffered, every quad was a write system call
  (CSV to stdout 39.7 s -> 1.3 s at 1% scale).
- Share IRIs as Arc nodes with static predicates and one graph per
  batch, cache row/column nodes, and mint cell UUIDv8s from a
  precomputed SHA-256 prefix state.
- Encode N-Quads/N-Triples on a pool of encoder threads with a direct
  byte encoder identical to oxrdf's Display, so one file uses every
  core; the writer only moves bytes.
- Compress .gz on all cores into a single standard gzip member (gzp,
  level 6).

CSV 81.7 s -> 11.5 s, CSV.gz 215 s -> 26.5 s, Parquet 141 s -> 20.8 s,
XLSX 16.5 s -> 5.9 s. Output is byte-identical to the previous release
(sorted N-Quads, run graph excluded); gzip output is a single member.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@jgeluk
jgeluk merged commit 87abdcf into main Nov 22, 2026
22 checks passed
@jgeluk
jgeluk deleted the perf/benchmark branch November 22, 2026 01:20
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