[webkit-changes] [WebKit/WebKit] f15f5a: Convert scroll-padding/scroll-margin types to stro...

Sam Weinig noreply at github.com
Mon Jan 13 20:17:27 PST 2025


  Branch: refs/heads/main
  Home:   https://github.com/WebKit/WebKit
  Commit: f15f5a852578c4770b1c84cfa5442ebf73375745
      https://github.com/WebKit/WebKit/commit/f15f5a852578c4770b1c84cfa5442ebf73375745
  Author: Sam Weinig <sam at webkit.org>
  Date:   2025-01-13 (Mon, 13 Jan 2025)

  Changed paths:
    M Source/WTF/WTF.xcodeproj/project.pbxproj
    M Source/WTF/wtf/CMakeLists.txt
    A Source/WTF/wtf/FlatteningVariantAdaptor.h
    M Source/WTF/wtf/Forward.h
    M Source/WTF/wtf/StdLibExtras.h
    M Source/WTF/wtf/VariantExtras.h
    M Source/WebCore/CMakeLists.txt
    M Source/WebCore/Headers.cmake
    M Source/WebCore/SaferCPPExpectations/RefCntblBaseVirtualDtorExpectations
    M Source/WebCore/Sources.txt
    M Source/WebCore/WebCore.xcodeproj/project.pbxproj
    M Source/WebCore/animation/ViewTimeline.cpp
    M Source/WebCore/css/CSSColorSchemeValue.h
    M Source/WebCore/css/CSSProperties.json
    A Source/WebCore/css/CSSScrollMarginEdgeValue.cpp
    A Source/WebCore/css/CSSScrollMarginEdgeValue.h
    A Source/WebCore/css/CSSScrollPaddingEdgeValue.cpp
    A Source/WebCore/css/CSSScrollPaddingEdgeValue.h
    M Source/WebCore/css/CSSValue.cpp
    M Source/WebCore/css/CSSValue.h
    M Source/WebCore/css/ComputedStyleExtractor.cpp
    M Source/WebCore/css/calc/CSSCalcTree+Simplification.cpp
    M Source/WebCore/css/calc/CSSCalcTree+Simplification.h
    M Source/WebCore/css/calc/CSSCalcValue.cpp
    M Source/WebCore/css/calc/CSSCalcValue.h
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+ScrollSnap.cpp
    M Source/WebCore/css/parser/CSSPropertyParserConsumer+ScrollSnap.h
    M Source/WebCore/css/typedom/CSSStyleValueFactory.cpp
    M Source/WebCore/css/values/CSSValueAggregates.h
    M Source/WebCore/css/values/CSSValueConcepts.h
    A Source/WebCore/css/values/primitives/CSSPrimitiveData.h
    A Source/WebCore/css/values/primitives/CSSPrimitiveKeywordList.h
    A Source/WebCore/css/values/primitives/CSSPrimitiveNumeric.h
    M Source/WebCore/css/values/primitives/CSSPrimitiveNumericConcepts.h
    A Source/WebCore/css/values/primitives/CSSPrimitiveNumericOrKeyword.h
    A Source/WebCore/css/values/primitives/CSSPrimitiveNumericTypes+CSSValueConversions.h
    M Source/WebCore/css/values/primitives/CSSPrimitiveNumericTypes+CSSValueCreation.h
    M Source/WebCore/css/values/primitives/CSSPrimitiveNumericTypes.h
    A Source/WebCore/css/values/scroll-snap/CSSScrollMargin.h
    A Source/WebCore/css/values/scroll-snap/CSSScrollPadding.h
    M Source/WebCore/page/scrolling/ScrollSnapOffsetsInfo.cpp
    M Source/WebCore/rendering/RenderBox.cpp
    M Source/WebCore/rendering/RenderElement.cpp
    M Source/WebCore/rendering/TextPainter.cpp
    M Source/WebCore/rendering/style/RenderStyle.cpp
    M Source/WebCore/rendering/style/RenderStyle.h
    M Source/WebCore/rendering/style/RenderStyleInlines.h
    M Source/WebCore/rendering/style/StyleRareNonInheritedData.h
    M Source/WebCore/style/StyleBuilderConverter.h
    M Source/WebCore/style/StyleBuilderCustom.h
    M Source/WebCore/style/values/StyleValueTypes.h
    A Source/WebCore/style/values/primitives/StylePrimitiveNumeric.h
    A Source/WebCore/style/values/primitives/StylePrimitiveNumericOrKeyword+Conversions.h
    A Source/WebCore/style/values/primitives/StylePrimitiveNumericOrKeyword.h
    M Source/WebCore/style/values/primitives/StylePrimitiveNumericTypes+Conversions.h
    M Source/WebCore/style/values/primitives/StylePrimitiveNumericTypes+Evaluation.h
    M Source/WebCore/style/values/primitives/StylePrimitiveNumericTypes.h
    A Source/WebCore/style/values/scroll-snap/StyleScrollMargin.cpp
    A Source/WebCore/style/values/scroll-snap/StyleScrollMargin.h
    A Source/WebCore/style/values/scroll-snap/StyleScrollPadding.cpp
    A Source/WebCore/style/values/scroll-snap/StyleScrollPadding.h
    M Source/WebCore/style/values/shapes/StyleShapeFunction.cpp

  Log Message:
  -----------
  Convert scroll-padding/scroll-margin types to strongly typed CSS/Style values
