Bug 238993

Summary: [WK2] Store sync-message reply arguments inside the SendSyncResult object
Product: WebKit Reporter: Zan Dobersek (Reviews) <zdobersek>
Component: New BugsAssignee: Zan Dobersek (Reviews) <zdobersek>
Status: RESOLVED FIXED    
Severity: Normal CC: achristensen, cdumez, kkinnunen, sam, simon.fraser, webkit-bug-importer, zan
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
WIP patch
none
WIP patch
ews-feeder: commit-queue-
WIP patch
none
Patch
none
Patch zan: review?

Description Zan Dobersek (Reviews) 2022-04-08 03:54:38 PDT
[WK2] Use structured bindings to access synchronous-message reply arguments
Comment 1 Zan Dobersek (Reviews) 2022-04-08 03:55:13 PDT
Created attachment 457040 [details]
WIP patch
Comment 2 Zan Dobersek 2022-04-12 04:40:13 PDT
Created attachment 457325 [details]
WIP patch
Comment 3 Zan Dobersek 2022-04-12 05:01:30 PDT
This one is still just a prototype, and some feedback on the general idea would be appreciated.


When dispatching synchronous messages, right now the expectation is to also provide a tuple of references to variables where the reply arguments should be stored. This normally requires initializing each such variable either through default initialization or assigning it some default value.

Reply is processed by essentially decoding whatever is sent back and then moving the resulted values into the prepared variables that were passed through the tuple of references.


Proposed change here changes the way the reply values are made available to the caller. Decoding is now done into a single tuple object, and the caller can access those values after validating the message (and reply) went through successfully.

When valid, the reply values stored in the tuple can be referenced or moved out. Through takeReplyArgumentsOr() it's also possible to either move the tuple out of the result struct or construct a tuple of the given default values for each reply argument, depending if the message is valid or not.

It's also possible to combine this with the C++17 structured bindings and std::tie(), establishing references to values inside the reply tuple or simply moving the values out into local variables that can then be moved into further operations. This can clear out the code a bit, avoiding e.g. defining variables that then aren't ever read because of an IPC failure. It could also possibly avoid some extra use of std::optional<> in reply arguments that's nowadays used because the default constructor for a given reply type isn't viable.


One downside is the loss of type info that's currently there for every local reply argument variable. Structured binding leverages auto, so there's no good way of immediately knowing what the specific type is if you're not a compiler. And as with other similar cases in C++, it can be too easy to forget an ampersand and perform a copy instead of establishing a reference for each reply argument.
Comment 4 Zan Dobersek 2022-04-12 05:22:54 PDT
Created attachment 457326 [details]
WIP patch
Comment 5 Kimmo Kinnunen 2022-04-12 09:22:44 PDT
Comment on attachment 457326 [details]
WIP patch

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

looks good by me.
maybe we need to still run it by other developers.
marking it as r? to get feedback

> Source/WebKit/ChangeLog:3
> +        [WK2] Use structured bindings to access synchronous-message reply arguments

I think it should highlight that the benefit is perhaps not to use structured bindings per se, rather being able to receive non-default constructible objects and avoid the default construction.

> Source/WebKit/Platform/IPC/Connection.h:270
> +    struct SendSyncReply {

If possible, I think this should be just called SendSyncResult
and as a implementation detail it'd hold the std::unique_ptr<Decoder> m_decoder;
and then the above sendSync should be removed, once all call sites are changed to the other form.
Comment 6 Zan Dobersek 2022-04-12 09:28:53 PDT
Comment on attachment 457326 [details]
WIP patch

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

>> Source/WebKit/Platform/IPC/Connection.h:270
>> +    struct SendSyncReply {
> 
> If possible, I think this should be just called SendSyncResult
> and as a implementation detail it'd hold the std::unique_ptr<Decoder> m_decoder;
> and then the above sendSync should be removed, once all call sites are changed to the other form.

Should be possible, parallel structure and methods are used right now to keep changes limited to only a couple of places in WebKit.
Comment 7 Radar WebKit Bug Importer 2022-04-15 03:55:14 PDT
<rdar://problem/91802564>
Comment 8 Zan Dobersek 2022-04-28 00:27:12 PDT
Created attachment 458497 [details]
Patch
Comment 9 Zan Dobersek 2022-04-28 02:17:54 PDT
Created attachment 458503 [details]
Patch

With the WinCairo fix.
Comment 10 Alex Christensen 2022-04-28 09:56:25 PDT
I like this direction.
Comment 11 Zan Dobersek 2022-09-20 03:05:47 PDT
Pull request: https://github.com/WebKit/WebKit/pull/4523
Comment 12 EWS 2022-09-23 04:58:02 PDT
Committed 254783@main (c3ae481a1d05): <https://commits.webkit.org/254783@main>

Reviewed commits have been landed. Closing PR #4523 and removing active labels.