Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 优化 Sm4Ecb 的内存占用 #211

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

kvii
Copy link

@kvii kvii commented Jan 21, 2025

把函数里那两个 tmp 切片删了,换成了 out 和 inData 的对应下标。内存使用次数明显下降了,而且性能也稍微高了一点。Sm4Cbc 那边感觉应该也可以这么改。

优化前:

> go test -benchmem -run=^$ -bench "^(BenchmarkSm4EcbEncode|BenchmarkSm4EcbDecode)$" github.com/tjfoc/gmsm/sm4
goos: darwin
goarch: arm64
pkg: github.com/tjfoc/gmsm/sm4
cpu: Apple M1 Pro
BenchmarkSm4EcbEncode-10          515772              2223 ns/op             928 B/op         18 allocs/op
BenchmarkSm4EcbDecode-10          296337              3932 ns/op             960 B/op         28 allocs/op
PASS
ok      github.com/tjfoc/gmsm/sm4       2.624s

优化后:

> go test -benchmem -run=^$ -bench "^(BenchmarkSm4EcbEncode|BenchmarkSm4EcbDecode)$" github.com/tjfoc/gmsm/sm4
goos: darwin
goarch: arm64
pkg: github.com/tjfoc/gmsm/sm4
cpu: Apple M1 Pro
BenchmarkSm4EcbEncode-10          593889              1999 ns/op             752 B/op          7 allocs/op
BenchmarkSm4EcbDecode-10          323290              3591 ns/op             608 B/op          6 allocs/op
PASS
ok      github.com/tjfoc/gmsm/sm4       3.353s

@kvii
Copy link
Author

kvii commented Jan 22, 2025

还有这些函数应该是支持原地替换的。如果参数不需要留着的话还能再省下分配响应切片的时间和空间。

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.

1 participant