Bug 232424

Summary: Have CachedRawResourceClient and related networking actors use SharedBuffer.
Product: WebKit Reporter: Jean-Yves Avenard [:jya] <jean-yves.avenard>
Component: WebCore Misc.Assignee: Jean-Yves Avenard [:jya] <jean-yves.avenard>
Status: RESOLVED FIXED    
Severity: Normal CC: achristensen, benjamin, calvaris, cdumez, cgarcia, darin, dino, eric.carlson, ews-watchlist, fmalita, galpeter, ggaren, glenn, gustavo, gyuyoung.kim, hi, japhet, jer.noble, joepeck, menard, pangle, pdr, philipj, pnormand, sabouhallawa, schenney, sergio, toyoshim, vjaquez, webkit-bug-importer, youennf, yutak
Priority: P2 Keywords: InRadar
Version: Other   
Hardware: Unspecified   
OS: Unspecified   
See Also: https://bugs.webkit.org/show_bug.cgi?id=232422
https://bugs.webkit.org/show_bug.cgi?id=230121
https://bugs.webkit.org/show_bug.cgi?id=235615
Bug Depends on: 233030, 233441, 234724, 234916    
Bug Blocks: 234713    
Attachments:
Description Flags
WIP
ews-feeder: commit-queue-
WIP
none
WIP
none
WIP
ews-feeder: commit-queue-
WIP
none
WIP
none
Patch for EWS test (combined)
none
Patch For Review
ews-feeder: commit-queue-
Patch For Review
none
Patch For EWS (combined patch)
none
Patch for review
none
Patch for EWS testing (combined patch)
none
Patch
none
rebase none

Description Jean-Yves Avenard [:jya] 2021-10-27 23:39:56 PDT
As seen in bug 232422.

By using SharedBuffer with CachedRawResourceClient we could eliminate some unnecessary memory allocation and copies.
Comment 1 Radar WebKit Bug Importer 2021-10-27 23:40:20 PDT
<rdar://problem/84744687>
Comment 2 Jean-Yves Avenard [:jya] 2021-12-22 04:45:59 PST
Created attachment 447793 [details]
WIP
Comment 3 Jean-Yves Avenard [:jya] 2021-12-22 05:06:40 PST
Created attachment 447795 [details]
WIP

fix gtk build
Comment 4 Jean-Yves Avenard [:jya] 2021-12-22 16:24:25 PST
I think the crash seeing here are similar to those seen in bug 230121.

The underlying vector gets modified. I suspect a call to SharedBuffer::take() in the way.
Comment 5 Jean-Yves Avenard [:jya] 2021-12-23 03:46:26 PST
Created attachment 447868 [details]
WIP
Comment 6 Jean-Yves Avenard [:jya] 2021-12-23 05:52:22 PST
Created attachment 447875 [details]
WIP

rebase
Comment 7 Jean-Yves Avenard [:jya] 2021-12-23 06:09:22 PST
Created attachment 447876 [details]
WIP
Comment 8 Jean-Yves Avenard [:jya] 2021-12-27 06:12:31 PST
Created attachment 447999 [details]
WIP
Comment 9 Jean-Yves Avenard [:jya] 2021-12-27 17:39:11 PST
Created attachment 448015 [details]
Patch for EWS test (combined)
Comment 10 Jean-Yves Avenard [:jya] 2021-12-27 18:37:36 PST
Created attachment 448018 [details]
Patch For Review
Comment 11 Jean-Yves Avenard [:jya] 2021-12-27 19:17:05 PST
Created attachment 448022 [details]
Patch For Review
Comment 12 Jean-Yves Avenard [:jya] 2021-12-27 19:18:07 PST
Created attachment 448023 [details]
Patch For EWS (combined patch)
Comment 13 Jean-Yves Avenard [:jya] 2021-12-28 19:02:09 PST
Created attachment 448060 [details]
Patch for review
Comment 14 Jean-Yves Avenard [:jya] 2021-12-28 19:03:27 PST
Created attachment 448061 [details]
Patch for EWS testing (combined patch)
Comment 15 Jean-Yves Avenard [:jya] 2022-01-05 20:04:09 PST
Created attachment 448464 [details]
Patch

rebase
Comment 16 youenn fablet 2022-01-06 01:59:38 PST
Comment on attachment 448464 [details]
Patch

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

> Source/WebCore/Modules/fetch/FetchLoader.cpp:151
> +void FetchLoader::didReceiveData(const SharedBuffer& buffer, bool)

This bool is mysterious.
Ideally, we would remove it, or use a bool enum.

> Source/WebCore/loader/DocumentThreadableLoader.cpp:454
> +    m_client->didReceiveData(buffer, false);

It does not seem that we have a case where we call didReceiveData(buffer, true).
Maybe we can just not pass this bool, especially since we probably no longer have -1 as the length in buffer.

> Source/WebCore/workers/WorkerScriptLoader.cpp:218
> +    size_t len = useStrLen ? strlen(reinterpret_cast<const char*>(buffer.data())) : buffer.size();

Can we try removing this -1 code path?

> Source/WebCore/xml/XMLHttpRequest.cpp:1058
> +    size_t len = useStrLen ? strlen(reinterpret_cast<const char*>(buffer.data())) : buffer.size();

Can we try removing this -1 code path?

> Source/WebKit/NetworkProcess/ServiceWorker/ServiceWorkerSoftUpdateLoader.cpp:189
> +    if (!buffer.isEmpty()) {

Should we move that check in forEachSegment?
Or are we expecting that other forEachSegment call sites will not benefit of this?
Comment 17 Jean-Yves Avenard [:jya] 2022-01-06 05:25:12 PST
Created attachment 448488 [details]
rebase
Comment 18 EWS 2022-01-06 07:32:55 PST
Committed r287684 (245780@main): <https://commits.webkit.org/245780@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 448488 [details].
Comment 19 Chris Dumez 2022-01-25 14:19:00 PST
Comment on attachment 448488 [details]
rebase

Aren't we creating an extra copy of the data whenever we construct the SharedBuffer now? note that the long term place for these didReceiveData() functions was to pass a Span<const uint8_t> (which is nicer to deal with and yet doesn't create a copy).

Note that this regressed memory (rdar://87830583). It is unclear to me if the issue is due to this extra copying or if there is some other bug. Either way, I do think it is a bad idea to create a copy of the data unconditionally and unnecessarily in many cases.
Comment 20 Jean-Yves Avenard [:jya] 2022-01-25 17:08:42 PST
(In reply to Chris Dumez from comment #19)
> Comment on attachment 448488 [details]
> rebase
> 
> Aren't we creating an extra copy of the data whenever we construct the
> SharedBuffer now? note that the long term place for these didReceiveData()
> functions was to pass a Span<const uint8_t> (which is nicer to deal with and
> yet doesn't create a copy).

No, the SharedBuffer is wrapping the SharedMemory as it comes from the network process.

There's no new memory allocation compare to what was there before, only less.

The data is never copied once in the content process, it's refcounted instead.

> 
> Note that this regressed memory (rdar://87830583). It is unclear to me if
> the issue is due to this extra copying or if there is some other bug. Either
> way, I do think it is a bad idea to create a copy of the data
> unconditionally and unnecessarily in many cases.

we do not create a copy.