[llvm] Add StringMap::lookup() overload to take a default value and return a reference to the value to avoid a copy (PR #115469)
kadir çetinkaya via llvm-commits
llvm-commits at lists.llvm.org
Tue Nov 12 00:30:39 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 {
----------------
kadircet wrote:
IME such `lookup` APIs are usually implemented by having a static default constructed object T and returning a reference to it when the find fails.
This surely has some downsides like never running the destructor until program exit for such objects and eating more memory as people instantiate more stringmaps :/ So I am not sure if it's worth the simplicity of code.
https://github.com/llvm/llvm-project/pull/115469
More information about the llvm-commits
mailing list