Skip to content

Commit 89a3911

Browse files
tcl3AtkinsSJ
authored andcommitted
LibWeb: Avoid intermediate string allocations in typed om serialization
1 parent f84e6f8 commit 89a3911

27 files changed

+102
-94
lines changed

Libraries/LibWeb/CSS/CSSKeywordValue.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
#include "CSSKeywordValue.h"
8+
#include <AK/StringBuilder.h>
89
#include <LibWeb/Bindings/CSSKeywordValuePrototype.h>
910
#include <LibWeb/Bindings/Intrinsics.h>
1011
#include <LibWeb/CSS/Keyword.h>
@@ -59,12 +60,19 @@ WebIDL::ExceptionOr<void> CSSKeywordValue::set_value(FlyString value)
5960
}
6061

6162
// https://drafts.css-houdini.org/css-typed-om-1/#keywordvalue-serialization
62-
WebIDL::ExceptionOr<String> CSSKeywordValue::to_string() const
63+
void CSSKeywordValue::serialize(StringBuilder& builder) const
6364
{
6465
// To serialize a CSSKeywordValue this:
6566
// 1. Return this’s value internal slot.
6667
// AD-HOC: Serialize it as an identifier. Spec issue: https://github.com/w3c/csswg-drafts/issues/12545
67-
return serialize_an_identifier(m_value);
68+
serialize_an_identifier(builder, m_value);
69+
}
70+
71+
WebIDL::ExceptionOr<String> CSSKeywordValue::to_string() const
72+
{
73+
StringBuilder builder;
74+
serialize(builder);
75+
return builder.to_string_without_validation();
6876
}
6977

7078
// https://drafts.css-houdini.org/css-typed-om-1/#create-an-internal-representation

Libraries/LibWeb/CSS/CSSKeywordValue.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class CSSKeywordValue final : public CSSStyleValue {
2828
FlyString const& value() const { return m_value; }
2929
WebIDL::ExceptionOr<void> set_value(FlyString value);
3030

31+
void serialize(StringBuilder&) const;
3132
virtual WebIDL::ExceptionOr<String> to_string() const override;
3233
virtual WebIDL::ExceptionOr<NonnullRefPtr<StyleValue const>> create_an_internal_representation(PropertyNameAndID const&, PerformTypeCheck) const override;
3334

Libraries/LibWeb/CSS/CSSMathClamp.cpp

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,16 @@ void CSSMathClamp::visit_edges(Visitor& visitor)
6868
}
6969

7070
// https://drafts.css-houdini.org/css-typed-om-1/#serialize-a-cssmathvalue
71-
String CSSMathClamp::serialize_math_value(Nested, Parens) const
71+
void CSSMathClamp::serialize_math_value(StringBuilder& s, Nested, Parens) const
7272
{
7373
// AD-HOC: The spec is missing serialization rules for CSSMathClamp: https://github.com/w3c/css-houdini-drafts/issues/1152
74-
StringBuilder s;
7574
s.append("clamp("sv);
76-
s.append(m_lower->to_string({ .nested = true, .parenless = true }));
75+
m_lower->serialize(s, { .nested = true, .parenless = true });
7776
s.append(", "sv);
78-
s.append(m_value->to_string({ .nested = true, .parenless = true }));
77+
m_value->serialize(s, { .nested = true, .parenless = true });
7978
s.append(", "sv);
80-
s.append(m_upper->to_string({ .nested = true, .parenless = true }));
81-
s.append(")"sv);
82-
return s.to_string_without_validation();
79+
m_upper->serialize(s, { .nested = true, .parenless = true });
80+
s.append(')');
8381
}
8482

8583
// https://drafts.css-houdini.org/css-typed-om-1/#dom-cssmathclamp-lower

Libraries/LibWeb/CSS/CSSMathClamp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class CSSMathClamp final : public CSSMathValue {
2828
GC::Ref<CSSNumericValue> value() const;
2929
GC::Ref<CSSNumericValue> upper() const;
3030

31-
virtual String serialize_math_value(Nested, Parens) const override;
31+
virtual void serialize_math_value(StringBuilder&, Nested, Parens) const override;
3232
virtual bool is_equal_numeric_value(GC::Ref<CSSNumericValue> other) const override;
3333
virtual Optional<SumValue> create_a_sum_value() const override;
3434

Libraries/LibWeb/CSS/CSSMathInvert.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,18 @@ void CSSMathInvert::visit_edges(Visitor& visitor)
5454
}
5555

