[llvm] Add StringMap::lookup() overload to take a default value and return a reference to the value to avoid a copy (PR #115469)

Boaz Brickner via llvm-commits llvm-commits at lists.llvm.org
Mon Nov 11 01:14:29 PST 2024


================
@@ -257,6 +257,15 @@ class LLVM_ALLOCATORHOLDER_EMPTYBASE StringMap
     return ValueTy();
   }
 
+  /// lookup - Return the entry for the specified key, or a default
+  /// provided value if no such entry exists.
+  const ValueTy &lookup(StringRef Key, const ValueTy& Value) const {
----------------
bricknerb wrote:

I was aiming to return by reference following the discussion in https://github.com/llvm/llvm-project/pull/112517#discussion_r1832610617.
When the value is costly to copy, even if the default value is cheap, returning by value would be costly.
I assume there would be a lot of use cases for this and it could simplify the code.
I don't feel strongly about the name.
Returning a pointer would work, though it seems to be somewhat less elegant. Do you mean that also the default value would be a pointer?

https://github.com/llvm/llvm-project/pull/115469


More information about the llvm-commits mailing list