Reduce LadybugDB publication time and memory with typed node COPY #40
Labels
No labels
area/cli
area/core
area/database
area/indexing
area/query
type/epic
type/feature
type/foundation
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
JayKobe6k/ClariTree#40
Loading…
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem and impact
ClariTree writes source-anchor and semantic value nodes through prepared
UNWINDparameter batches even though value relationships already use LadybugDB CSVCOPY. On a representative 40-translation-unit workload, node and anchor publication account for most persistence time and retain substantially more native memory. This makes index creation and update slower and raises OOM pressure without improving correctness.Environment and workload
654b2fbcf8e3ccc7ce7428b637d8b2736f67de19lbug0.20.2melee-event-matchat6b6f8a8a987c2e871b8b96baa863aaa99e406f33tests/profile_lineage.rsMemoryMax=8G; 200 TUMemoryMax=16G;MemorySwapMax=0; explicit runtime limitsReproduction after building the release profile test:
Verify
/tmpuser quota as well as free space, or setTMPDIRbeforenix develop; one 200-TU repeat was excluded after an environmentalDisk quota exceededcheckpoint failure.Baseline and profiling evidence
The 40-TU baseline persisted 28,375 definitions, 28,847 uses, 62,693 lineage relationships, and 729,601 source facts in 50.960 s, peaking near 5.6 GiB. Phase timing localized approximately 16.948 s to source nodes, 14.183 s to source relationships, 8.508 s to definitions, and 7.711 s to uses; validation was under approximately 1.4 s.
A typed-COPY candidate produced identical counts in three no-swap runs:
The 28.315 s median is 44.4% below baseline and peak memory is approximately 63% lower. A comparable 200-TU tmpfs run fell from 318.381 s / 7,597,436 KiB to 201.081 s / 5.7G, a directional 36.8% time reduction and roughly 25% lower peak. Two root-disk candidate runs completed in 261.530 s and 316.149 s at 5.7G but have no matching root-disk baseline and are not used for the improvement claim.
Root cause
UNWINDbatches: src/persistence/ladybug.rs lines 3501-3600.LIST<STRUCT>and checkpoints each batch: src/persistence/ladybug_source.rs lines 1377-1426.The causal mechanism is repeated construction and native conversion of very wide parameter structs plus less efficient per-row insertion. Bounded CSV COPY removes that conversion path and releases each temporary file after one configured chunk.
Proposed implementation
SourceAnchor,ValueDefinition,ValueUse, and all four source-anchor endpoint families in configured bounded chunks.Some("")versusNULL; double embedded quotes and preserve commas, newlines, backslashes, Unicode, and BLOB\xNNbytes. Disable COPY auto-detection and set delimiter, quote, escape, header, and parallel options explicitly.Alternatives considered:
Expected benefit, confidence, and risks
Acceptance criteria
Related: #38 owns streaming typed mapping; #39 owns buffer-pool configuration.
The implementation candidate now passes the complete required validation through the pinned Nix environment:
cargo fmt --all -- --checkcargo lintcargo test --all-targets --all-features(274 unit tests plus every integration suite)cargo doc --no-deps --all-featurescargo check --all-targets --all-features --target x86_64-pc-windows-gnugit diff --checkReview found and fixed a null/empty-string CSV distinction defect. Present fields are now always quoted while absent fields remain unquoted; adversarial tests cover empty strings, nulls, commas, quotes, newlines, backslashes, Unicode, and fingerprint BLOB encoding. An initially added standalone native database test caused parallel-suite interference and was removed; the existing value-node database test now verifies the distinction without adding another concurrent LadybugDB instance. A fresh post-fix review found no remaining actionable defect.
The candidate remains uncommitted in the local worktree; no commit or push has been claimed.
Implemented and pushed to
mainas32959f55abed0b7bd92d94670fcf9c9abb452a1e.The delivered implementation retains the measured 40-TU median improvement (50.960 s to 28.315 s, 44.4%) and approximately 63% lower peak memory, with exact count parity. Full required Nix validation passed before commit, and commit hooks passed formatting and linting.