perf(api): SSE 导航层返工 —— 5+ 次边界压成 1 次(三项赢,仍默认关闭)

按 sse-codec-c.md §6.4 的架构改造方向返工。**部分成功**:从「五项全输」
变成「三项赢 / 一项持平 / 一项输」,且所有场景分配数都下降。

## 改造内容
| 项 | 前 | 后 |
|---|---|---|
| cgo 边界次数 | 5+(每字段一次 findKey) | 1(ha_sse_chunk_locate) |
| 键查找 | 每键各扫一遍对象(6 趟) | 单趟分派(遍历成员表一次即分发) |
| 解码 | 每字段一次往返 + 各自 decBuf | 同一趟内写进一块 sbuf(1 次分配) |
| 成员表遍历 | 6 趟 | 2 趟(顶层 + delta) |

顺带修掉两处自造的浪费(都是「先扫一遍拿个数、再扫第二遍拿首元素」):
choices 数组的「数个数 + 取首元素」合一趟;choice0 内的 delta/finish_reason
合一趟。成员遍历实测 107ns/趟,省一趟就是省 107ns。

新增 ha_sse_chunk_locate:一次调用完成根校验 + 顶层分派 + choices[0] +
delta 分派 + content/reasoning/finish 解码,输出写调用方持有的 C 结构体
(C 结构体无 Go 指针 ⇒ 可安全传指针,消除 out-param 逃逸)。
choices_count>1 时直接回退(Go 侧 Unmarshal 会解析全部元素,本层只认 [0],
其余元素可能类型不符而让 Go 整块作废 ⇒ 无法保证等价)。

## 实测(50000 次 × 3 轮取中位)
| 场景 | Entry | GoOnly | 判定 |
|---|---|---|---|
| content_zh | 1540ns / 5allocs | 1871ns / 13allocs | 快 18%,分配 -62% |
| content_ascii | 1250ns / 5allocs | 1304ns / 13allocs | 持平,分配 -62% |
| finish | 820ns / 6allocs | 921ns / 12allocs | 快 11% |
| usage | 2530ns / 9allocs | 2591ns / 12allocs | 持平偏快 |
| toolcall | 3450ns / 20allocs | 3000ns / 21allocs | 慢 15% |

## toolcall 仍输的根因(已定位,非猜测)
分解测量:C 侧纯 C 零边界 = 766ns;Go 侧 []openAIToolCall unmarshal =
1305ns/15allocs;对照 Go 整块 unmarshal ≈ 2980ns。
问题在第二行:tool_calls 元素是对象,Arguments interface{} 需要真实的
map[string]interface{},必须走 encoding/json 的反射建树。
而为了定位已先做了一遍 C 扫描 ⇒ 同一份数据被解析了两次。
⇒ 不是 C 慢,是「扫两遍 vs 扫一遍」。
标量字段(content/reasoning/finish)C 能一次到位 ⇒ 那些场景赢;
需要建树的字段(tool_calls/usage)C 的定位是纯开销。

## 为什么仍默认关闭(理由充分,不是保守)
1. toolcall 是真实负载最常见的一类块(任何一次工具调用流),仍慢 15%
2. 18% 收益不足以抵消「与 encoding/json 语义并存的第二实现」的风险
3. 本刀原始动机在 toolcall 场景没有兑现:分配数 20 vs 21 几乎没降
⇒ 前提是先做「按字段类型决定是否 C 化」,让 toolcall 也不输,再重测。

## 正确性
6 万+ 差分用例(协议形态/真实负载/随机 JSON 3 万/随机字节 3 万)全过。
基准测量也修了:先前 C 基准脚本用 CLOCK_MONOTONIC 却只取 tv_nsec,
算出 -4201ns 的负值 —— 测量工具本身出错会直接毁掉结论。

## 验证
ASan+UBSan PASS;gcc+clang 零告警;arm64 交叉 0 告警;
libFuzzer 66 万次零崩溃;全量 go test 38 包 ok / 0 FAIL
This commit is contained in:
JianFeeeee
2026-09-26 10:41:52 +08:00
parent 7748ec450e
commit 7a1322c97f
5 changed files with 742 additions and 190 deletions

View File

