<?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>297124</bug_id>
          
          <creation_ts>2025-08-08 11:15:38 -0700</creation_ts>
          <short_desc>[Bindings] Optimize union IDL attribute setters by avoiding conversion to variant</short_desc>
          <delta_ts>2025-10-05 18:59:13 -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>Bindings</component>
          <version>Safari 18</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <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="Sam Weinig">sam</reporter>
          <assigned_to name="Sam Weinig">sam</assigned_to>
          <cc>cdumez</cc>
    
    <cc>webkit-bug-importer</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>2135207</commentid>
    <comment_count>0</comment_count>
    <who name="Sam Weinig">sam</who>
    <bug_when>2025-08-08 11:15:38 -0700</bug_when>
    <thetext>Setters for IDL attributes with a union type can be optimized by taking advantage of the fact that we type check the JS value, at which point we know the concrete final type, but then construct a variant with that value, at which which point we lose that information. This means, the first thing the implementation often does is switch on the variant it was passed to recover that information. Instead, the implementation can expose overloads for each member of the union, thus preserving the information.

For example, for CanvasFillStrokeStyles&apos;s strokeStyle attribute:

```
interface mixin CanvasFillStrokeStyles {
    // colors and styles (see also the CanvasPathDrawingStyles and CanvasTextDrawingStyles interfaces)
    attribute (DOMString or CanvasGradient or CanvasPattern) strokeStyle; // (default black)
    ...
}
```

The implementation currently exposes a getter and setter:

```
    using StyleVariant = Variant&lt;String, RefPtr&lt;CanvasGradient&gt;, RefPtr&lt;CanvasPattern&gt;&gt;;
    StyleVariant strokeStyle() const;
    void setStrokeStyle(StyleVariant&amp;&amp;);
```

and what setStrokeStyle has to do is check what type it got and then do something with the style:

```
void CanvasRenderingContext2DBase::setStrokeStyle(CanvasRenderingContext2DBase::StyleVariant&amp;&amp; style)
{
    if (std::holds_alternative&lt;String&gt;(style)) {
        ...
    } else if (std::holds_alternative&lt;RefPtr&lt;CanvasGradient&gt;&gt;(style)) {
        ...
    } else {
        ...
}
```

Instead, it will now expose 3 setters:

```
    using StyleVariant = Variant&lt;String, RefPtr&lt;CanvasGradient&gt;, RefPtr&lt;CanvasPattern&gt;&gt;;
    StyleVariant strokeStyle() const;
    void setStrokeStyle(String&amp;&amp;);
    void setStrokeStyle(RefPtr&lt;CanvasGradient&gt;&amp;&amp;);
    void setStrokeStyle(RefPtr&lt;CanvasPattern&gt;&amp;&amp;);
```

with no additional checking needed.

(This idea originates from Kimmo Kinnunen, all credit to them)</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2135217</commentid>
    <comment_count>1</comment_count>
    <who name="Sam Weinig">sam</who>
    <bug_when>2025-08-08 11:30:33 -0700</bug_when>
    <thetext>Pull request: https://github.com/WebKit/WebKit/pull/49126</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2136826</commentid>
    <comment_count>2</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2025-08-15 11:16:13 -0700</bug_when>
    <thetext>&lt;rdar://problem/158423078&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>2148600</commentid>
    <comment_count>3</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2025-10-05 18:59:10 -0700</bug_when>
    <thetext>Committed 301035@main (16afa35bbfd9): &lt;https://commits.webkit.org/301035@main&gt;

Reviewed commits have been landed. Closing PR #49126 and removing active labels.</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>