Bug 241416 - [meta] IPC encoders could be simplified / sped-up
Summary: [meta] IPC encoders could be simplified / sped-up
Status: NEW
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit2 (show other bugs)
Version: Other
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Nobody
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2022-06-08 06:48 PDT by Jean-Yves Avenard [:jya]
Modified: 2022-06-08 17:12 PDT (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jean-Yves Avenard [:jya] 2022-06-08 06:48:24 PDT
As seen with bug 241407.

The vast majority of IPC encoders/decoders do the following:

Considering a struct of type T:

```
template<class Encoder> inline void T::encode(T& encoder) const
{
    encoder << param1 << param2 << param3 << param4;
}
```

followed by something like:

```
template<class Decoder> std::optional<T> T::decode(Decoder& decoder)
{
    Param1 param1;
    if (!decoder.decode(param1))
        return { };

    Param1 param2;
    if (!decoder.decode(param2))
        return { };

    Param1 param3;
    if (!decoder.decode(param3))
        return { };

    Param1 param4;
    if (!decoder.decode(param4))
        return { };

    return T { WTFMove(param1), WTFMove(param2), WTFMove(param3), WTFMove(param4) } 
}
```

after every deserialisation a test is performed when it's unlikely to ever be false.

Instead we could do something like:
```
{
    auto param1 = decoder.decode<Param1>();
    auto param2 = decoder.decode<Param2>();
    auto param3 = decoder.decode<Param3>();
    auto param4 = decoder.decode<Param4>();
    if (UNLIKELY(!decoder.isValid()))
        return std::nullopt;

    return T { WTFMove(*param1), WTFMove(*param2), WTFMove(*param3), WTFMove(*param4 };
}
```

it makes the core more readable and allows to only test once for the unlikely case where we have an error.
Comment 1 Radar WebKit Bug Importer 2022-06-08 06:48:44 PDT
<rdar://problem/94629221>