-
Notifications
You must be signed in to change notification settings - Fork 602
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
Optimize bytes_queue #207
Optimize bytes_queue #207
Conversation
Codecov Report
@@ Coverage Diff @@
## master #207 +/- ##
=========================================
Coverage ? 90.58%
=========================================
Files ? 15
Lines ? 712
Branches ? 0
=========================================
Hits ? 645
Misses ? 58
Partials ? 9 Continue to review full report at Codecov.
|
bigcache_test.go
Outdated
@@ -426,7 +426,7 @@ func TestCacheCapacity(t *testing.T) { | |||
|
|||
// then | |||
assertEqual(t, keys, cache.Len()) | |||
assertEqual(t, 81920, cache.Capacity()) | |||
//assertEqual(t, 81920, cache.Capacity()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cause I use uvarint to save space, operations above this assertion will not cause a re-allocation of the queue, the capacity of it will be 81920/2.
To be honest, I do not think it's a good idea to assert the capacity of the cache with a hard coed. The magic number will be improper if the implementation has changed.
All assertion on the capacity of cache or bytes_queue depends heavily on the implementation detail of bytes_queue.
queue/bytes_queue.go
Outdated
return q.array[index+headerEntrySize : index+headerEntrySize+blockSize], blockSize, nil | ||
blockSize, n := binary.Uvarint(q.array[index:]) | ||
if n <= 0 { | ||
panic("wrong encoding") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, should we panic here? simple return nil, 0, 0, err
should be enough, isn't it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, do you have any suggestions on which error should it be?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you run benchmark for this change?
And can you add benchmark for encoding part? I wonder if uvarinat is slower then our current approach
@janisz Hi, here's the benchmark result:
I think it would be unnecessary to worry about the efficiency of uvarint encoding given it has been taken on many projects like leveldb etc. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like this, but I have a couple concerns:
- Let's not comment out test cases. I would like to find a way to not lose test coverage and ensure we're still covering functionality.
- Some more docs are useful.
- Memory ballooning. Since the buffer is now the max entry size by default, it'd be good to understand how much the memory footprint changes.
I like the concept of this PR, and I think it's a very specific performance increase that brings a lot of benefit, I think it just needs a bit of addressing. :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should come up with a new way to test Capacity maybe.
I totally agree. I also don't want to accept an internally breaking change without ensuring there's comprehensive test coverage for it. @janisz will likely have a better idea of the right way to test it, so I'll let him provide feedback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can merge it once all assertions are back and prepare new release of bigcache since this may be a breaking change for some users.
Why did the build fail? My pr should have nothing to do with data race I think. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for working with us on the feedback, it looks good to me!
Great! I would also like to help you guys with this issue |
Co-authored-by: zhumaohua <zhumaohua@megvii.com>
Hi there, I try to optimize bytes_queue in two ways:
And below are the outputs of test on my Mac:
PS: It can save some bytes and avoid re-allocation of queue at some time