5656
// https://drafts.css-houdini.org/css-typed-om-1/#serialize-a-cssmathvalue
57-
String CSSMathInvert::serialize_math_value(Nested nested, Parens parens) const
57+
void CSSMathInvert::serialize_math_value(StringBuilder& s, Nested nested, Parens parens) const
5858
{
5959
// NB: Only steps 1 and 6 apply here.
6060
// 1. Let s initially be the empty string.
61-
StringBuilder s;
6261

6362
// 6. Otherwise, if this is a CSSMathInvert:
6463
{
6564
// 1. If paren-less is true, continue to the next step; otherwise, if nested is true, append "(" to s;
6665
// otherwise, append "calc(" to s.
6766
if (parens == Parens::With) {
6867
if (nested == Nested::Yes) {
69-
s.append("("sv);
68+
s.append('(');
7069
} else {
7170
s.append("calc("sv);
7271
}
@@ -76,14 +75,13 @@ String CSSMathInvert::serialize_math_value(Nested nested, Parens parens) const
7675
s.append("1 / "sv);
7776

7877
// 3. Serialize this’s value internal slot with nested set to true, and append the result to s.
79-
s.append(m_value->to_string({ .nested = true }));
78+
m_value->serialize(s, { .nested = true });
8079

8180
// 4. If paren-less is false, append ")" to s,
8281
if (parens == Parens::With)
83-
s.append(")"sv);
82+
s.append(')');
8483

8584
// 5. Return s.
86-
return s.to_string_without_validation();
8785
}
8886
}
8987

Libraries/LibWeb/CSS/CSSMathInvert.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CSSMathInvert final : public CSSMathValue {
2626

2727
GC::Ref<CSSNumericValue> value() const;
2828

29-
virtual String serialize_math_value(Nested, Parens) const override;
29+
virtual void serialize_math_value(StringBuilder&, Nested, Parens) const override;
3030
virtual bool is_equal_numeric_value(GC::Ref<CSSNumericValue> other) const override;
3131
virtual Optional<SumValue> create_a_sum_value() const override;
3232

Libraries/LibWeb/CSS/CSSMathMax.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,10 @@ void CSSMathMax::visit_edges(Visitor& visitor)
8181
}
8282

8383
// https://drafts.css-houdini.org/css-typed-om-1/#serialize-a-cssmathvalue
84-
String CSSMathMax::serialize_math_value(Nested, Parens) const
84+
void CSSMathMax::serialize_math_value(StringBuilder& s, Nested, Parens) const
8585
{
8686
// NB: Only steps 1 and 2 apply here.
8787
// 1. Let s initially be the empty string.
88-
StringBuilder s;
8988

9089
// 2. If this is a CSSMathMin or CSSMathMax:
9190
{
@@ -101,12 +100,11 @@ String CSSMathMax::serialize_math_value(Nested, Parens) const
101100
} else {
102101
s.append(", "sv);
103102
}
104-
s.append(arg->to_string({ .nested = true, .parenless = true }));
103+
arg->serialize(s, { .nested = true, .parenless = true });
105104
}
106105

107106
// 3. Append ")" to s and return s.
108-
s.append(")"sv);
109-
return s.to_string_without_validation();
107+
s.append(')');
110108
}
111109
}
112110

Libraries/LibWeb/CSS/CSSMathMax.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CSSMathMax final : public CSSMathValue {
2626

2727
GC::Ref<CSSNumericArray> values() const;
2828

29-
virtual String serialize_math_value(Nested, Parens) const override;
29+
virtual void serialize_math_value(StringBuilder&, Nested, Parens) const override;
3030
virtual bool is_equal_numeric_value(GC::Ref<CSSNumericValue> other) const override;
3131
virtual Optional<SumValue> create_a_sum_value() const override;
3232

Libraries/LibWeb/CSS/CSSMathMin.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,10 @@ void CSSMathMin::visit_edges(Visitor& visitor)
8282
}
8383

8484
// https://drafts.css-houdini.org/css-typed-om-1/#serialize-a-cssmathvalue
85-
String CSSMathMin::serialize_math_value(Nested, Parens) const
85+
void CSSMathMin::serialize_math_value(StringBuilder& s, Nested, Parens) const
8686
{
8787
// NB: Only steps 1 and 2 apply here.
8888
// 1. Let s initially be the empty string.
89-
StringBuilder s;
9089

9190
// 2. If this is a CSSMathMin or CSSMathMax:
9291
{
@@ -102,12 +101,11 @@ String CSSMathMin::serialize_math_value(Nested, Parens) const
102101
} else {
103102
s.append(", "sv);
104103
}
105-
s.append(arg->to_string({ .nested = true, .parenless = true }));
104+
arg->serialize(s, { .nested = true, .parenless = true });
106105
}
107106

108107
// 3. Append ")" to s and return s.
109-
s.append(")"sv);
110-
return s.to_string_without_validation();
108+
s.append(')');
111109
}
112110
}
113111

Libraries/LibWeb/CSS/CSSMathMin.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class CSSMathMin final : public CSSMathValue {
2626

2727
GC::Ref<CSSNumericArray> values() const;
2828

29-
virtual String serialize_math_value(Nested, Parens) const override;
29+
virtual void serialize_math_value(StringBuilder&, Nested, Parens) const override;
3030
virtual bool is_equal_numeric_value(GC::Ref<CSSNumericValue> other) const override;
3131
virtual Optional<SumValue> create_a_sum_value() const override;
3232

0 commit comments

Comments
 (0)