>>107173236
Neat! When you call GetSharedString with a std::string, it passes by reference, and then copies the underlying pointer to the string_view. However, when your cache has a miss, it converts back from string_view to string, which is a copy. You can make this copy optional by passing the string by value instead:
std::string_view GetSharedString(std::string value) {
/*...*/
stringset.emplace(std::move(value)); // no copy, only a move
}
then you can call like this:
std::string myStr = "Hello world";
stringManager.GetSharedString(myStr); // copy the data here, immediately, gets moved later
//or
stringManager.GetSharedString(std::move(myStr)); // move the string into the parameter, then it gets moved internally
The mutation semantics are a bit clearer, and you get the option of zero-cost insertions: if you pass by value, you know your string will never be changed. If you explicitly move it, you know that your string gets mutated, but you get the benefit of not having to copy the data
Also for string literals, this may not even be necessary because modern compilers support string pooling