# 性能参考与调优

> acdat v0.1.1 实测吞吐与内存数据，如何避开慢 22 倍的并行扫描执行计划陷阱，以及大词库容量规划要点。

---

LLMS 索引： [llms.txt](/zh/llms.txt)

---

`acdat` 的编译和核心扫描接近内存带宽上限。在真实业务中，决定查询耗时的通常不是自动机本身，而是执行计划、单行宽度与并行工作进程数。

以下基准数据均测自 `acdat` v0.1.1（Apple M5 Max、PostgreSQL 18.6、热缓存、库内聚合，不含客户端传输）。完整环境、复现命令与对照分析见规格：[Go 与 PostgreSQL 性能对照](/zh/spec/acdat/performance/)、[基准测试记录](/zh/spec/acdat/benchmark/)。

## 1. 真实语料吞吐对比（1,030 万行，1,114 条规则） {#real-corpus}

数据集：10,318,163 条真实短标题（共 306.9 MiB，约 31 字节/行），匹配 1,114 条真实词典规则（编译仅 1.16 ms，单进程运行时缓存 227 KiB）。

```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: "单后端"
    type: bar
    barMaxWidth: 36
    itemStyle:
      color: "#94a3b8"
      borderRadius: [4, 4, 0, 0]
    label:
      show: true
      position: top
    data: [202, 153, 83]
  - name: "4 并行"
    type: bar
    barMaxWidth: 36
    itemStyle:
      color: "#3b82f6"
      borderRadius: [4, 4, 0, 0]
    label:
      show: true
      position: top
    data: [772, 602, 376]
  - name: "8 并行"
    type: bar
    barMaxWidth: 36
    itemStyle:
      color: "#10b981"
      borderRadius: [4, 4, 0, 0]
    label:
      show: true
      position: top
    data: [1346, 1055, 645]
```

| 操作 | 单后端耗时 | 4 并行耗时 | 8 并行耗时 | 8 并行加速比 |
| --- | ---: | ---: | ---: | ---: |
| `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` 命中首处即短路返回，无需构造结果字符串或匹配元组，在短行场景下比 `matches` 快约 2.4 倍。

## 2. 十万条规则与行宽敏感度 {#row-width}

数据集：100,000 条共享前缀规则（编译 36.9 ms，单进程运行时缓存 5.51 MiB），扫描 256–314 MiB 根节点失配文本。

```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/行（1,000 万行）", "1 KiB/行（26 万行）", "64 KiB/行（4,096 行）"]
yAxis:
  type: value
  name: "GiB/s"
series:
  - name: "单后端"
    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 并行"
    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 并行"
    type: bar
    barMaxWidth: 36
    itemStyle:
      color: "#10b981"
      borderRadius: [4, 4, 0, 0]
    label:
      show: true
      position: top
    data: [2.94, 5.01, 5.13]
```

| 单行宽度 | 总行数 | 单后端耗时 | 4 并行耗时 | 8 并行耗时 |
| --- | ---: | ---: | ---: | ---: |
| 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 |

每行达到 1 KiB 或 64 KiB 时，单后端吞吐达到独立 C 匹配核心基线（965 MiB/s）的约 94%；而在 33 字节短行上，执行器逐行函数调用边界占用了约一半开销。

## 3. 避开慢 22 倍的并行 `matches` 计划陷阱 {#parallel-trap}

把仅含一行的自动机表直接与文档表 `CROSS JOIN` 时，并行优化器可能把自动机表放进内层循环，每扫一行文档就重扫一次自动机表：

```sql
-- 慢写法：并行 Gather 下可能逐行重扫自动机表（8 并行耗时 10.43 s）
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;
```

改用**不相关标量子查询**让优化器生成只执行一次的 `InitPlan`，同样 1,030 万行、8 并行下耗时直接从 **10.43 秒** 降到 **0.476 秒**（快 22 倍）：

```sql
-- 快写法：不相关标量子查询走 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;
```

对大表上线新查询前，务必运行 `EXPLAIN (ANALYZE, BUFFERS)`，确认读取自动机的计划节点 `loops=1`。

## 4. 内存预算与四条调优原则 {#sizing}

每个后端及每个并行工作进程都会缓存一份物化后的运行时自动机。规划大词库时，按 `(1 + max_parallel_workers_per_gather)` 估算峰值缓存占用：

| 词典构成 | 存储产物 | 单进程缓存 | 9 进程预算（主进程 + 8 并行） |
| --- | ---: | ---: | ---: |
| 真实语料（1,114 条规则） | 269 KiB | 227 KiB | 约 2.0 MiB |
| 10 万条共享前缀规则 | 5.94 MiB | 5.51 MiB | 约 49.6 MiB |
| 10 万条随机 ASCII 规则 | 28.96 MiB | 23.93 MiB | 约 215.4 MiB |

日常调优牢记四条原则：

- **绝不在行循环中重新编译**：永远不要在逐行求值的 `WHERE` 或 `SELECT` 里调用 `acdat.compile()`。
- **先过滤再扫描**：先用 B-tree、GIN 或时间范围索引缩小候选行；仅需判断有无命中时优先用 `acdat.contains()` 而非 `acdat.matches()`。
- **按真实词典与语料复测**：根节点快速失配与高频失配跳转的吞吐差距可达十倍以上，不能只凭规则条数外推吞吐。
- **大结果集使用流式导出**：导出数百万条命中或替换结果时，使用 `COPY` 或服务端游标，避免普通 `SELECT` 把完整结果塞爆客户端内存。

---

反链：

- [快速上手](/zh/docs/acdat/getting-started/)
- [运维手册](/zh/docs/acdat/operations/)
