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

Kazu Hirata via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 8 11:20:57 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 {
----------------
kazutakahirata wrote:

I'm wondering if you are OK with `lookup_or` returning by value.

- One minor thing that's worrying me is that the name `lookup` doesn't suggest that it accepts a default value.
- `std::optional<T>::value_or` returns by value, so by adopting the same behavior, we don't have to worry about the dangling reference.

Are you planning to use the new function for big structs?

I don't have a strong opinion here.

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


More information about the llvm-commits mailing list