Skip to content

Performance improvements - #137

Merged
KazaiMazai merged 16 commits into
mainfrom
Performance-improvements
Jun 8, 2026
Merged

KazaiMazai merged 16 commits into
mainfrom
Performance-improvements

Conversation

@KazaiMazai

Copy link
Copy Markdown
Owner

Summary

This PR makes the entire write path O(n log n) for indexed bulk saves (and O(n) when no B-tree index is involved), with no functionality removed and a Context value-semantics bug fixed along the way.

Typical graph saves are ~5–8× faster; index-heavy bulk loads are ~16–21× faster at 5k entities and the gap widens without bound with dataset size.

All numbers below are release builds (-O), measured same-machine against the baseline commit a24c84e. (The gains are actually larger in release than in debug — the baseline's O(n²) copy-on-write is real work the optimizer can't remove, while the optimized O(1)/O(log n) paths inline and specialize well.)

Results

Relation-graph saves (at n = 1000 entities)

Workload Before After Speedup
Plain Entity (no relations) 0.196 s 0.025 s 7.8×
Entity with Relations saved as ids 0.233 s 0.038 s 6.1×
Entity with full nested entities graph 0.569 s 0.106 s 5.4×

Index heavy write costs (at n = 5000 entities)

Index Before After Speedup
@FullTextIndex 2.730 s 0.130 s 21.0×
@Unique 0.592 s 0.032 s 18.5×
@HashIndex 0.545 s 0.030 s 18.2×
No index (storage + metadata) 0.401 s 0.025 s 16.0×

This is how the gains compound with scale. Before was O(n) per insert (quadratic in bulk); after is per-insert O(1) or O(log n). So the multiplier grows with the dataset:

Path n Before After Speedup
Storage + metadata 5,000 0.401 s 0.025 s 16×
Storage + metadata 20,000 6.049 s 0.098 s 62×
Full-text 5,000 2.730 s 0.130 s 21×
Full-text 20,000 45.46 s 0.566 s 80×

The optimized path stays near-flat per entity (~5 µs/save storage, ~28 µs/save full-text) at both 5k and 20k, while the baseline grows quadratically — so the multiplier keeps widening with the dataset.

Reads also improved

The typed Storage<T> boxes + ObjectIdentifier keys also speed up entity resolution (find / findAll / all), so reads that materialize many entities got faster too. Narrow indexed lookups (a handful of ids) were already sub-millisecond and are unchanged.

Read n Before After Speedup
Non-indexed filter (full scan) 5,000 0.010 s 0.004 s ~2.5×
Raw filter (resolve-all then .filter) 5,000 0.007 s 0.003 s ~2.3×

(Millisecond-scale timings at high RSD, so treat these as directional. The reliable signal: resolve-dominated reads — full scans that materialize every entity — get ~2.3–2.5× from the typed-storage/ObjectIdentifier changes; the indexed sort resolves the full set too but its B-tree traversal dilutes the win, and index-served narrow reads were already sub-millisecond and stay flat.)

Why it was slow

Every index update did resolve → mutate → save, and the storage did copy-out / mutate / copy-back. Both leave the backing collection shared at mutation time, forcing a full O(n) copy-on-write on every insert — masquerading as O(1). The same pattern lived in five places: EntitiesRepository, Index.HashableValue, Index.ComparableValue, the Unique.* indexes, and FullTextIndex.HashableValue. On top of that, .none relations were writing empty Link nodes that did nothing, the updatedAt metadata wrote two indexes per save when only one is ever read, and every storage access resolved its key by reflection (String(reflecting:)).

What changed

Per-insert cost now follows the index's data structure:

Per insert Bulk Writes
O(1) amortized (hash-backed) O(n) entity storage, @HashIndex, @FullTextIndex, Hashable-only @Unique
O(log n) (B-tree Map) O(n log n) @Index, Comparable @Unique, and the always-on updatedAt metadata index

The log n is BTree depth. The only universal contributor is updatedAt metadata (one B-tree write per save by default) — so with metadata disabled and hash-only indexes the path is genuinely O(n).

Changes Fix
relations write optimisation Skip empty/no-op Link writes for .none relations
entity repository CoW optimization In-place storage mutation (subscript(_:default:)) — O(n²)→O(1)/insert
non copying mutation helpers Context.mutate / mutateIfPresent — take unique ownership, mutate in place
migrated all indexes to structs All indexes are value types using the mutate helpers
typed storage Storage<T> boxes — one as? cast per lookup instead of per element
ObjectIdentifier keys Storage keyed by type identity — no reflection, no string, faster hashing
removed odd hashable metadata index updatedAt writes only the B-tree (for sort/range); dropped the redundant, never-read hash index

Tests

Value-type indexes fixed a latent Context value-semantics bug: index storage was reference-shared, so copying a Context and mutating one copy silently corrupted the other's indexes. Now copies are independent (verified by test). All 382 tests pass, including the index / collision / full-text / value-semantics suites; reads still hit every index (indexed filters and sorts remain faster than scans).

@KazaiMazai KazaiMazai self-assigned this Jun 8, 2026
@KazaiMazai
KazaiMazai merged commit d6adb11 into main Jun 8, 2026
1 check passed
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