imageworks/OpenShadingLanguage — pull request #361 by lgritz (2014-04-27 01:07:57+00:00)
Title: Use string_ref for most public API input parameters

The string_ref class was was put in OIIO a while back (and harmlessly backported to OIIO 1.3 as well, to accommodate anybody building OSL 1.4 against OIIO 1.3).

Here I'm just using it for some public OSL API functions, where we had dubiously used std::string before.

In general, string_ref solves several problems:
1. Avoids redundant std::string construction (including allocation/deletion) when the API takes a std::string& but the calling function only has a char* (including when passing a string literal).
2. It's "safer" since it includes a length internally (the callee knows exactly how many characters are being passed)..
3. Can be more efficient if the callee needs to know the length (especially if this might happen multiple times).
4. Presents a single uniform interface that can take a char*, a std::string, or a ustring as an argument, all very efficiently.

C++17 is shaping up to feature a similar class (unfortunately renamed at a late date from string_ref to string_view -- some day we'll deal with this) and so this kind of passing of character sequences to yield safer, more efficient code is going to be the norm across the whole C++ culture as we move forward.