REOPENED 228142
Micro-optimize innerHTML
https://bugs.webkit.org/show_bug.cgi?id=228142
Summary Micro-optimize innerHTML
Yusuke Suzuki
Reported 2021-07-21 01:38:39 PDT
Micro-optimize innerHTML
Attachments
Patch (24.80 KB, patch)
2021-07-21 01:41 PDT, Yusuke Suzuki
no flags
Patch (28.91 KB, patch)
2021-07-21 16:00 PDT, Yusuke Suzuki
no flags
Patch (32.94 KB, patch)
2021-07-21 16:45 PDT, Yusuke Suzuki
no flags
Patch (53.02 KB, patch)
2021-07-22 10:50 PDT, Yusuke Suzuki
no flags
Patch (33.34 KB, patch)
2021-07-22 10:52 PDT, Yusuke Suzuki
no flags
Patch (33.08 KB, patch)
2021-07-22 11:14 PDT, Yusuke Suzuki
simon.fraser: review+
Yusuke Suzuki
Comment 1 2021-07-21 01:41:00 PDT
Yusuke Suzuki
Comment 2 2021-07-21 16:00:21 PDT
Yusuke Suzuki
Comment 3 2021-07-21 16:45:15 PDT
Simon Fraser (smfr)
Comment 4 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
Geoffrey Garen
Comment 5 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?
Yusuke Suzuki
Comment 6 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.
Yusuke Suzuki
Comment 7 2021-07-22 10:50:44 PDT
Yusuke Suzuki
Comment 8 2021-07-22 10:52:23 PDT
Simon Fraser (smfr)
Comment 9 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.
Yusuke Suzuki
Comment 10 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.
Yusuke Suzuki
Comment 11 2021-07-22 11:14:28 PDT
Simon Fraser (smfr)
Comment 12 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"
Yusuke Suzuki
Comment 13 2021-07-22 12:13:54 PDT
Will land with comment change since the code is not changed from the previous green EWS results.
Yusuke Suzuki
Comment 14 2021-07-22 12:18:53 PDT
Radar WebKit Bug Importer
Comment 15 2021-07-22 12:19:22 PDT
Yusuke Suzuki
Comment 16 2021-07-22 13:04:18 PDT
Geoffrey Garen
Comment 17 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?
WebKit Commit Bot
Comment 18 2021-07-26 13:16:35 PDT
Re-opened since this is blocked by bug 228297
Yusuke Suzuki
Comment 19 2021-07-26 18:43:44 PDT
Yusuke Suzuki
Comment 20 2021-07-29 11:17:40 PDT
Reverted.
Simon Fraser (smfr)
Comment 21 2021-07-30 13:34:20 PDT
Note You need to log in before you can comment on or make changes to this bug.