Bug 239426

Summary: Leverage StringView in more places
Product: WebKit Reporter: Chris Dumez <cdumez>
Component: WebCore Misc.Assignee: Chris Dumez <cdumez>
Status: RESOLVED FIXED    
Severity: Normal CC: aboxhall, achristensen, alecflett, andresg_22, apinheiro, beidson, benjamin, cfleizach, changseok, clopez, cmarcelo, darin, dino, dmazzoni, eric.carlson, esprehn+autocc, ews-watchlist, fmalita, galpeter, ggaren, glenn, gyuyoung.kim, hi, jcraig, jdiggs, jer.noble, joepeck, jsbell, macpherson, menard, mifenton, pangle, pdr, philipj, sabouhallawa, samuel_white, sam, schenney, sergio, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
ews-feeder: commit-queue-
Patch
ews-feeder: commit-queue-
Patch
none
Patch
none
Patch
none
Patch
ews-feeder: commit-queue-
Patch none

Description Chris Dumez 2022-04-16 17:44:20 PDT
Leverage StringView in more places, to reduce the number of String allocations.
Comment 1 Chris Dumez 2022-04-16 17:49:10 PDT
Created attachment 457760 [details]
Patch
Comment 2 Chris Dumez 2022-04-16 17:58:25 PDT
Created attachment 457761 [details]
Patch
Comment 3 Chris Dumez 2022-04-16 19:45:48 PDT
Created attachment 457762 [details]
Patch
Comment 4 Chris Dumez 2022-04-16 20:56:48 PDT
Created attachment 457764 [details]
Patch
Comment 5 Chris Dumez 2022-04-17 00:01:17 PDT
Created attachment 457767 [details]
Patch
Comment 6 Sam Weinig 2022-04-17 14:15:14 PDT
Comment on attachment 457767 [details]
Patch

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

> Source/WebCore/Modules/applicationmanifest/ApplicationManifestParser.cpp:148
> +    stringValue = StringView(stringValue).stripWhiteSpace().convertToASCIILowercase();

Going through the convertToASCIILowercase() and seeing how many can be converted to either explicit equalIgnoringASCIICase() or case-ignore SortedArrayMap would be a good investment.

> Source/WebCore/Modules/applicationmanifest/ApplicationManifestParser.cpp:234
> +                purposeStringValue = StringView(purposeStringValue).stripWhiteSpace().convertToASCIILowercase();
>                  Vector<String> keywords = purposeStringValue.splitAllowingEmptyEntries(" ");

This could totally be done without any allocation using SortedArrayMap and StringView's functor based split.

> Source/WebCore/editing/Editor.cpp:3224
> +    String transposed = makeString(text[1], text[0]);

Unrelated to this change, but it seems like this is totally not going to work with all graphemes. I bet transposing (control-t) two emoji doesn't work. Yep.

> Source/WebCore/platform/network/CacheValidation.cpp:301
> +                double maxAge = directives[i].second.toDouble(ok);

Another good project for the future is replacing these out parameter based conversion functions with std:;optional returning ones.

> Source/WebCore/platform/network/MIMEHeader.cpp:125
> +    auto encoding = text.stripWhiteSpace();

Another good SortedArrayMap use case for the future.

> Source/WebCore/platform/sql/SQLiteDatabase.cpp:464
> +        if (!executeCommandSlow(makeString("DROP TABLE ", table)))

Because string concatenation and databases make me very uncomfortable, can this / should this use prepareStatement + '?' + bindText()? (not relevant to this change, but a question none-the-less).

> Source/WebCore/platform/xr/PlatformXR.h:120
> +    auto feature = string.stripWhiteSpace().convertToASCIILowercase();