@ -118,6 +118,11 @@ static int go_arr_all(const char *p, size_t n, ha_span *out, int cap) {
return count;
}
static int go_chunk_locate(const char *p, size_t n, ha_chunk_out *out,
char *sbuf, size_t scap, size_t *sused) {
return ha_sse_chunk_locate(p, n, out, sbuf, scap, sused);
}
static int go_sse_abi(void) { return ha_sse_abi_version(); }
*/
import "C"
@ -326,3 +331,134 @@ func scanArray(arr strSpan) ([]strSpan, bool) {
}
return out, true
}
// ---------------------------------------------------------------------
// 批量定位(第三刀的重做:一次 cgo 调用代替 5+ 次)
// ---------------------------------------------------------------------
// chunkLocateResult 是 C 侧 ha_chunk_out 的 Go 视图。
type chunkLocateResult struct {
status int
// 原始 span(用于交回 encoding/json 的那些字段)
usageSpan strSpan
usageKind int
toolCallsSpan strSpan
toolCallsKind int
// C 已解码的字符串(sbuf 的副本)
content string
reasoning string
finish string
// 标志
choicesPresent bool
choicesKind int
choicesCount int
choice0Span strSpan
hasDelta bool
deltaKind int
contentKind int
reasoningKind int
finishKind int
}
// 槽位/类型常量(与 ha_sse.h 保持一致;改动必须同步 ABI 版本)
const (
slotDelta = 0
slotContent = 1
slotReasoning = 2
slotToolCalls = 3
slotFinishReason = 4
slotUsage = 5
slotCount = 6
kindAbsent = 0
kindNull = 1
kindString = 2
kindObject = 3
kindArray = 4
kindOther = 5
chunkOK = 0
chunkFallback = -1
chunkTypeFail = -2
)
// locateChunkBatch 一次调用完成整块定位。
func locateChunkBatch(data string) chunkLocateResult {
var out chunkLocateResult
if len(data) == 0 {
out.status = chunkFallback
return out
}
p, n := cstr(data)
var co C.ha_chunk_out
// ★ 单块缓冲:整块解码输出(content+reasoning+finish)都写这一块。
// 尺寸按输入上界(每字节最坏 3 字节 U+FFFD)——1 次分配,
// 替代原来「每个字段一次 decBuf」的多次分配。
sbuf := make([]byte, len(data)*3+16)
var used C.size_t
st := C.go_chunk_locate(p, n, &co, cstrb(sbuf), C.size_t(len(sbuf)), &used)
out.status = int(st)
if st != C.int(chunkOK) {
return out
}
out.usageKind = int(co.slot[slotUsage].kind)
out.usageSpan = strSpan{co.slot[slotUsage].span.p, co.slot[slotUsage].span.len}
out.toolCallsKind = int(co.slot[slotToolCalls].kind)
out.toolCallsSpan = strSpan{co.slot[slotToolCalls].span.p, co.slot[slotToolCalls].span.len}
out.contentKind = int(co.slot[slotContent].kind)
out.reasoningKind = int(co.slot[slotReasoning].kind)
out.finishKind = int(co.slot[slotFinishReason].kind)
out.hasDelta = int(co.slot[slotDelta].kind) == kindObject
out.deltaKind = int(co.slot[slotDelta].kind)
out.choicesPresent = co.has_choices == 1
out.choicesKind = int(co.choices_kind)
out.choicesCount = int(co.choices_count)
s := sbuf[:int(used)]
out.content = string(s[co.content_off : co.content_off+co.content_len])
out.reasoning = string(s[co.reasoning_off : co.reasoning_off+co.reasoning_len])
out.finish = string(s[co.finish_off : co.finish_off+co.finish_len])
return out
}
// locateChunkBatchInto 是 locateChunkBatch 的零分配内核(基准用):
// 复用调用方提供的 sbuf,不自己 make。
func locateChunkBatchInto(data string, sbuf []byte) chunkLocateResult {
var out chunkLocateResult
if len(data) == 0 {
out.status = chunkFallback
return out
}
p, n := cstr(data)
var co C.ha_chunk_out
var used C.size_t
st := C.go_chunk_locate(p, n, &co, cstrb(sbuf), C.size_t(len(sbuf)), &used)
out.status = int(st)
if st != C.int(chunkOK) {
return out
}
out.usageKind = int(co.slot[slotUsage].kind)
out.usageSpan = strSpan{co.slot[slotUsage].span.p, co.slot[slotUsage].span.len}
out.toolCallsKind = int(co.slot[slotToolCalls].kind)
out.toolCallsSpan = strSpan{co.slot[slotToolCalls].span.p, co.slot[slotToolCalls].span.len}
out.contentKind = int(co.slot[slotContent].kind)
out.reasoningKind = int(co.slot[slotReasoning].kind)
out.finishKind = int(co.slot[slotFinishReason].kind)
out.hasDelta = int(co.slot[slotDelta].kind) == kindObject
out.deltaKind = int(co.slot[slotDelta].kind)
out.choicesPresent = co.has_choices == 1
out.choicesKind = int(co.choices_kind)
out.choicesCount = int(co.choices_count)
out.choice0Span = strSpan{co.choice0_span.p, co.choice0_span.len}
s := sbuf[:int(used)]
out.content = string(s[co.content_off : co.content_off+co.content_len])
out.reasoning = string(s[co.reasoning_off : co.reasoning_off+co.reasoning_len])
out.finish = string(s[co.finish_off : co.finish_off+co.finish_len])
return out
}