RESOLVED FIXED297124
[Bindings] Optimize union IDL attribute setters by avoiding conversion to variant
https://bugs.webkit.org/show_bug.cgi?id=297124
Summary [Bindings] Optimize union IDL attribute setters by avoiding conversion to var...
Sam Weinig
Reported 2025-08-08 11:15:38 PDT
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'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<String, RefPtr<CanvasGradient>, RefPtr<CanvasPattern>>; StyleVariant strokeStyle() const; void setStrokeStyle(StyleVariant&&); ``` and what setStrokeStyle has to do is check what type it got and then do something with the style: ``` void CanvasRenderingContext2DBase::setStrokeStyle(CanvasRenderingContext2DBase::StyleVariant&& style) { if (std::holds_alternative<String>(style)) { ... } else if (std::holds_alternative<RefPtr<CanvasGradient>>(style)) { ... } else { ... } ``` Instead, it will now expose 3 setters: ``` using StyleVariant = Variant<String, RefPtr<CanvasGradient>, RefPtr<CanvasPattern>>; StyleVariant strokeStyle() const; void setStrokeStyle(String&&); void setStrokeStyle(RefPtr<CanvasGradient>&&); void setStrokeStyle(RefPtr<CanvasPattern>&&); ``` with no additional checking needed. (This idea originates from Kimmo Kinnunen, all credit to them)
Attachments
Sam Weinig
Comment 1 2025-08-08 11:30:33 PDT
Radar WebKit Bug Importer
Comment 2 2025-08-15 11:16:13 PDT
EWS
Comment 3 2025-10-05 18:59:10 PDT
Committed 301035@main (16afa35bbfd9): <https://commits.webkit.org/301035@main> Reviewed commits have been landed. Closing PR #49126 and removing active labels.
Note You need to log in before you can comment on or make changes to this bug.