动态评估bytes.Buffer的需求的容量,全局共享bytes.Buffer池#16
Merged
thinkeridea merged 2 commits intomainfrom Feb 5, 2021
Merged
Conversation
Codecov Report
@@ Coverage Diff @@
## main #16 +/- ##
=======================================
Coverage 90.77% 90.77%
=======================================
Files 25 26 +1
Lines 791 802 +11
=======================================
+ Hits 718 728 +10
- Misses 51 52 +1
Partials 22 22
Continue to review full report at Codecov.
|
This was referenced Feb 18, 2021
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.
采用全局共享不同容量区间(采用2的幂次方来划分),一共 22 个区间,最小64 byte, 最大 256 MB, 根据动态计算,在获取
bytes.Buffer时从相应桶的sync.Pool来获取,以减少多余的内存浪费及bytes.Buffer扩容次数。相较于使用固定容量的
sync.Pool在需求容量动态波动时可以获取30~40倍的性能提升(这并不包含GC带来的性能提升,那将是更令人兴奋的),在内存分配上可以减少近300倍。即使在每次都获取固定容量,且不发生扩容的情况(这会导致动态池只是用单个桶),
sync.Pool并没有带来很大的惊喜,动态池相较于sync.Pool性能略下降 28%, 单次约 17ns, 即使这样这依然让人振奋,大多数我们都在处理不同大小的数据,使用动态bytes.Buffer共享池我们将收益巨大。