-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[ADT] Add C++17-style insert_or_assign for DenseMap #94151
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
Merged
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
1724a23
implement insert_or_assign
c8ef a13126c
test the const ref api
c8ef 9142324
add a subclass to avoid default construct
c8ef 1271ba9
refactor MoveOnly
c8ef e40b110
Merge branch 'main' into adt
c8ef 6873996
Merge branch 'main' into adt
c8ef 2b0fea1
reset counter for each fragment
c8ef 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
Oops, something went wrong.
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 test coverage only covers the rvalue ref Key overload, yeah? Be good to have similar coverage for the const ref overload too.
Also, some of the value/point of
insert_or_assign
is to directly construct the value where possible, and otherwise fallback to assignment. But this test doesn't differentiate between whether the object was default constructed then assigned to, or directly copy/move constructed. To get that more precise test coverage it might be necessary to add more detailed tracking to the CountCopyAndMove helper - but I think we might have a more reusable/generic/detailed type shared across more of these ADT unit tests that could be used?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 have updated the test case to test both APIs.
I'm not completely sure how to test it as it appears to require integration with the DenseMap internal structure. Could you provide some insight on this?
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 it would be enough to drop the default constructor on CountCopyAndMove (or add a variant of it that does).
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.
Simply removing the default constructor would require modifying many other tests, so I opted to add a subclass to address the issue.
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.
Ah, we have MoveOnly in llvm/unittests/ADT/MoveOnly, which does differentiate counting between the ctor and assignment operator - but doesn't provide copy operations/counting those. Something similar could be added.
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.
The updated test doesn't look like it differentiates between ctor and assignment, though? I guess by virtue of it not being default constructible it necessarily can't be construct+assign in the cases of new values? That's a bit subtle, and I'd have thought testing that only copy/move construction happened on new insertion, and only copy/move assignment happened when insert_or_assigning over an existing value would be more clear about the expectations?
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.
Thank you for your suggestions! I have refactored the MoveOnly class, added statistics regarding the copy constructor and assignment, and tested the insert_or_assign API using this information.