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

---

LLMS index: [llms.txt](/llms.txt)

---

`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](https://github.com/pgsty/acdat/blob/v0.1.1/docs/PERFORMANCE.md) · [BENCHMARK.md](https://github.com/pgsty/acdat/blob/v0.1.1/docs/BENCHMARK.md).

## 1. Real corpus throughput (10.3M rows, 1,114 rules) {#real-corpus}

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

```echarts {height="340px"}
backgroundColor: transparent
tooltip:
  trigger: axis
  axisPointer:
    type: shadow
legend:
  top: 6
grid:
  left: 16
  right: 20
  top: 48
  bottom: 16
  containLabel: true
xAxis:
  type: category
  data: ["contains", "replace", "matches"]
yAxis:
  type: value
  name: "MiB/s"
series:
  - name: "1 backend"
    type: bar
    barMaxWidth: 36
    itemStyle:
      color: "#94a3b8"
      borderRadius: [4, 4, 0, 0]
    label:
      show: true
      position: top
    data: [202, 153, 83]
  - name: "4 workers"
    type: bar
    barMaxWidth: 36
    itemStyle:
      color: "#3b82f6"
      borderRadius: [4, 4, 0, 0]
    label:
      show: true
      position: top
    data: [772, 602, 376]
  - name: "8 workers"
    type: bar
    barMaxWidth: 36
    itemStyle:
      color: "#10b981"
      borderRadius: [4, 4, 0, 0]
    label:
      show: true
      position: top
    data: [1346, 1055, 645]
```

| 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 {#row-width}

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.

```echarts {height="340px"}
backgroundColor: transparent
tooltip:
  trigger: axis
  axisPointer:
    type: shadow
legend:
  top: 6
grid:
  left: 16
  right: 20
  top: 48
  bottom: 16
  containLabel: true
xAxis:
  type: category
  data: ["33 B / row (10M rows)", "1 KiB / row (262k rows)", "64 KiB / row (4k rows)"]
yAxis:
  type: value
  name: "GiB/s"
series:
  - name: "1 backend"
    type: bar
    barMaxWidth: 36
    itemStyle:
      color: "#94a3b8"
      borderRadius: [4, 4, 0, 0]
    label:
      show: true
      position: top
    data: [0.51, 0.89, 0.89]
  - name: "4 workers"
    type: bar
    barMaxWidth: 36
    itemStyle:
      color: "#3b82f6"
      borderRadius: [4, 4, 0, 0]
    label:
      show: true
      position: top
    data: [2.19, 3.41, 3.55]
  - name: "8 workers"
    type: bar
    barMaxWidth: 36
    itemStyle:
      color: "#10b981"
      borderRadius: [4, 4, 0, 0]
    label:
      show: true
      position: top
    data: [2.94, 5.01, 5.13]
```

| 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 {#parallel-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:

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

```sql
-- 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 {#sizing}

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

---

Backlinks:

- [Getting started](/docs/acdat/getting-started/)
- [Operations](/docs/acdat/operations/)
