-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[WIP][stdlib] Dictionary: Add unsafe bulk-loading initializer #21208
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4e61394
[stdlib] Dictionary: Add unsafe bulk-loading initializer
lorentey 5969054
[stdlib] Dictionary: Add support for non-unique keys in bulk loading …
lorentey 836b692
[stdlib] Process elements from back to front
lorentey 2a0ad88
[stdlib] Doc updates
lorentey 22d3a5e
[stdlib] Keep the first duplicate key instead of the last
lorentey 32a7ed9
[squash] Typo fix
lorentey 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 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
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
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
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
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.
This is a cool algorithm, @lorentey 👏
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 had to work it out myself because I couldn't picture how to hash-in-place like this, but I think I got it. Nice work. (Did you adapt it from somewhere or come up with it yourself?)
Does this obsolete DictionaryBuilder?
For my own understanding:
bucket
", "bucket
up tocount
", and "count
and after".bucket
" and "count
and after" is either uninitialized or in use. Everything in "bucket
up tocount
" is either unprocessed or in use.native.hashTable
, the same way it would be for a working Dictionary. Whenever we see such a bucket in the middle section, we skip over it, andnative.hashTable.insertNew
will never return an in-use bucket. (This is the key thing I was missing when trying to picture the algorithm on my own.)bucket
).Uh oh!
There was an error while loading. Please reload this page.
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.
That's a nice analysis & it's exactly right -- in fact, I'll steal it to document the invariants!
This is unlikely to be a new algorithm -- I can swear I saw something like this done before, although maybe not in these exact circumstances. (Knuth, maybe? This feels like the sort of thing he may include as an exercise.)
We're experimenting with switching some forms of bridging from
_DictionaryBuilder
to this, but it's only an experiment at this point. This is designed specifically to work with the narrow use case of-[NSDictionary objects:keys:count:]
.It's quite more fiddly than
_DictionaryBuilder
, so if the input comes from a sequence of unique key/value pairs, then I expect the builder will likely still be faster. (Although benchmarks may surprise us!)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 wonder if there's a tiny simplification in going backwards instead of forwards—indeed, @Catfish-Man described it to me that way at first. That way you only have two regions to think about (and for a concrete benefit, only have to make one comparison).
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 idea; applied!