<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>241416</bug_id>
          
          <creation_ts>2022-06-08 06:48:24 -0700</creation_ts>
          <short_desc>[meta] IPC encoders could be simplified / sped-up</short_desc>
          <delta_ts>2022-06-08 17:12:35 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>WebKit2</component>
          <version>Other</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          <see_also>https://bugs.webkit.org/show_bug.cgi?id=241407</see_also>
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Jean-Yves Avenard [:jya]">jean-yves.avenard</reporter>
          <assigned_to name="Nobody">webkit-unassigned</assigned_to>
          <cc>cdumez</cc>
    
    <cc>fujii</cc>
    
    <cc>kkinnunen</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1874710</commentid>
    <comment_count>0</comment_count>
    <who name="Jean-Yves Avenard [:jya]">jean-yves.avenard</who>
    <bug_when>2022-06-08 06:48:24 -0700</bug_when>
    <thetext>As seen with bug 241407.

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

Considering a struct of type T:

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

followed by something like:

```
template&lt;class Decoder&gt; std::optional&lt;T&gt; T::decode(Decoder&amp; 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&apos;s unlikely to ever be false.

Instead we could do something like:
```
{
    auto param1 = decoder.decode&lt;Param1&gt;();
    auto param2 = decoder.decode&lt;Param2&gt;();
    auto param3 = decoder.decode&lt;Param3&gt;();
    auto param4 = decoder.decode&lt;Param4&gt;();
    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.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1874711</commentid>
    <comment_count>1</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2022-06-08 06:48:44 -0700</bug_when>
    <thetext>&lt;rdar://problem/94629221&gt;</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>