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

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 4 02:01:48 PDT 2024


https://github.com/kazutakahirata updated https://github.com/llvm/llvm-project/pull/107178

>From 1f8f2db1677da4ee0eb8e6bfefe3abbb3e9d39cf Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Tue, 3 Sep 2024 20:06:09 -0700
Subject: [PATCH 1/2] [XRay] Use DenseMap::{operator[],try_emplace} (NFC)

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.
---
 llvm/include/llvm/XRay/Graph.h | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

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);
     };

>From 199cbb6c4354d911e3399012530992323773acf4 Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Wed, 4 Sep 2024 02:01:05 -0700
Subject: [PATCH 2/2] Return Edges[i] directly.

---
 llvm/include/llvm/XRay/Graph.h | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/llvm/include/llvm/XRay/Graph.h b/llvm/include/llvm/XRay/Graph.h
index 6c8b178e58ff8c..07b418b8cb6408 100644
--- a/llvm/include/llvm/XRay/Graph.h
+++ b/llvm/include/llvm/XRay/Graph.h
@@ -384,12 +384,11 @@ class Graph {
   /// constructs it, if it's endpoints do not exist it also default constructs
   /// them.
   EdgeAttribute &operator[](const EdgeIdentifier &I) {
-    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 Attr;
+    return Edges[I];
   }
 
   /// Looks up a vertex with Identifier I, or an error if it does not exist.



More information about the llvm-commits mailing list