Skip to content

Commit 09ea204

Browse files
committedOct 24, 2018
IR: Optimize FunctionType::get to perform one hash lookup instead of two, NFCI
Summary: This function was performing two hash lookups when a new function type was requested: first checking if it exists and second to insert it. This patch updates the function to perform a single hash lookup in this case by updating the value in the hash table in-place in case the function type was not there before. Reviewers: bkramer Reviewed By: bkramer Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D53471 llvm-svn: 345151
1 parent d7babe4 commit 09ea204

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed
 

Diff for: ‎llvm/lib/IR/Type.cpp

+13-7
Original file line numberDiff line numberDiff line change
@@ -297,20 +297,26 @@ FunctionType::FunctionType(Type *Result, ArrayRef<Type*> Params,
297297
FunctionType *FunctionType::get(Type *ReturnType,
298298
ArrayRef<Type*> Params, bool isVarArg) {
299299
LLVMContextImpl *pImpl = ReturnType->getContext().pImpl;
300-
FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
301-
auto I = pImpl->FunctionTypes.find_as(Key);
300+
const FunctionTypeKeyInfo::KeyTy Key(ReturnType, Params, isVarArg);
302301
FunctionType *FT;
303-
304-
if (I == pImpl->FunctionTypes.end()) {
302+
// Since we only want to allocate a fresh function type in case none is found
303+
// and we don't want to perform two lookups (one for checking if existent and
304+
// one for inserting the newly allocated one), here we instead lookup based on
305+
// Key and update the reference to the function type in-place to a newly
306+
// allocated one if not found.
307+
auto Insertion = pImpl->FunctionTypes.insert_as(nullptr, Key);
308+
if (Insertion.second) {
309+
// The function type was not found. Allocate one and update FunctionTypes
310+
// in-place.
305311
FT = (FunctionType *)pImpl->TypeAllocator.Allocate(
306312
sizeof(FunctionType) + sizeof(Type *) * (Params.size() + 1),
307313
alignof(FunctionType));
308314
new (FT) FunctionType(ReturnType, Params, isVarArg);
309-
pImpl->FunctionTypes.insert(FT);
315+
*Insertion.first = FT;
310316
} else {
311-
FT = *I;
317+
// The function type was found. Just return it.
318+
FT = *Insertion.first;
312319
}
313-
314320
return FT;
315321
}
316322

0 commit comments

Comments
 (0)
Please sign in to comment.