Skip to content

[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
merged 7 commits into from
Jun 5, 2024
Merged

Conversation

c8ef
Copy link
Contributor

@c8ef c8ef commented Jun 2, 2024

add C++17-style insert_or_assign for DenseMap

close: #94115

Copy link

github-actions bot commented Jun 2, 2024

Thank you for submitting a Pull Request (PR) to the LLVM Project!

This PR will be automatically labeled and the relevant teams will be
notified.

If you wish to, you can add reviewers by using the "Reviewers" section on this page.

If this is not working for you, it is probably because you do not have write
permissions for the repository. In which case you can instead tag reviewers by
name in a comment by using @ followed by their GitHub username.

If you have received no comments on your PR for a week, you can request a review
by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate
is once a week. Please remember that you are asking for valuable time from other developers.

If you have further questions, they may be answered by the LLVM GitHub User Guide.

You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums.

@llvmbot
Copy link
Member

llvmbot commented Jun 2, 2024

@llvm/pr-subscribers-llvm-adt

Author: None (c8ef)

Changes

add C++17-style try_emplace and insert_or_assign for DenseMap

close: #94115


Full diff: https://github.com/llvm/llvm-project/pull/94151.diff

2 Files Affected:

  • (modified) llvm/include/llvm/ADT/DenseMap.h (+16)
  • (modified) llvm/unittests/ADT/DenseMapTest.cpp (+28)
diff --git a/llvm/include/llvm/ADT/DenseMap.h b/llvm/include/llvm/ADT/DenseMap.h
index 3ef6a7cd1b4b5..7ccc9445c0a7b 100644
--- a/llvm/include/llvm/ADT/DenseMap.h
+++ b/llvm/include/llvm/ADT/DenseMap.h
@@ -312,6 +312,22 @@ class DenseMapBase : public DebugEpochBase {
       insert(*I);
   }
 
+  template <typename V>
+  std::pair<iterator, bool> insert_or_assign(const KeyT &Key, V &&Val) {
+    auto Ret = try_emplace(Key, std::forward<V>(Val));
+    if (!Ret.second)
+      Ret.first->second = std::forward<V>(Val);
+    return Ret;
+  }
+
+  template <typename V>
+  std::pair<iterator, bool> insert_or_assign(KeyT &&Key, V &&Val) {
+    auto Ret = try_emplace(std::move(Key), std::forward<V>(Val));
+    if (!Ret.second)
+      Ret.first->second = std::forward<V>(Val);
+    return Ret;
+  }
+
   /// Returns the value associated to the key in the map if it exists. If it
   /// does not exist, emplace a default value for the key and returns a
   /// reference to the newly created value.
diff --git a/llvm/unittests/ADT/DenseMapTest.cpp b/llvm/unittests/ADT/DenseMapTest.cpp
index cc3244528f27e..3fa20ef62ccea 100644
--- a/llvm/unittests/ADT/DenseMapTest.cpp
+++ b/llvm/unittests/ADT/DenseMapTest.cpp
@@ -499,6 +499,34 @@ TEST(DenseMapCustomTest, ReserveTest) {
   }
 }
 
+TEST(DenseMapCustomTest, InsertOrAssignTest) {
+  DenseMap<int, CountCopyAndMove> Map;
+  CountCopyAndMove::Copy = 0;
+  CountCopyAndMove::Move = 0;
+
+  CountCopyAndMove val1;
+  auto try0 = Map.insert_or_assign(0, val1);
+  EXPECT_TRUE(try0.second);
+  EXPECT_EQ(0, CountCopyAndMove::Move);
+  EXPECT_EQ(1, CountCopyAndMove::Copy);
+
+  auto try1 = Map.insert_or_assign(0, val1);
+  EXPECT_FALSE(try1.second);
+  EXPECT_EQ(0, CountCopyAndMove::Move);
+  EXPECT_EQ(2, CountCopyAndMove::Copy);
+
+  CountCopyAndMove val2;
+  auto try2 = Map.insert_or_assign(2, val2);
+  EXPECT_TRUE(try2.second);
+  EXPECT_EQ(0, CountCopyAndMove::Move);
+  EXPECT_EQ(3, CountCopyAndMove::Copy);
+
+  auto try3 = Map.insert_or_assign(2, std::move(val2));
+  EXPECT_FALSE(try3.second);
+  EXPECT_EQ(1, CountCopyAndMove::Move);
+  EXPECT_EQ(3, CountCopyAndMove::Copy);
+}
+
 // Make sure DenseMap works with StringRef keys.
 TEST(DenseMapCustomTest, StringRefTest) {
   DenseMap<StringRef, int> M;

@nikic nikic changed the title [ADT] add C++17-style try_emplace and insert_or_assign for DenseMap [ADT] Add C++17-style insert_or_assign for DenseMap Jun 2, 2024
@nikic nikic requested review from MaskRay and dwblaikie June 2, 2024 09:02
Comment on lines 502 to 529
TEST(DenseMapCustomTest, InsertOrAssignTest) {
DenseMap<int, CountCopyAndMove> Map;
CountCopyAndMove::Copy = 0;
CountCopyAndMove::Move = 0;

CountCopyAndMove val1;
auto try0 = Map.insert_or_assign(0, val1);
EXPECT_TRUE(try0.second);
EXPECT_EQ(0, CountCopyAndMove::Move);
EXPECT_EQ(1, CountCopyAndMove::Copy);

auto try1 = Map.insert_or_assign(0, val1);
EXPECT_FALSE(try1.second);
EXPECT_EQ(0, CountCopyAndMove::Move);
EXPECT_EQ(2, CountCopyAndMove::Copy);

CountCopyAndMove val2;
auto try2 = Map.insert_or_assign(2, val2);
EXPECT_TRUE(try2.second);
EXPECT_EQ(0, CountCopyAndMove::Move);
EXPECT_EQ(3, CountCopyAndMove::Copy);

auto try3 = Map.insert_or_assign(2, std::move(val2));
EXPECT_FALSE(try3.second);
EXPECT_EQ(1, CountCopyAndMove::Move);
EXPECT_EQ(3, CountCopyAndMove::Copy);
}

Copy link
Collaborator

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?

Copy link
Contributor Author

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.

I have updated the test case to test both APIs.

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?

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?

Copy link
Contributor

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).

