Bug 228142 - Micro-optimize innerHTML
Summary: Micro-optimize innerHTML
Status: REOPENED
Alias: None
Product: WebKit
Classification: Unclassified
Component: New Bugs (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Yusuke Suzuki
URL:
Keywords: InRadar
Depends on: 228297
Blocks:
  Show dependency treegraph
 
Reported: 2021-07-21 01:38 PDT by Yusuke Suzuki
Modified: 2021-07-30 13:34 PDT (History)
13 users (show)

See Also:


Attachments
Patch (24.80 KB, patch)
2021-07-21 01:41 PDT, Yusuke Suzuki
no flags Details | Formatted Diff | Diff
Patch (28.91 KB, patch)
2021-07-21 16:00 PDT, Yusuke Suzuki
no flags Details | Formatted Diff | Diff
Patch (32.94 KB, patch)
2021-07-21 16:45 PDT, Yusuke Suzuki
no flags Details | Formatted Diff | Diff
Patch (53.02 KB, patch)
2021-07-22 10:50 PDT, Yusuke Suzuki
no flags Details | Formatted Diff | Diff
Patch (33.34 KB, patch)
2021-07-22 10:52 PDT, Yusuke Suzuki
no flags Details | Formatted Diff | Diff
Patch (33.08 KB, patch)
2021-07-22 11:14 PDT, Yusuke Suzuki
simon.fraser: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Yusuke Suzuki 2021-07-21 01:38:39 PDT
Micro-optimize innerHTML
Comment 1 Yusuke Suzuki 2021-07-21 01:41:00 PDT
Created attachment 433923 [details]
Patch
Comment 2 Yusuke Suzuki 2021-07-21 16:00:21 PDT
Created attachment 433965 [details]
Patch
Comment 3 Yusuke Suzuki 2021-07-21 16:45:15 PDT
Created attachment 433970 [details]
Patch
Comment 4 Simon Fraser (smfr) 2021-07-22 10:33:34 PDT
Comment on attachment 433970 [details]
Patch

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

> Source/WTF/wtf/Vector.h:769
> +    void shrinkToBestFit();

I'd like to see a comment explaining how shrinkToBestFit() is different from shrinkToFit() and when to use it.

> Source/WTF/wtf/text/AtomStringImpl.cpp:130
> +        StringImpl* impl = str.get();

auto?

> Source/WTF/wtf/text/AtomStringImpl.cpp:313
> +        StringImpl* impl = str.get();

auto

> Source/WTF/wtf/text/AtomStringImpl.cpp:338
> +        StringImpl* impl = str.get();

auto
Comment 5 Geoffrey Garen 2021-07-22 10:33:55 PDT
Comment on attachment 433970 [details]
Patch

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

> Source/WTF/wtf/Vector.h:1264
> +    ASSERT(currentSize <= newCapacity);

This function is named "shrink" but this assertion claims that it always grows. And the implementation seems similar to expandCapacity().

Can you explain more about what we're trying to do by calling this function?
Comment 6 Yusuke Suzuki 2021-07-22 10:49:03 PDT
Comment on attachment 433970 [details]
Patch

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

>> Source/WTF/wtf/Vector.h:1264
>> +    ASSERT(currentSize <= newCapacity);
> 
> This function is named "shrink" but this assertion claims that it always grows. And the implementation seems similar to expandCapacity().
> 
> Can you explain more about what we're trying to do by calling this function?

Ah, no. We are calling shrinkCapacity, so even though newCapacity is larger than the current capacity, then it becomes no-op. So, this function is always shrinking.
I've added the following comment here.

    // shrinkToBestFit shrinks the capacity of the Vector as if expandCapacity happens just now. shrinkToFit attempts to make size() == capacity(). Thus,
    // if we append some items to this Vector just after that, we always need to expand the Vector. So shrinkToFit is not appropriate if we know capacity of
    // the Vector is much larger than the size and the Vector can be appended soon. On the other hand, shrinkToBestFit can leave some reasonable size of
    // the capacity.

>> Source/WTF/wtf/text/AtomStringImpl.cpp:130
>> +        StringImpl* impl = str.get();
> 
> auto?

Changed.

>> Source/WTF/wtf/text/AtomStringImpl.cpp:313
>> +        StringImpl* impl = str.get();
> 
> auto

Ditto.

>> Source/WTF/wtf/text/AtomStringImpl.cpp:338
>> +        StringImpl* impl = str.get();
> 
> auto

Ditto.
Comment 7 Yusuke Suzuki 2021-07-22 10:50:44 PDT
Created attachment 434020 [details]
Patch
Comment 8 Yusuke Suzuki 2021-07-22 10:52:23 PDT
Created attachment 434021 [details]
Patch
Comment 9 Simon Fraser (smfr) 2021-07-22 11:01:46 PDT
(In reply to Yusuke Suzuki from comment #6)
> Comment on attachment 433970 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=433970&action=review
> 
> >> Source/WTF/wtf/Vector.h:1264
> >> +    ASSERT(currentSize <= newCapacity);
> > 
> > This function is named "shrink" but this assertion claims that it always grows. And the implementation seems similar to expandCapacity().
> > 
> > Can you explain more about what we're trying to do by calling this function?
> 
> Ah, no. We are calling shrinkCapacity, so even though newCapacity is larger
> than the current capacity, then it becomes no-op. So, this function is
> always shrinking.
> I've added the following comment here.
> 
>     // shrinkToBestFit shrinks the capacity of the Vector as if
> expandCapacity happens just now. shrinkToFit attempts to make size() ==
> capacity(). Thus,
>     // if we append some items to this Vector just after that, we always
> need to expand the Vector. So shrinkToFit is not appropriate if we know
> capacity of
>     // the Vector is much larger than the size and the Vector can be
> appended soon. On the other hand, shrinkToBestFit can leave some reasonable
> size of
>     // the capacity.
> 

Maybe this could be said more succinctly as something like "shrinkToBestFit()" shrinks the capacity to fix the current size, while leaving sufficient capacity for a few more items" - maybe "few" can be more precisely described.
Comment 10 Yusuke Suzuki 2021-07-22 11:13:29 PDT
Comment on attachment 433970 [details]
Patch

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

>>>> Source/WTF/wtf/Vector.h:1264
>>>> +    ASSERT(currentSize <= newCapacity);
>>> 
>>> This function is named "shrink" but this assertion claims that it always grows. And the implementation seems similar to expandCapacity().
>>> 
>>> Can you explain more about what we're trying to do by calling this function?
>> 
>> Ah, no. We are calling shrinkCapacity, so even though newCapacity is larger than the current capacity, then it becomes no-op. So, this function is always shrinking.
>> I've added the following comment here.
>> 
>>     // shrinkToBestFit shrinks the capacity of the Vector as if expandCapacity happens just now. shrinkToFit attempts to make size() == capacity(). Thus,
>>     // if we append some items to this Vector just after that, we always need to expand the Vector. So shrinkToFit is not appropriate if we know capacity of
>>     // the Vector is much larger than the size and the Vector can be appended soon. On the other hand, shrinkToBestFit can leave some reasonable size of
>>     // the capacity.
> 
> Maybe this could be said more succinctly as something like "shrinkToBestFit()" shrinks the capacity to fix the current size, while leaving sufficient capacity for a few more items" - maybe "few" can be more precisely described.

I think "more items" is better since the upperbound of this Vector's capacity is the one as if expandCapacity happens with the current size, so it is not limited to "a few".
This capacity size should be suitable for the Vector which can be appended since this capacity is computed as the same way when we expand the capacity with append call.
Comment 11 Yusuke Suzuki 2021-07-22 11:14:28 PDT
Created attachment 434023 [details]
Patch
Comment 12 Simon Fraser (smfr) 2021-07-22 11:55:17 PDT
Comment on attachment 434023 [details]
Patch

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

> Source/WTF/wtf/Vector.h:771
> +    // "shrinkToBestFit()" shrinks the capacity to fix the current size, while leaving sufficient capacity for more items. The upperbound of the
> +    // capacity is the one as if expandCapacity happens with the current size.

"The upperbound of the capacity is that which would result from calling expandCapacity() with the current size"
Comment 13 Yusuke Suzuki 2021-07-22 12:13:54 PDT
Will land with comment change since the code is not changed from the previous green EWS results.
Comment 14 Yusuke Suzuki 2021-07-22 12:18:53 PDT
Committed r280193 (239881@main): <https://commits.webkit.org/239881@main>
Comment 15 Radar WebKit Bug Importer 2021-07-22 12:19:22 PDT
<rdar://problem/80975442>
Comment 16 Yusuke Suzuki 2021-07-22 13:04:18 PDT
Committed r280194 (239882@main): <https://commits.webkit.org/239882@main>
Comment 17 Geoffrey Garen 2021-07-22 14:05:02 PDT
> > This function is named "shrink" but this assertion claims that it always grows. And the implementation seems similar to expandCapacity().
> > 
> > Can you explain more about what we're trying to do by calling this function?
> 
> Ah, no. We are calling shrinkCapacity, so even though newCapacity is larger
> than the current capacity, then it becomes no-op. So, this function is
> always shrinking.

Got it!

I wonder if "shrinkWithSpareCapacity" would convey this behavior better. "Best" is kind of ambiguous: Best for speed or for memory?
Comment 18 WebKit Commit Bot 2021-07-26 13:16:35 PDT
Re-opened since this is blocked by bug 228297
Comment 19 Yusuke Suzuki 2021-07-26 18:43:44 PDT
Committed r280335 (239982@main): <https://commits.webkit.org/239982@main>
Comment 20 Yusuke Suzuki 2021-07-29 11:17:40 PDT
Reverted.
Comment 21 Simon Fraser (smfr) 2021-07-30 13:34:20 PDT
Reverted in http://trac.webkit.org/r280335