[llvm] [sancov] Avoid repeated map lookups (NFC) (PR #109906)

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 24 21:27:40 PDT 2024


https://github.com/kazutakahirata created https://github.com/llvm/llvm-project/pull/109906

None

>From 1dec2449cf34e4278041cd19c571fc8d7b16278d Mon Sep 17 00:00:00 2001
From: Kazu Hirata <kazu at google.com>
Date: Tue, 24 Sep 2024 17:44:16 -0700
Subject: [PATCH] [sancov] Avoid repeated map lookups (NFC)

---
 llvm/tools/sancov/sancov.cpp | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

diff --git a/llvm/tools/sancov/sancov.cpp b/llvm/tools/sancov/sancov.cpp
index dd51226e21311f..80f9996ba705ba 100644
--- a/llvm/tools/sancov/sancov.cpp
+++ b/llvm/tools/sancov/sancov.cpp
@@ -323,11 +323,10 @@ static void operator<<(json::OStream &W,
             for (const auto &Loc : Point->Locs) {
               if (Loc.FileName != FileName || Loc.FunctionName != FunctionName)
                 continue;
-              if (WrittenIds.find(Point->Id) != WrittenIds.end())
+              if (!WrittenIds.insert(Point->Id).second)
                 continue;
 
               // Output <point_id> : "<line>:<col>".
-              WrittenIds.insert(Point->Id);
               W.attribute(Point->Id,
                           (utostr(Loc.Line) + ":" + utostr(Loc.Column)));
             }
@@ -418,9 +417,6 @@ SymbolizedCoverage::read(const std::string &InputFile) {
             auto LineStr = Loc.substr(0, ColonPos);
             auto ColStr = Loc.substr(ColonPos + 1, Loc.size());
 
-            if (Points.find(PointId) == Points.end())
-              Points.insert(std::make_pair(PointId, CoveragePoint(PointId)));
-
             DILineInfo LineInfo;
             LineInfo.FileName = Filename;
             LineInfo.FunctionName = FunctionName;
@@ -428,7 +424,8 @@ SymbolizedCoverage::read(const std::string &InputFile) {
             LineInfo.Line = std::strtoul(LineStr.c_str(), &End, 10);
             LineInfo.Column = std::strtoul(ColStr.c_str(), &End, 10);
 
-            CoveragePoint *CoveragePoint = &Points.find(PointId)->second;
+            CoveragePoint *CoveragePoint =
+                &Points.try_emplace(PointId, PointId).first->second;
             CoveragePoint->Locs.push_back(LineInfo);
           }
         }
@@ -576,10 +573,8 @@ getCoveragePoints(const std::string &ObjectFile,
       FrameInfo.FileName = normalizeFilename(FrameInfo.FileName);
       if (Ig.isIgnorelisted(FrameInfo))
         continue;
-      if (Infos.find(FrameInfo) == Infos.end()) {
-        Infos.insert(FrameInfo);
+      if (Infos.insert(FrameInfo).second)
         Point.Locs.push_back(FrameInfo);
-      }
     }
 
     Result.push_back(Point);



More information about the llvm-commits mailing list