Skip to content

[XRay] Use DenseMap::{operator[],try_emplace} (NFC) #107178

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

Conversation

kazutakahirata
Copy link
Contributor

I'm planning to deprecate DenseMap::FindAndConstruct in favor of
operator[]. I'm using try_emplace because "Vertices[I.first];" on its
own might look like a nop statement.

I'm planning to deprecate DenseMap::FindAndConstruct in favor of
operator[].  I'm using try_emplace because "Vertices[I.first];" on its
own might look like a nop statement.
@llvmbot
Copy link
Member

llvmbot commented Sep 4, 2024

@llvm/pr-subscribers-xray

Author: Kazu Hirata (kazutakahirata)

Changes

I'm planning to deprecate DenseMap::FindAndConstruct in favor of
operator[]. I'm using try_emplace because "Vertices[I.first];" on its
own might look like a nop statement.


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

1 Files Affected:

  • (modified) llvm/include/llvm/XRay/Graph.h (+7-9)
diff --git a/llvm/include/llvm/XRay/Graph.h b/llvm/include/llvm/XRay/Graph.h
index 953ac1aa696371..6c8b178e58ff8c 100644
--- a/llvm/include/llvm/XRay/Graph.h
+++ b/llvm/include/llvm/XRay/Graph.h
@@ -378,20 +378,18 @@ class Graph {
 
   /// Looks up the vertex with identifier I, if it does not exist it default
   /// constructs it.
-  VertexAttribute &operator[](const VertexIdentifier &I) {
-    return Vertices.FindAndConstruct(I).second;
-  }
+  VertexAttribute &operator[](const VertexIdentifier &I) { return Vertices[I]; }
 
   /// Looks up the edge with identifier I, if it does not exist it default
   /// constructs it, if it's endpoints do not exist it also default constructs
   /// them.
   EdgeAttribute &operator[](const EdgeIdentifier &I) {
-    auto &P = Edges.FindAndConstruct(I);
-    Vertices.FindAndConstruct(I.first);
-    Vertices.FindAndConstruct(I.second);
+    auto &Attr = Edges[I];
+    Vertices.try_emplace(I.first);
+    Vertices.try_emplace(I.second);
     InNeighbors[I.second].insert(I.first);
     OutNeighbors[I.first].insert(I.second);
-    return P.second;
+    return Attr;
   }
 
   /// Looks up a vertex with Identifier I, or an error if it does not exist.
@@ -479,8 +477,8 @@ class Graph {
     auto EI = Val.first;
     const auto &p = Edges.insert(std::move(Val));
     if (p.second) {
-      Vertices.FindAndConstruct(EI.first);
-      Vertices.FindAndConstruct(EI.second);
+      Vertices.try_emplace(EI.first);
+      Vertices.try_emplace(EI.second);
       InNeighbors[EI.second].insert(EI.first);
       OutNeighbors[EI.first].insert(EI.second);
     };

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

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

LGTM

@kazutakahirata kazutakahirata merged commit 58f2896 into llvm:main Sep 4, 2024
5 of 8 checks passed
@kazutakahirata kazutakahirata deleted the cleanup_DenseMap_FindAndConstruct_XRay branch September 4, 2024 09:09
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.

3 participants