perf(storage): SSTable 迭代器改缓冲读,归并快 17 倍 - #301
Merged
Merged
Conversation
迭代器逐字段直接从 *os.File 读取(键长、键、值长、值),每条记录约四次 read syscall。 它同时服务 compaction 的每个归并源与范围扫描,是全量读的热点:10 万条记录即约 40 万次 系统调用。加一层 64KiB 缓冲后,内核往返次数与记录数解耦。 实测(本机 macOS,10 万条 × 200B value): 顺序迭代 261ms → 11ms (38 万条/秒 → 923 万条/秒,24×) 8 路归并 1.391s → 80ms ( 7 万条/秒 → 124 万条/秒,17×) 投递 Fetch 取 201 条:游标居中 3.5ms → 1.57ms,游标靠后 0.9ms → 0.12ms 加缓冲后不能再用 Seek(0, SeekCurrent) 判断是否越过数据区终点:那返回的是底层文件偏移, 已被预读推到缓冲末尾,会让迭代提前结束。改为自行累计已消费字节数。带 seek 的构造在定位后 必须 Reset 缓冲,否则缓冲里残留的是 seek 之前预读的字节。 新增 iterator_bench_test.go 固化这两项测量,便于后续改动对照。 Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (2)
📝 WalkthroughWalkthroughThe SSTable iterator now uses a 64 KiB buffered reader and explicit byte-position tracking. New throughput tests measure sequential scans and multi-file compaction merges. ChangesSSTable iteration performance
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant SSTableIterator
participant BufferedReader
participant SSTableFile
SSTableIterator->>SSTableFile: Seek to block offset
SSTableIterator->>BufferedReader: Reset at seek offset
SSTableIterator->>BufferedReader: Read record fields
BufferedReader->>SSTableFile: Read buffered bytes
SSTableIterator->>SSTableIterator: Update consumed position
✨ Finishing Touches 💡 1🛠️ Fix failing CI checks 💡
📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
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.
迭代器逐字段直接从
*os.File读取(键长、键、值长、值),每条记录约四次 read syscall。它同时服务 compaction 的每个归并源与范围扫描 —— 是全量读的热点,10 万条记录即约 40 万次系统调用。加一层 64KiB 缓冲,让内核往返次数与记录数解耦。
实测(本机 macOS,10 万条 × 200B value)
归并那一项用
git stash只回退iterator.go做前后对照,排除其它改动干扰。两个必须一起改的细节
不能再用
Seek(0, SeekCurrent)判断是否越过数据区终点 —— 那返回的是底层文件偏移,已被预读推到缓冲末尾,会让迭代提前结束(表现为条目莫名减少)。改为自行累计已消费字节数。带 seek 的构造在定位后必须
Reset缓冲 —— 否则缓冲里残留的是 seek 之前预读的字节,会从错误位置开始解析。过程记录
首次替换时搜索串写错(原文是
it.f,我写成it.r),替换静默未生效,测出 261ms→270ms「无提升」。没有就此下结论说「syscall 不是瓶颈」,而是先去核对代码是否真的改了 —— 一看Next()原样未动。修正后才是真实结果。验证
新增
iterator_bench_test.go固化这两项测量,便于后续改动对照。go build ./...(含-tags pprof)、go vet、gofmt、go test -race ./...连跑 2 次全绿 —— 这条路径被 compaction、范围扫描、新旧判定(recency)、故障注入等既有用例大量覆盖。🤖 Generated with Claude Code
Summary by CodeRabbit
Performance
Tests