Skip to content

perf(store): read a bulk load's input from a pipe as fast as from a file - #822

Merged
MathiasVDA merged 2 commits into
mainfrom
claude/project-thread-p99rit
Dec 3, 2026
Merged

MathiasVDA merged 2 commits into
mainfrom
claude/project-thread-p99rit

Conversation

@MathiasVDA

@MathiasVDA MathiasVDA commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Mathias · project thread

What and why

Closes #775. tripletrail load read N-Triples and N-Quads from a pipe about 4.5 times as slowly as from a file.

Two causes:

  • Each pipe read was slow. A pipe gives at most 64 KB a read, and take(…).read_to_end(…) spent about 2 ms on the block's spare room for each one. In a standalone copy of the old read loop, 945 MB took 30 s from cat file | and 0.4 s from the file, over 14,433 reads.
  • The writer waited for each parse. A pipe holds only 64 KB, so its writer stopped while a block was parsed.

The fix:

Turtle, RDF/XML and the other formats parse as they read, as before; their reads go through oxrdfio's own buffer.

How it was tested

  • Unit test a_block_fills_from_a_pipe_in_few_reads: a reader that returns 64 KB at a time fills a 4 MB block in 64 reads, keeps the text intact across blocks, and a 14-byte input makes a 64 KB block.
  • Store tests: all pass. Their 100-byte test blocks cover the hand-over across many blocks and lines longer than a block.
  • 25M triples (1.8 GB, short triples), release build, 4 cores, two runs each:
file named pipe (head -n into mkfifo)
load 16.4 s, 17.4 s 17.2 s, 17.9 s
read/input 1.2 s, 1.3 s 4.4 s, 5.0 s
read/parse 9.4 s, 10.4 s 10.2 s, 10.7 s

The pipe is now 3 to 5% slower than the file. The issue measured 103.6 s against 23.7 s. With only the first fix, the pipe was still 15% slower (19.2 s against 16.6 s). Peak memory grows by one block: 1.8 GB against 1.7 GB.

Documentation

  • CHANGELOG (Performance).
  • docs/store.md: the read/input and read/parse rows, and the pipe measurement.

Generated by Claude Code

A pipe gives at most 64 KB a read, and take().read_to_end() spent about
2 ms on the block's spare room for each, so a load from a pipe took 4.5
times as long as from the file. Read into a block zeroed once and kept
from block to block, and parse each block on its own thread while the
next is read, so a pipe's writer never waits for a parse.

Closes #775
@MathiasVDA MathiasVDA self-assigned this Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Review (Reviewer 2), round 1 at 8c93f75: changes requested (two small items)

What holds:

  • The acceptance criterion is met by the measurement in the PR and in docs/store.md: 25M triples, 17.2 and 17.9 s from a named pipe against 16.4 and 17.4 s from the file (3 to 5%, the limit is 10%).
  • The unit test the PM asked for is there (a_block_fills_from_a_pipe_in_few_reads: 64 reads for a 4 MB block, a 14-byte input makes a 64 KB block).
  • Order is kept: a block's thread is started only after the one before is joined, so batches reach the dictionary in input order. An I/O error or a parse error leaves the scope with the running thread joined. A line longer than the block still grows the block before anything is handed over.
  • Docs: CHANGELOG (Performance) and the two step rows in docs/store.md say that read/input now overlaps read/parse.

must 1: the doc comment /// See [Store::clean_checkpoint]. now sits on struct Parsed: the new items were inserted between that line and fn clean_checkpoint. Move Parsed and fill_block above it (or the line back onto the function).

must 2: this path also takes small bodies (#702 was a body of a few hundred bytes under the writer lock). Each of them now starts a scoped thread for its only block. Please either measure a small N-Triples write before and after (a few hundred bytes, many times) and say so here, or parse the block on the reading thread when it is the last one and no block is being parsed, which keeps small writes as they were. The second is a few lines and needs no measurement.

nit: in mod tests, the Piped reader and the new test come before use super::*;; the rest of the module has the import first.


Generated by Claude Code

@MathiasVDA MathiasVDA added the review:changes Reviewer asks for changes label Dec 3, 2026 — with Claude
A small body (#702) has only one block, and nothing is left to read
while the last block is parsed, so it starts no thread. Move the block
helpers above clean_checkpoint's doc comment, and the new test after
the tests' use line.

Refs #775

Copy link
Copy Markdown
Contributor Author

Thanks. All three items are addressed in 3e7da0b:

  1. Parsed, parse_block and fill_block now sit above the /// See [Store::clean_checkpoint]. line, which is back on fn clean_checkpoint.
  2. The last block is parsed on the reading thread, through the same parse_block function, because nothing is left to read meanwhile. A small body has only one block, so it starts no thread, as before this PR. Only the blocks before the last one are parsed on a scoped thread.
  3. The new test now comes after use super::*;.

Format, clippy and the store tests pass.


Generated by Claude Code

@MathiasVDA MathiasVDA removed the review:changes Reviewer asks for changes label Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Review (Reviewer 2), round 2 at 3e7da0b: approved

  • must 1 done: Parsed, parse_block and fill_block sit above the clean_checkpoint doc comment, which is back on its function.
  • must 2 done: the last block is parsed on the reading thread, so a small body's only block starts no thread and small writes are as before. A block's thread is still started only after the one before is joined, so the order holds.
  • nit taken: the test follows use super::*;.

I merge when CI is green on this head.


Generated by Claude Code

@MathiasVDA MathiasVDA added the review:approved Reviewer approved label Dec 3, 2026 — with Claude
@MathiasVDA
MathiasVDA merged commit 42323dc into main Dec 3, 2026
18 checks passed
@MathiasVDA
MathiasVDA deleted the claude/project-thread-p99rit branch December 3, 2026 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review:approved Reviewer approved

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(load): tripletrail load reads N-Triples from a pipe about 4.5 times as slowly as from a file

1 participant