This would be more efficient / idiomatic if we switch to use a case insensitive SortedArrayMap (for the future, not relevant to the kind of changes you are making).
Comment 7 Chris Dumez 2022-04-17 14:32:26 PDT
(In reply to Sam Weinig from comment #6)
> Comment on attachment 457767 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=457767&action=review
> 
> > Source/WebCore/Modules/applicationmanifest/ApplicationManifestParser.cpp:148
> > +    stringValue = StringView(stringValue).stripWhiteSpace().convertToASCIILowercase();
> 
> Going through the convertToASCIILowercase() and seeing how many can be
> converted to either explicit equalIgnoringASCIICase() or case-ignore
> SortedArrayMap would be a good investment.
> 
> > Source/WebCore/Modules/applicationmanifest/ApplicationManifestParser.cpp:234
> > +                purposeStringValue = StringView(purposeStringValue).stripWhiteSpace().convertToASCIILowercase();
> >                  Vector<String> keywords = purposeStringValue.splitAllowingEmptyEntries(" ");
> 
> This could totally be done without any allocation using SortedArrayMap and
> StringView's functor based split.
> 
> > Source/WebCore/editing/Editor.cpp:3224
> > +    String transposed = makeString(text[1], text[0]);
> 
> Unrelated to this change, but it seems like this is totally not going to
> work with all graphemes. I bet transposing (control-t) two emoji doesn't
> work. Yep.
> 
> > Source/WebCore/platform/network/CacheValidation.cpp:301
> > +                double maxAge = directives[i].second.toDouble(ok);
> 
> Another good project for the future is replacing these out parameter based
> conversion functions with std:;optional returning ones.
> 
> > Source/WebCore/platform/network/MIMEHeader.cpp:125
> > +    auto encoding = text.stripWhiteSpace();
> 
> Another good SortedArrayMap use case for the future.
> 
> > Source/WebCore/platform/sql/SQLiteDatabase.cpp:464
> > +        if (!executeCommandSlow(makeString("DROP TABLE ", table)))
> 
> Because string concatenation and databases make me very uncomfortable, can
> this / should this use prepareStatement + '?' + bindText()? (not relevant to
> this change, but a question none-the-less).

As far as I know (and just double checked on google), it isn't possible to prepare a statement and bind a table name. It only works for parameters, not table names.

> > Source/WebCore/platform/xr/PlatformXR.h:120
> > +    auto feature = string.stripWhiteSpace().convertToASCIILowercase();
> 
> This would be more efficient / idiomatic if we switch to use a case
> insensitive SortedArrayMap (for the future, not relevant to the kind of
> changes you are making).
Comment 8 Chris Dumez 2022-04-17 15:18:43 PDT
Created attachment 457780 [details]
Patch
Comment 9 Chris Dumez 2022-04-17 16:46:36 PDT
Created attachment 457783 [details]
Patch
Comment 10 Chris Dumez 2022-04-17 17:10:29 PDT
(In reply to Sam Weinig from comment #6)
> Comment on attachment 457767 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=457767&action=review
> 
> > Source/WebCore/Modules/applicationmanifest/ApplicationManifestParser.cpp:148
> > +    stringValue = StringView(stringValue).stripWhiteSpace().convertToASCIILowercase();
> 
> Going through the convertToASCIILowercase() and seeing how many can be
> converted to either explicit equalIgnoringASCIICase() or case-ignore
> SortedArrayMap would be a good investment.

I wish I understood how to use SortedArrayMap with string literal keys...
ComparableASCIILiteral / ComparableCaseFoldingASCIILiteral / ComparableLettersLiteral with no explanation of what each is for.

Which ones are case sensitive and which ones are not for e.g. Probably obvious to some people but not to me. 

I looked at the SortedArrayMap API test, hoping it would help me understand. Sadly, only one test is actually trying uppercase and it is with ComparableLettersLiteral and somehow it matches. So somehow ComparableLettersLiteral is case-insensitive, which I find surprising.
Then what is ComparableCaseFoldingASCIILiteral for?

As often with WebKit code, I wish there was a smidge of documentation :)
Comment 11 EWS 2022-04-17 22:57:04 PDT
Committed r292951 (249716@main): <https://commits.webkit.org/249716@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 457783 [details].
Comment 12 Radar WebKit Bug Importer 2022-04-17 22:58:14 PDT
<rdar://problem/91878229>
Comment 13 Carlos Alberto Lopez Perez 2022-04-18 04:47:50 PDT
Committed r292952 (249717@trunk): <https://commits.webkit.org/249717@trunk>
Comment 14 Darin Adler 2022-04-18 09:46:31 PDT
(In reply to Chris Dumez from comment #10)
> I wish I understood how to use SortedArrayMap with string literal keys...
> ComparableASCIILiteral / ComparableCaseFoldingASCIILiteral /
> ComparableLettersLiteral with no explanation of what each is for.

I’m happy to write more documentation.

> Which ones are case sensitive and which ones are not for e.g. Probably
> obvious to some people but not to me. 

ComparableASCIILiteral is case sensitive
ComparableCaseFoldingASCIILiteral is case insensitive
ComparableLettersLiteral is case insensitive

We can also make new ones; there is nothing special about this.

> So somehow
> ComparableLettersLiteral is case-insensitive, which I find surprising.
> Then what is ComparableCaseFoldingASCIILiteral for?

This is like equalIgnoringASCIICase and equalLettersIgnoringASCIICase.

ComparableCaseFoldingASCIILiteral is case-insensitve

ComparableLettersLiteral is case insensitive and more efficient when the keys are all guaranteed to only include the ASCII characters we can compare with the high speed "just or with 0x20" approach. We can give it a longer more-descriptive name. I think your complaints mostly boil down to unhappiness with that name.

> As often with WebKit code, I wish there was a smidge of documentation :)

Happy to add it.

We can also make additional ComparableXXX classes that build whitespace skipping into the canonicalization and comparison functions.