Copy link
Contributor Author

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).

Simply removing the default constructor would require modifying many other tests, so I opted to add a subclass to address the issue.

Copy link
Collaborator

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.

Copy link
Collaborator

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?

Copy link
Contributor Author

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?

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.

@c8ef c8ef requested a review from dwblaikie June 3, 2024 02:04
@c8ef c8ef requested a review from nikic June 3, 2024 08:58
Copy link
Collaborator

@dwblaikie dwblaikie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great - ideally, the refactoring of the CountCopyAndMove type would be a separate commit - would you be up for sending that in a separate review to be committed ahead of the new insert_or_assign functionality? Otherwise I can probably do that work while still crediting you on the commits

Comment on lines 478 to 511
TEST(DenseMapCustomTest, InsertOrAssignTest) {
DenseMap<int, CountCopyAndMove> Map;
CountCopyAndMove::ResetCounts();

CountCopyAndMove val1(1);
auto try0 = Map.insert_or_assign(0, val1);
EXPECT_TRUE(try0.second);
EXPECT_EQ(0, CountCopyAndMove::TotalMoves());
EXPECT_EQ(1, CountCopyAndMove::CopyConstructions);
EXPECT_EQ(0, CountCopyAndMove::CopyAssignments);

auto try1 = Map.insert_or_assign(0, val1);
EXPECT_FALSE(try1.second);
EXPECT_EQ(0, CountCopyAndMove::TotalMoves());
EXPECT_EQ(1, CountCopyAndMove::CopyConstructions);
EXPECT_EQ(1, CountCopyAndMove::CopyAssignments);

int key2 = 2;
CountCopyAndMove val2(2);
auto try2 = Map.insert_or_assign(key2, std::move(val2));
EXPECT_TRUE(try2.second);
EXPECT_EQ(1, CountCopyAndMove::MoveConstructions);
EXPECT_EQ(0, CountCopyAndMove::MoveAssignments);
EXPECT_EQ(1, CountCopyAndMove::CopyConstructions);
EXPECT_EQ(1, CountCopyAndMove::CopyAssignments);

CountCopyAndMove val3(3);
auto try3 = Map.insert_or_assign(key2, std::move(val3));
EXPECT_FALSE(try3.second);
EXPECT_EQ(1, CountCopyAndMove::MoveConstructions);
EXPECT_EQ(1, CountCopyAndMove::MoveAssignments);
EXPECT_EQ(1, CountCopyAndMove::CopyConstructions);
EXPECT_EQ(1, CountCopyAndMove::CopyAssignments);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably easier to read if the counts are reset between each portion of the test? so it's clear the expectations are about what the immediate previous operation did, rather than the total of all the things that came before?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Think just this could be addressed then good to go?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@c8ef
Copy link
Contributor Author

c8ef commented Jun 5, 2024

Looks great - ideally, the refactoring of the CountCopyAndMove type would be a separate commit - would you be up for sending that in a separate review to be committed ahead of the new insert_or_assign functionality? Otherwise I can probably do that work while still crediting you on the commits

No problem, I have submitted pull request #94421 for the refactoring.

dwblaikie pushed a commit that referenced this pull request Jun 5, 2024
@c8ef c8ef requested a review from dwblaikie June 5, 2024 07:19
Copy link
Collaborator

@dwblaikie dwblaikie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for your patience!

@dwblaikie dwblaikie merged commit 07b8990 into llvm:main Jun 5, 2024
7 checks passed
Copy link

github-actions bot commented Jun 5, 2024

@c8ef Congratulations on having your first Pull Request (PR) merged into the LLVM Project!

Your changes will be combined with recent changes from other authors, then tested
by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR.

Please check whether problems have been caused by your change specifically, as
the builds can include changes from many authors. It is not uncommon for your
change to be included in a build that fails due to someone else's changes, or
infrastructure issues.

How to do this, and the rest of the post-merge process, is covered in detail here.

If your change does cause a problem, it may be reverted, or you can revert it yourself.
This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again.

If you don't get any reports, no action is required from you. Your changes are working as expected, well done!

@c8ef c8ef deleted the adt branch June 5, 2024 15:28
@c8ef
Copy link
Contributor Author

c8ef commented Jun 5, 2024

Really appreciate your detailed guidance!

1 similar comment
@c8ef

This comment was marked as duplicate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[ADT] Add DenseMap::insert_or_assign
4 participants