-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Introduce sizehint!(s, n; shrink = true) to controll shrinkage #51929
Merged
vtjnash
merged 11 commits into
JuliaLang:master
from
petvana:pv/dict-controll-shrinkage-v3
Nov 8, 2023
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2634a30
Introduce sizehint!(s, n; shrink = true) to controll shrinkage
petvana d7eceb4
Add shrinkage control for Vector
petvana 0735eea
Add shrinkage control for Set
petvana 271f999
Add tests for Set and Vector
petvana 620cd2b
lock WeakKeyDict
petvana c0c055b
Make shrink Bool in base/array.jl
petvana 088b152
Force shrink to be Bool
petvana b1c8338
Merge branch 'pv/dict-controll-shrinkage-v3' of github.com:petvana/ju…
petvana 655a5bc
Compat + news
petvana 42ff9ec
Typo
petvana 35b2950
Merge branch 'master' into pv/dict-controll-shrinkage-v3
petvana File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,20 @@ | ||
@testset "shrink small array" begin | ||
x = [1, 2, 3, 4] | ||
@test x[1] == 1 | ||
@test x[2] == 2 | ||
@test x[3] == 3 | ||
@test x[4] == 4 | ||
@test ccall(:jl_array_size, Int, (Any, UInt), x, 0) == 4 | ||
@test ccall(:jl_array_size, Int, (Any, UInt), x, 1) == 4 | ||
sizehint!(x, 10000) | ||
@test x[1] == 1 | ||
@test x[2] == 2 | ||
@test x[3] == 3 | ||
@test x[4] == 4 | ||
@test ccall(:jl_array_size, Int, (Any, UInt), x, 0) == 4 | ||
@test ccall(:jl_array_size, Int, (Any, UInt), x, 1) == 10000 | ||
sizehint!(x, 4) | ||
@test x[1] == 1 | ||
@test x[2] == 2 | ||
@test x[3] == 3 | ||
@test x[4] == 4 | ||
@test ccall(:jl_array_size, Int, (Any, UInt), x, 0) == 4 | ||
@test ccall(:jl_array_size, Int, (Any, UInt), x, 1) == 4 | ||
|
||
x = [1, 2, 3, 4] | ||
@test x[1] == 1 | ||
@test x[2] == 2 | ||
@test x[3] == 3 | ||
@test x[4] == 4 | ||
@test ccall(:jl_array_size, Int, (Any, UInt), x, 0) == 4 | ||
@test ccall(:jl_array_size, Int, (Any, UInt), x, 1) == 4 | ||
sizehint!(x, 1000000) | ||
@test x[1] == 1 | ||
@test x[2] == 2 | ||
@test x[3] == 3 | ||
@test x[4] == 4 | ||
@test ccall(:jl_array_size, Int, (Any, UInt), x, 0) == 4 | ||
@test ccall(:jl_array_size, Int, (Any, UInt), x, 1) == 1000000 | ||
sizehint!(x, 4) | ||
@test x[1] == 1 | ||
@test x[2] == 2 | ||
@test x[3] == 3 | ||
@test x[4] == 4 | ||
@test ccall(:jl_array_size, Int, (Any, UInt), x, 0) == 4 | ||
@test ccall(:jl_array_size, Int, (Any, UInt), x, 1) == 4 | ||
function check_array(x, size, capacity) | ||
@test x[1] == 1 | ||
@test x[2] == 2 | ||
@test x[3] == 3 | ||
@test x[4] == 4 | ||
@test ccall(:jl_array_size, Int, (Any, UInt), x, 0) == size | ||
@test ccall(:jl_array_size, Int, (Any, UInt), x, 1) == capacity | ||
end | ||
for hint_size = [10000, 1000000] | ||
x = [1, 2, 3, 4] | ||
check_array(x, 4, 4) | ||
sizehint!(x, hint_size) | ||
check_array(x, 4, hint_size) | ||
sizehint!(x, 4; shrink = false) | ||
check_array(x, 4, hint_size) | ||
sizehint!(x, 4) | ||
check_array(x, 4, 4) | ||
end | ||
end |
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.
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.
Reading this, I don't understand what the argument does. Would you mind adding a more detailed explanation? Also it would seem more appropriate to mention the argument above, outside of the "# Notes on the performance model", as I wouldn't expect to have to read a performance section to find out the meaning of an argument.
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, this seems like a fair point. The new optional argument enables us to suggest not to shrink the already reserved capacity when suggesting new size by setting
shrink = false
. This is helpful for algorithms like merging two collection where you want to pre-allocate memory, but there is not reason to change (shrink) size if all the data fits. I'm not a native speaker, would you be able to propose a more fitting description?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've not a native speaker either, but see #52226. I hope I got it right.