Bug 238263 - Avoid unnecessary String constructor under FunctionExecutable::toStringSlow()
Summary: Avoid unnecessary String constructor under FunctionExecutable::toStringSlow()
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Chris Dumez
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2022-03-23 08:56 PDT by Chris Dumez
Modified: 2022-03-23 11:39 PDT (History)
8 users (show)

See Also:


Attachments
Patch (1.75 KB, patch)
2022-03-23 08:57 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (1.70 KB, patch)
2022-03-23 09:53 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Dumez 2022-03-23 08:56:12 PDT
Avoid unnecessary String constructor under FunctionExecutable::toStringSlow().
Comment 1 Chris Dumez 2022-03-23 08:57:12 PDT
Created attachment 455505 [details]
Patch
Comment 2 Darin Adler 2022-03-23 09:02:40 PDT
Comment on attachment 455505 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=455505&action=review

> Source/JavaScriptCore/runtime/FunctionExecutable.cpp:144
> -    String functionHeader;
> +    ASCIILiteral functionHeader = ASCIILiteral::null();

A few other thoughts: Why ASCIILiteral::null() here, but ""_s below? I’m not familiar with the variadic jsMakeNontrivialString. Does jsMakeNontrivialString benefit from this being an ASCIILiteral rather than a const char* or a StringView? Since src is a StringView I get the impression that we don’t need to make a String so not sure why ASCIILiteral is better than const char*. I’m sort of surprised that this idiom is jsMakeNontrivialString instead of just jsNontrivialString(makeString()).

I think I would have done:

    auto functionHeader = "";
Comment 3 Chris Dumez 2022-03-23 09:10:17 PDT
(In reply to Darin Adler from comment #2)
> Comment on attachment 455505 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=455505&action=review
> 
> > Source/JavaScriptCore/runtime/FunctionExecutable.cpp:144
> > -    String functionHeader;
> > +    ASCIILiteral functionHeader = ASCIILiteral::null();
> 
> A few other thoughts: Why ASCIILiteral::null() here, but ""_s below? I’m not
> familiar with the variadic jsMakeNontrivialString. Does
> jsMakeNontrivialString benefit from this being an ASCIILiteral rather than a
> const char* or a StringView? Since src is a StringView I get the impression
> that we don’t need to make a String so not sure why ASCIILiteral is better
> than const char*. I’m sort of surprised that this idiom is
> jsMakeNontrivialString instead of just jsNontrivialString(makeString()).
> 
> I think I would have done:
> 
>     auto functionHeader = "";

Technically, the initialization value doesn't matter much since the switch below handles all enum values. Previously, the initialization value was a null string, not an empty string. ASCIILiteral::null() thus seemed closer to pre-existing code than ""_s.

That said, I don't mind initializing to _""s.

jsMakeNontrivialString() doesn't benefit from using an ASCIILiteral here. Using an ASCIILiteral also doesn't hurt as far as I can tell. Using ASCIILiteral also could be helpful for perf if we ever decide to store the string length as a data member in ASCIILiteral.

I think it is best practice to use _s for all String literals in WebKit. It should always be as good or faster than "". If we have API that is slower with an ASCIILiteral, I think that API would need to be fixed.
Comment 4 Chris Dumez 2022-03-23 09:53:15 PDT
Created attachment 455509 [details]
Patch
Comment 5 EWS 2022-03-23 11:38:32 PDT
Committed r291755 (248786@main): <https://commits.webkit.org/248786@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 455509 [details].
Comment 6 Radar WebKit Bug Importer 2022-03-23 11:39:46 PDT
<rdar://problem/90713418>