-
Notifications
You must be signed in to change notification settings - Fork 812
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
Small optimization to avoid unnecessary Extents merge when growing cache entry #4026
Small optimization to avoid unnecessary Extents merge when growing cache entry #4026
Conversation
…che entry Signed-off-by: Alvin Lin <[email protected]>
Signed-off-by: Alvin Lin <[email protected]>
Signed-off-by: Alvin Lin <[email protected]>
Signed-off-by: Alvin Lin <[email protected]>
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.
Nice, thanks. Is it worth adding a test for this to verify intended behavior?
I was pondering about the same question, but then I came to the conclusion that since this is a small optimization, so as long as existing tests passes then the change is functionally correct because it's not supposed to change any functional behaviour. In other words, I was thinking how I can write a test that fails before this change, but passes after change; I couldn't. If I am able to write some test that fail before this change, then something not related to this change is wrong :-) I also thought about refactor the Let me know your thoughts on this as well :-) |
I haven't looked into details, but I was thinking that unit test could verify that merging such extents returns original big extent, instead of two smaller ones? |
Thanks for sharing your thought, I see your point. But in order to test this we might need to modify either |
Oh, I thought that the test could check if this slice is equal to input slice (= big one) or not equal (= new merged slice), but apparently slices in Go are not comparable. Perhaps it would be doable by constructing input slices with some big capacity, to be able to distinguish between them, or by checking for number of allocations (using https://golang.org/pkg/testing/#AllocsPerRun), which should be 0 if input slice was returned unmodified. |
Signed-off-by: Alvin Lin <[email protected]>
d82315c
to
67a714c
Compare
Oh this was interesting, I learned something new! But I did not go with this approach because I was afraid it would make the test brittle. However, your comments gave me an idea about how to test, take a look at my new revision :-) I confirm that the test fails in the absence of the optimization with error that "response is nil". |
Signed-off-by: Alvin Lin <[email protected]>
@pstibrany any more comments on this PR? |
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.
Good job, LGTM. Thanks!
Signed-off-by: Marco Pracucci <[email protected]>
Thanks a lot @alvinlin123 for working on this. The PR is good and I will merge it as soon as the CI pass. For the next optimization PR (I'm sure there will be many coming ;)) I would suggest you to test the optimization with a benchmark (ideally an existing one or write a new one if none exists) in order to check its impact. 👏 |
What this PR does:
Given 2 Extents that starts at the same time, but ends at different time, it is better to put the Extent ends later (larger) at the front of the slice during sort-by-start-time time. Reason being that because the larger Extent "covers" the smaller Extents, so during the merging phase the smaller Extent simply gets dropped.
If we put the smaller Extent before the larger Extent, then during the merge phase, the algorithm will need to figure out the overlapping point the merge the larger Extent into the smaller Extent, which is another
O(log n)
operation.Before:
After:
Which issue(s) this PR fixes:
Didn't create issue because it's an small optimization.
Checklist
CHANGELOG.md
updated - the order of entries should be[CHANGE]
,[FEATURE]
,[ENHANCEMENT]
,[BUGFIX]