Performance and tuning
acdat compiles fast and scans near memory bandwidth. In production, query latency is usually governed by PostgreSQL’s execution plan, row width, and parallel worker count rather than the automaton itself.
All benchmark figures below were measured on acdat v0.1.1 (Apple M5 Max, PostgreSQL 18.6, hot buffers, in-database aggregation without client transfer). Full methodology and raw logs: PERFORMANCE.md · BENCHMARK.md.
1. Real corpus throughput (10.3M rows, 1,114 rules)
Dataset: 10,318,163 short video titles (306.9 MiB total, ~31 bytes/row) scanned against a 1,114-rule real dictionary (compiled in 1.16 ms, 227 KiB runtime cache per process).
| Operation | 1 backend | 4 workers | 8 workers | 8-worker speedup |
|---|---|---|---|---|
contains | 1.522 s | 0.398 s | 0.228 s | 6.67x |
replace (leftmost_longest) | 2.007 s | 0.510 s | 0.291 s | 6.89x |
matches (all_overlapping) | 3.678 s | 0.816 s | 0.476 s | 7.73x |
contains stops on the first match and allocates no output text or hit tuples, making it ~2.4x faster than matches on short rows.
2. Scaling to 100,000 rules across row widths
Dataset: 100,000 shared-prefix patterns (compiled in 36.9 ms, 5.51 MiB runtime cache) scanning 256–314 MiB of root-miss text across three row sizes.
| Row width | Rows | 1 backend | 4 workers | 8 workers |
|---|---|---|---|---|
| 33 B | 10,000,000 | 600.6 ms | 140.1 ms | 104.0 ms |
| 1 KiB | 262,144 | 281.6 ms | 73.3 ms | 49.9 ms |
| 64 KiB | 4,096 | 280.4 ms | 70.5 ms | 48.8 ms |
At 1 KiB and 64 KiB per row, a single PostgreSQL backend reaches ~94% of the standalone C core’s throughput (965 MiB/s). On 33-byte rows, executor per-row call overhead accounts for roughly half of runtime.
3. Avoid the 22x parallel matches trap
Joining a single-row machine table directly with CROSS JOIN can cause the parallel planner to place the machine scan inside the document loop—re-reading the machine row once per document:
Wrapping the lookup in an uncorrelated scalar subquery turns it into a once-per-query InitPlan. On the 10.3M-row corpus with 8 workers, runtime dropped from 10.43 s to 0.476 s (22x faster):
Always check EXPLAIN (ANALYZE, BUFFERS) before rolling out a large scan and verify that the machine scan runs with loops=1.
4. Memory budget and tuning rules
Each backend and parallel worker keeps its own materialized runtime cache. Budget (1 + max_parallel_workers_per_gather) copies for large dictionaries:
| Dictionary profile | Stored artifact | Cache / process | 9 processes (leader + 8 workers) |
|---|---|---|---|
| Real corpus (1,114 rules) | 269 KiB | 227 KiB | ~2.0 MiB |
| 100k shared-prefix rules | 5.94 MiB | 5.51 MiB | ~49.6 MiB |
| 100k random ASCII rules | 28.96 MiB | 23.93 MiB | ~215.4 MiB |
Four practical rules for fast scans:
- Compile once outside the row loop: Never put
acdat.compile()inside a per-rowWHEREorSELECTexpression. - Filter before scanning: Narrow candidate rows with B-tree, GIN, or time-range indexes first, and choose
acdat.contains()overacdat.matches()when you only need a boolean match. - Benchmark your own dictionary: Root-miss workloads run much faster than dense-fallback ASCII workloads; rule count alone does not predict throughput.
- Stream large exports: Use
COPYor a server-side cursor when exporting millions of matches or rewritten rows instead of buffering a giantSELECTin client memory.