Skip to content

Feat/optimize mem use#134

Merged
yanyiwu merged 5 commits intoyanyiwu:masterfrom
kevinmatthe:feat/optimize_mem_use
Nov 27, 2025
Merged

Feat/optimize mem use#134
yanyiwu merged 5 commits intoyanyiwu:masterfrom
kevinmatthe:feat/optimize_mem_use

Conversation

@kevinmatthe
Copy link
Copy Markdown
Contributor

预提交PR,TODO: 补充测试用例、性能报告
关联issue/问题:
#133 增加RSS提前释放
Cut、CutAll、CutForSearch存在过多的跨CGO内存拷贝,利用vector返回优化。

@kevinmatthe kevinmatthe force-pushed the feat/optimize_mem_use branch 2 times, most recently from 7b4bf8f to 37361c7 Compare November 20, 2025 14:30
- 修改分词函数返回类型为 Word* 替代 char** 以提高效率
- 新增内存整理函数 Trim() 用于释放未使用的内存
- 添加 FreeWithTrim() 和 WithTrim() 方法优化 Go 接口内存管理
- 实现零拷贝的 convertCWordToSlice 和 convertCWordToStructs 转换函数
- 新增基准测试文件 jieba_benchmark_test.go 用于性能评估

⚡️ perf(jieba): 改进分词结果处理

- 使用 Word 结构体替代字符串数组存储分词结果
- 优化 C/Go 边界数据转换效率
- 减少内存分配和拷贝操作

✅ test(jieba): 添加基准测试

- 新增 Cut/CutAll/CutForSearch 性能测试
- 添加内存占用对比测试
- 实现 RSS 内存统计功能
@kevinmatthe kevinmatthe force-pushed the feat/optimize_mem_use branch from 37361c7 to 8e5b074 Compare November 20, 2025 14:36
- 修复注释格式,增加空格提高可读性
- 统一变量声明方式,使用短变量声明
- 简化结构体初始化语法,移除冗余类型声明
- 重命名示例函数以更清晰地表达用途

✅ test(jieba): 更新测试用例

- 保持测试覆盖率不变
- 优化测试数据格式
- 确保测试断言清晰明确
- 包含<stdlib.h>头文件以确保内存分配函数可用
- 新增Trim函数用于手动触发malloc_trim内存回收
- 移除旧的性能测试代码
- 添加新的性能测试文件jieba_trim_test.go

✅ test(jieba): 添加malloc_trim相关测试用例

- 添加BenchmarkTrimCost_Amortized测试不同负载下的Trim性能
- 添加TestTrimLatencyImpact测试Trim对延迟的影响
- 添加TestTrimLatency测试Trim的实际耗时
- 添加TestRSSRecovery验证Trim对内存回收的效果
- 将getRSS函数从jieba_benchmark_test.go移动到jieba_trim_test.go
- 删除benchmark测试文件中未使用的导入包
@kevinmatthe
Copy link
Copy Markdown
Contributor Author

Part1. 内存拷贝优化:
在Base分支中(当前master),通过CGO导出的Cut、CutAll等方法,均返回了char**,将类型计算在C层完成,但实际上导致了更大量内存的拷贝;实际上这部分类型计算放到Golang中进行的开销甚至会比CGO更低,同时能避免过多的内存拷贝。基于这个背景,对上述返回char**的函数进行了更新,允许直接返回Word结构体,在Golang层进行二次计算。

Base分支的Benchmark:
image

待合入分支的Benchmark:
image

可见在高频调用的耗时上可以缩减接近30%的水平。

@yanyiwu yanyiwu merged commit 94cbf93 into yanyiwu:master Nov 27, 2025
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants