[PATCH] D53471: IR: Optimize FunctionType::get to perform one hash lookup instead of two, NFCI

Krasimir Georgiev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 24 08:21:01 PDT 2018


This revision was automatically updated to reflect the committed changes.
Closed by commit rL345151: IR: Optimize FunctionType::get to perform one hash lookup instead of two, NFCI (authored by krasimir, committed by ).

Repository:
  rL LLVM

https://reviews.llvm.org/D53471

Files:
  llvm/trunk/lib/IR/Type.cpp


Index: llvm/trunk/lib/IR/Type.cpp
===================================================================
--- llvm/trunk/lib/IR/Type.cpp
+++ llvm/trunk/lib/IR/Type.cpp
@@ -297,20 +297,26 @@
 FunctionType *FunctionType::get(Type *ReturnType,
                                 ArrayRef<Type*> Params, bool isVarArg) {
   LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
-  FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
-  auto I = pImpl->FunctionTypes.find_as(Key);
+  const FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
   FunctionType *FT;
-
-  if (I == pImpl->FunctionTypes.end()) {
+  // Since we only want to allocate a fresh function type in case none is found
+  // and we don't want to perform two lookups (one for checking if existent and
+  // one for inserting the newly allocated one), here we instead lookup based on
+  // Key and update the reference to the function type in-place to a newly
+  // allocated one if not found.
+  auto Insertion = pImpl->FunctionTypes.insert_as(nullptr, Key);
+  if (Insertion.second) {
+    // The function type was not found. Allocate one and update FunctionTypes
+    // in-place.
     FT = (FunctionType *)pImpl->TypeAllocator.Allocate(
         sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1),
         alignof(FunctionType));
     new (FT) FunctionType(ReturnType, Params, isVarArg);
-    pImpl->FunctionTypes.insert(FT);
+    *Insertion.first = FT;
   } else {
-    FT = *I;
+    // The function type was found. Just return it.
+    FT = *Insertion.first;
   }
-
   return FT;
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D53471.170899.patch
Type: text/x-patch
Size: 1580 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20181024/345e8bfa/attachment.bin>


More information about the llvm-commits mailing list