Skip to content

Performance and tuning

Reference throughput and memory measurements for acdat v0.1.1, how to avoid the 22x parallel-scan planning trap, and sizing rules for production rule sets.

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).

Operation1 backend4 workers8 workers8-worker speedup
contains1.522 s0.398 s0.228 s6.67x
replace (leftmost_longest)2.007 s0.510 s0.291 s6.89x
matches (all_overlapping)3.678 s0.816 s0.476 s7.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 widthRows1 backend4 workers8 workers
33 B10,000,000600.6 ms140.1 ms104.0 ms
1 KiB262,144281.6 ms73.3 ms49.9 ms
64 KiB4,096280.4 ms70.5 ms48.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:

-- SLOW: parallel Gather can rescan active_dict once per document row (10.43 s on 8 workers)
SELECT count(*)
FROM documents AS d
CROSS JOIN active_dict AS m
CROSS JOIN LATERAL acdat.matches(d.body, m.machine, 'all_overlapping') AS h;

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):

-- FAST: uncorrelated scalar subquery loads the machine once via InitPlan (0.476 s)
SELECT count(*)
FROM documents AS d
CROSS JOIN LATERAL acdat.matches(
    d.body,
    (SELECT machine FROM acdat.active_machine WHERE name = 'security_ioc'),
    'all_overlapping'
) AS h;

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 profileStored artifactCache / process9 processes (leader + 8 workers)
Real corpus (1,114 rules)269 KiB227 KiB~2.0 MiB
100k shared-prefix rules5.94 MiB5.51 MiB~49.6 MiB
100k random ASCII rules28.96 MiB23.93 MiB~215.4 MiB

Four practical rules for fast scans:

  • Compile once outside the row loop: Never put acdat.compile() inside a per-row WHERE or SELECT expression.
  • Filter before scanning: Narrow candidate rows with B-tree, GIN, or time-range indexes first, and choose acdat.contains() over acdat.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 COPY or a server-side cursor when exporting millions of matches or rewritten rows instead of buffering a giant SELECT in client memory.