[PATCH] D157951: [NFC][TLI] Replace std::lower_bound call in getLibFunc with DenseMap lookup

Dhruv Chawla via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 14 23:42:07 PDT 2023


0xdc03 created this revision.
0xdc03 added a reviewer: nikic.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
0xdc03 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

While std::lower_bound takes logarithmic time (relative to the length of
the array) to execute, DenseMap gives better performance characteristics
as it traverses few (if any) elements when collisions do occur,
especially when the number of elements are known in advance.

This gives a speedup of 0.24%:
https://llvm-compile-time-tracker.com/compare.php?from=e63382542f0941b550839a226d0e2ad02a9de328&to=e8dfa8f14cafa0c4ab618eaf8bfb35e16d83bcf4&stat=instructions:u


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D157951

Files:
  llvm/lib/Analysis/TargetLibraryInfo.cpp


Index: llvm/lib/Analysis/TargetLibraryInfo.cpp
===================================================================
--- llvm/lib/Analysis/TargetLibraryInfo.cpp
+++ llvm/lib/Analysis/TargetLibraryInfo.cpp
@@ -11,6 +11,7 @@
 //===----------------------------------------------------------------------===//
 
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/ADT/DenseMap.h"
 #include "llvm/IR/Constants.h"
 #include "llvm/InitializePasses.h"
 #include "llvm/Support/CommandLine.h"
@@ -945,11 +946,18 @@
   if (funcName.empty())
     return false;
 
-  const auto *Start = std::begin(StandardNames);
-  const auto *End = std::end(StandardNames);
-  const auto *I = std::lower_bound(Start, End, funcName);
-  if (I != End && *I == funcName) {
-    F = (LibFunc)(I - Start);
+  static bool IsInitialized = false;
+  static DenseMap<StringRef, unsigned> Indices;
+  if (!IsInitialized) {
+    unsigned Idx = 0;
+    Indices.reserve(LibFunc::NumLibFuncs);
+    for (const auto &Func : StandardNames)
+      Indices[Func] = Idx++;
+    IsInitialized = true;
+  }
+
+  if (auto Loc = Indices.find(funcName); Loc != Indices.end()) {
+    F = (LibFunc)Loc->second;
     return true;
   }
   return false;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D157951.550196.patch
Type: text/x-patch
Size: 1205 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230815/d9db133a/attachment.bin>


More information about the llvm-commits mailing list