https://bugs.webkit.org/show_bug.cgi?id=285453

Reviewed by Antti Koivisto.

To allow for scroll-padding/scroll-margin conversion to strongly typed CSS/Style
values, a few new types/concepts need to be introduced to efficiently store
things like scroll-padding's edge values defined by the following grammar:

  <'scroll-padding-*'> = auto | <length-percentage [0,∞]>

Conceptually this maps to:

  std::variant<CSS::Keyword::Auto, CSS::LengthPercentage<CSS::Nonnegative>>

However, the use of std::variant causes unnecessary memory use to be introduced due
to the existing unused bits in CSS::LengthPercentage. We can do better by realizing
that CSS::LengthPercentage conceptually maps to:

  std::variant<CSS::LengthPercentageRaw<CSS::Nonnegative>, CSSCalc*>

So, if we flatten the nested std::variants together, we can uses a single index.

In fact, CSS::LengthPercentage is currently implement as hand rolled tagged union,
and we can re-use that same exact code to also support a list of CSS::Keyword values
as additions to the index.

We do that by extracting the core logic of CSS::LengthPercentage in CSS::PrimitiveNumeric
into a new CSS::PrimitiveData type that can be shared with a new class:

  CSS::PrimitiveNumericOrKeywords<CSS::Numeric, Keywords...>

This new class is used for scroll-padding's edge values.

----
* As an aside, CSS::PrimitiveData should eventually have its implementations replaced
by generic variant-like type that allows:

  a) the index and payload to be stored separately.
  b) a single payload to be assigned to multiple indices

These are needed to avoid bloating the size of the type due to raw values having both
a unit and value. We could try an alternative approach where we just use a lot of types
to represent raw values,
   e.g.
      using AngleRaw = std::variant<
          LiteralValue<AngleUnit::Deg>,
          LiteralValue<AngleUnit::Rad>,
          LiteralValue<AngleUnit::Grad>,
          LiteralValue<AngleUnit::Turn>
      >;
The concern with this approach, which might be unfounded as I have not measured, is
performance issues due to additional branching, especially in the case of the <length>
which has ~50 unit types. It may be that the compiler is smart enough (or we are clever
enough to trick it) to collapse those down.
----

On the Style side we have the same issue, but a different solution. The type name is mapped
over as Style::PrimitiveNumericOrKeywords<Style::Numeric, Keywords...> but we can utilize
the existing generic CompactVariant for this. To make this automatic, a new type adaptor
called WTF::FlatteningVariantAdaptor is used which, as the name suggests, flattens the
types, including any variant-likes, into a single variant-like.

