It's really easy to generate temporaries that shouldn't be needed, due to WebString's implicit conversion operators. Several examples I found while looking around:
Inefficient conversions:
base::UTF16ToUTF8(base::StringPiece16(web_string))
should be:
web_string.utf8()
Since the latter is more efficient if the string is already an 8-bit string.
Unnecessary conversions:
bool is_post = base::EqualsASCII(
base::StringPiece16(failed_request.httpMethod()), "POST");
creates a new string16 just to do a comparison.
More unnecessary conversions:
if (!base::IsStringASCII(capability.mimeType) ||
creates a new string16 just to check if the string only contains ASCII chars.
More unnecessary conversions (part 2, in tests):
provider_.RequestTextChecking(blink::WebString(WideToUTF16(kRussianWord)),
&completion,
std::vector<SpellCheckMarker>());
The first parameter of RequestTextChecking is a string16, so we convert a string literal to a string16, then turn it into a WebString, and promptly turn it back into a string16.
I'm not sure if there are other patterns: I was looking at the calls to https://cs.chromium.org/chromium/src/third_party/WebKit/public/platform/WebString.h?l=143&gs=cpp%253Ablink%253A%253Aclass-WebString%253A%253Aoperatorbasic_string()-const%2540chromium%252F..%252F..%252Fthird_party%252FWebKit%252Fpublic%252Fplatform%252FWebString.h%257Cdef&gsn=operator&ct=xref_usages
But there are similar issues with WebString's constructors as well.
We should consider:
- Making conversions explicit
- Change things like IsStringASCII to use containsOnlyASCII()
- Perhaps add equivalents for comparing against ASCII strings
- ???
Comment 1 by csharrison@chromium.org
, Nov 21 2016