perf: reduce Go FFI allocations and optimize native FTS ingestion / 优化 Go FFI 分配与原生全文索引写入 - #14
Draft
sunhailin-Leo wants to merge 8 commits into
Draft
sunhailin-Leo wants to merge 8 commits into
sunhailin-Leo wants to merge 8 commits into
Conversation
sunhailin-Leo
force-pushed
the
codex/optimize-go-ffi-upstream
branch
3 times, most recently
from
September 24, 2026 18:55
e604861 to
477b18d
Compare
…marks
Expose the zvec FTS index-level extra_params switch
{"store_positions": false} to Go users via the existing
NewFTSIndexParams extraParams argument (no new C symbols, so older
prebuilt libraries keep working).
- New integration tests: phrase queries keep working with the default
(positions on); with the flag off, term/BM25 queries keep working,
phrase queries are rejected, and the behavior survives Close/Open.
- FTS ingestion benchmark gains NoPos variants (Batch1/128/512/After)
so insert-time and on-disk gains/losses are measurable per corpus.
Co-Authored-By: Claude <noreply@anthropic.com>
Bump the zvec submodule from v0.7.0 to 2070ce7 (fork branch
perf/fts-store-positions-option, based on v0.7.0) which adds the
native FTS extra_params switch {"store_positions": false}:
skip per-(term, doc) position writes to cut ingestion cost and index
size, and reject phrase queries with an explicit error.
.gitmodules is intentionally left pointing at alibaba/zvec: the native
change is proposed upstream separately, and after it merges there the
gitlink should be repointed to the upstream commit. Until then the
pinned SHA exists only in the sunhailin-Leo/zvec fork, so source-mode
CI cannot fetch it at checkout (expected red Source jobs); validation
runs locally from the fork checkout instead.
Measured on 180,224 docs / varied corpus (macOS arm64, source mode):
sum of Insert() 167.4s -> 129.4s (-22.7%); closed collection total
607.5 MB -> 302.5 MB (-50.1%); closed FTS rocksdb dir 398.0 MB ->
93.3 MB (-76.5%). Positions-on ingestion keeps identical on-disk
output.
Co-Authored-By: Claude <noreply@anthropic.com>
sunhailin-Leo
force-pushed
the
codex/optimize-go-ffi-upstream
branch
2 times, most recently
from
September 25, 2026 03:55
0cee138 to
13bc090
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
改动 / Changes
perf/fts-store-positions-option、commit2070ce7(基于 v0.7.0,仅改 gitlink,不修改 .gitmodules)。FTSextra_params新增索引级开关{"store_positions": false}:插入时跳过每个 (term, doc) 的位置列表($POS)写放,短语查询改为显式 InvalidArgument 报错(不再静默返回错误结果);非布尔值在 open() 校验失败;insert 复用 (term, doc_id) 键与 tf 缓冲减少临时分配。无新增 C 符号,老预编译库向前兼容(忽略该键)。同时新增 Go 集成测试与 FTS NoPos 基准变体。原生改动建议同步作为独立 PR 提交上游 zvec,合并后把 gitlink 重指上游 commit。perf/fts-store-positions-option, commit2070ce7(based on v0.7.0; only the gitlink changes, .gitmodules is left untouched). The FTSextra_paramsgains an index-level switch{"store_positions": false}: ingestion skips per-(term, doc) position-list ($POS) writes, phrase queries are rejected with an explicit InvalidArgument error instead of silently returning wrong results; non-boolean values fail loudly at open(); insert() reuses the (term, doc_id) key and tf buffers to cut per-term temporary allocations. No new C symbols — older prebuilt libraries ignore the key. Go integration tests and FTS NoPos benchmark variants are added. The native change is also proposed upstream for zvec; once merged, repoint the gitlink to the upstream commit.GetVectorFP32FieldInto。purego 连续包装结果并合并字符串数组缓冲。新增 Darwin 原生堆回归测试。GetVectorFP32FieldInto. Pack purego results and string arrays. Add Darwin native-heap regression tests.数据 / Measurements
Apple M3 Pro · Go 1.24.3 · zvec v0.7.0. Getter figures are three-run medians; Go B/op excludes native allocations. Comparisons are case-specific (Query: main → PR; other rows: incremental).
全文索引 / FTS(原生优化 / native optimization)
本节为此 PR 的原生 FTS 插入速度与索引尺寸优化,来自另一台 Mac(搭载 cgo source 模式自建库、高后台负载),请勿与上表 M3 Pro 数据直接对比;组内对照组见下方负对照。180,224 文档 · varied 语料 · Batch512 · 每行一组单次运行。本 PR 修改了原生 FTS 实现(
store_positions开关)。新增原生改动:FTS
extra_params索引级开关{"store_positions": false}—— 插入跳过 (term, doc) 位置列表写放;短语查询显式报错(InvalidArgument);term/BM25 查询不受影响;无新增 C 符号。store_positions:false(n=3)store_positions:false时 term/BM25 查询照常工作;短语查询被显式拒绝并且错误在 Close/重开之后保持;非布尔取值在 open() 校验失败(防止拼写错误静默切换)。原生 fts 单测 74/74 通过(新增 5 个store_positions用例:跳写 $POS 且 $TF 保留、短语拒答、密封后 term 查询、缺省路径回归、非法取值报错)。ZVEC_BENCH_FTS_ROWS=180224 ZVEC_BENCH_FTS_CORPUS=varied go test -tags integration -run '^$' -bench '^BenchmarkFTSIngestion/(FTS_Batch512|FTS_NoPos_Batch512)$' -benchtime=1x -count=1 -benchmem .(FTS_Batch512 两次运行取基线与默认路径验证;FTS_NoPos_Batch512 -count=3)验证 / Validation
go vetmodes, cgocgocheck2, formatting, and FTS reopen-query pass on macOS/arm64 against the rebuilt source-mode library. 本轮新增:zvec 原生 fts 列索引单测 74/74(含 5 个store_positions新用例);cgo source 全量套件连续 3 次通过;purego 套件通过(ZVEC_LIBRARY_PATH 指向重建库);race 模式下全套件存在一个既有的临界门限:TestCollectionQueryFetchNativeHeapDoesNotGrowPerResult(1 MB 预算)在本机多次测量为 0.65–1.18 MB——父分支与未修改的 v0.7.0 库同样波动并可越线(该门限只覆盖 invert+HNSW 查询路径,与本次 FTS 改动无交集;单独运行通过,CI 专用 runner 上 race 全套通过)。13bc090,.gitmodules 未改动指向 alibaba/zvec,GitHub fork 网络共享对象存储故按 SHA 可拉取2070ce7;四平台 Source 从实库构建,含 race、purego、vet、benchmarks、gofmt,构建日志正确识别版本v0.7.0-1-g2070ce7)。13bc090— .gitmodules untouched at alibaba/zvec; the pinned SHA2070ce7is fetchable through GitHub's fork-network object sharing. All four Source platforms build the real library and pass race, purego, vet, benchmarks, and gofmt; the build log reports versionv0.7.0-1-g2070ce7correctly.