In the case of Style::PrimitiveNumericOrKeywords, that means a type like:

  Style::PrimitiveNumericOrKeywords<LengthPercentage<>, Keyword::Auto>;

which is essentially the same as:

  std::variant<std::variant<Length<>, Percentage<>, Calc*>, Keyword::Auto>

collapses to:

  std::variant<Length<>, Percentage<>, Calc*, Keyword::Auto>

and because we use a CompactVariant rather than a std::variant, it only
takes up 8 bytes (just like WebCore::Length).

These new types are used to implement the scroll-padding and scroll-margin
properties with strongly typed values, shrinking StyleRareNonInheritedData
by 16 bytes in the process (scroll-margin goes from a RectEdges<WebCore::Length>
(32 bytes) to a RectEdges<Style::Length<>> (16 bytes)!)

* Source/WTF/WTF.xcodeproj/project.pbxproj:
* Source/WTF/wtf/CMakeLists.txt:
* Source/WTF/wtf/FlatteningVariantAdaptor.h: Added.
    - Add FlatteningVariantAdaptor.h
* Source/WTF/wtf/Forward.h:
* Source/WTF/wtf/StdLibExtras.h:
* Source/WTF/wtf/VariantExtras.h:
    - Add new helpers to index into type lists like std::tuple, refactoring
      switchOn code to allow it to be shared.
* Source/WebCore/CMakeLists.txt:
* Source/WebCore/Headers.cmake:
* Source/WebCore/SaferCPPExpectations/RefCntblBaseVirtualDtorExpectations:
* Source/WebCore/Sources.txt:
* Source/WebCore/WebCore.xcodeproj/project.pbxproj:
    - Add new files.
* Source/WebCore/css/CSSColorSchemeValue.h:
    - Switch to generic CSSValueConversion customization point to make
      computed style extraction more straightforward.
* Source/WebCore/css/CSSProperties.json:
    - Temporarily remove generation of scroll-margin-*/scroll-padding-* parsing
      to allow use conversion to strong values. The generated will be learning to
      generate these shortly.
* Source/WebCore/css/CSSScrollMarginEdgeValue.cpp: Added.
* Source/WebCore/css/CSSScrollMarginEdgeValue.h: Added.
* Source/WebCore/css/CSSScrollPaddingEdgeValue.cpp: Added.
* Source/WebCore/css/CSSScrollPaddingEdgeValue.h: Added.
* Source/WebCore/css/CSSValue.cpp:
* Source/WebCore/css/CSSValue.h:
    - Add CSSValue subclasses for scroll-margin/scroll-padding edge values.
* Source/WebCore/css/ComputedStyleExtractor.cpp:
    - Switch scroll-margin-*/scroll-padding-*/color-scheme to generic CSSValue
      creation code path.
* Source/WebCore/css/calc/CSSCalcTree+Simplification.cpp:
* Source/WebCore/css/calc/CSSCalcTree+Simplification.h:
    - Add conservative check if a calc tree can be simplified for use when
      the typed CSSOM inserts unsimplified calc.
* Source/WebCore/css/calc/CSSCalcValue.cpp:
* Source/WebCore/css/calc/CSSCalcValue.h:
    - Utilize new canSimplify() to avoid a copy in the common case where the
      the typed CSSOM is not being used. Also adds a "no conversion data"
      overload which is now needed.
* Source/WebCore/css/parser/CSSPropertyParserConsumer+ScrollSnap.cpp:
* Source/WebCore/css/parser/CSSPropertyParserConsumer+ScrollSnap.h:
    - Add temporary parsing for scroll-margin-*/scroll-padding-* needed
      only to create the right CSSValue subclasses. The generator will
      learn how do to this shortly.
* Source/WebCore/css/typedom/CSSStyleValueFactory.cpp:
    - Add special cases for reification to maintain existing behavior.
* Source/WebCore/css/values/CSSValueAggregates.h:
    - Add new macros to more easily define full featured wrapper and extenders.
    - Remove isZero()/isEmpty() members. These have been replaced by proper
      customization points Style::isZero/Style::isEmpty() which can work with
      any type and are automatically usable for tuple and variant types.
* Source/WebCore/css/values/CSSValueConcepts.h:
    - Move HasIsZero/HasIsEmpty concepts here.
* Source/WebCore/css/values/primitives/CSSPrimitiveData.h: Added.
* Source/WebCore/css/values/primitives/CSSPrimitiveKeywordList.h: Added.
* Source/WebCore/css/values/primitives/CSSPrimitiveNumeric.h: Added.
* Source/WebCore/css/values/primitives/CSSPrimitiveNumericConcepts.h:
* Source/WebCore/css/values/primitives/CSSPrimitiveNumericOrKeyword.h: Added.
* Source/WebCore/css/values/primitives/CSSPrimitiveNumericTypes.h:
    - Refactor numeric types code and add new CSS::PrimitiveNumericOrKeyword
      built on the new core.
* Source/WebCore/css/values/primitives/CSSPrimitiveNumericTypes+CSSValueConversions.h: Added.
    - Add customization point to allow generic conversion from CSSValue to
      strongly typed CSS.
* Source/WebCore/css/values/primitives/CSSPrimitiveNumericTypes+CSSValueCreation.h:
    - Extend generic conversion to see through single element tuple-like types
      and variant-like types.
* Source/WebCore/css/values/scroll-snap/CSSScrollMargin.h: Added.
* Source/WebCore/css/values/scroll-snap/CSSScrollPadding.h: Added.
    - Add new types for scroll-margin/scroll-padding css values.
* Source/WebCore/page/scrolling/ScrollSnapOffsetsInfo.cpp:
* Source/WebCore/rendering/RenderBox.cpp:
* Source/WebCore/rendering/RenderElement.cpp:
* Source/WebCore/rendering/TextPainter.cpp:
    - Utilize new strong types and factor repeated extent logic out so it can be shared.
* Source/WebCore/rendering/style/RenderStyle.cpp:
* Source/WebCore/rendering/style/RenderStyle.h:
* Source/WebCore/rendering/style/RenderStyleInlines.h:
* Source/WebCore/rendering/style/StyleRareNonInheritedData.h:
* Source/WebCore/style/StyleBuilderConverter.h:
* Source/WebCore/style/StyleBuilderCustom.h:
    - Update for new scroll-margin/scroll-padding types.
* Source/WebCore/style/values/StyleValueTypes.h:
    - Add IsZero/IsEmpty customization point and default implementations.
* Source/WebCore/style/values/primitives/StylePrimitiveNumeric.h: Copied from Source/WebCore/style/values/primitives/StylePrimitiveNumericTypes.h.
* Source/WebCore/style/values/primitives/StylePrimitiveNumericOrKeyword+Conversions.h: Added.
* Source/WebCore/style/values/primitives/StylePrimitiveNumericOrKeyword.h: Added.
* Source/WebCore/style/values/primitives/StylePrimitiveNumericTypes+Conversions.h:
* Source/WebCore/style/values/primitives/StylePrimitiveNumericTypes+Evaluation.h:
* Source/WebCore/style/values/primitives/StylePrimitiveNumericTypes.h:
    - Refactor numeric types code and add new CSS::PrimitiveNumericOrKeyword
      built on the new core.
* Source/WebCore/style/values/scroll-snap/StyleScrollMargin.cpp: Added.
* Source/WebCore/style/values/scroll-snap/StyleScrollMargin.h: Added.
* Source/WebCore/style/values/scroll-snap/StyleScrollPadding.cpp: Added.
* Source/WebCore/style/values/scroll-snap/StyleScrollPadding.h: Added.
    - Add new types for scroll-margin/scroll-padding style values.

Canonical link: https://commits.webkit.org/288829@main



To unsubscribe from these emails, change your notification settings at https://github.com/WebKit/WebKit/settings/notifications


More information about the webkit-changes mailing list