Skip to content

Commit 82a12fc

Browse files
committed
optimize: better length expr boxing for better memory usage
1 parent 13c3610 commit 82a12fc

6 files changed

Lines changed: 80 additions & 67 deletions

File tree

float-pigment-css/src/parser/property_value/calc.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -334,17 +334,11 @@ pub(crate) enum ExpectValueType {
334334
fn combine_calc_expr(operator: Operator, lhs: CalcExpr, rhs: CalcExpr) -> CalcExpr {
335335
// Unwrap nested `Length::Expr(Calc(...))` to flatten the expression tree.
336336
let final_lhs = match lhs {
337-
CalcExpr::Length(length_expr) => match *length_expr {
338-
Length::Expr(LengthExpr::Calc(calc_expr)) => calc_expr,
339-
other => Box::new(CalcExpr::Length(Box::new(other))),
340-
},
337+
CalcExpr::Length(length_expr) => length_expr.into_calc_expr(),
341338
other => Box::new(other),
342339
};
343340
let final_rhs = match rhs {
344-
CalcExpr::Length(length_expr) => match *length_expr {
345-
Length::Expr(LengthExpr::Calc(calc_expr)) => calc_expr,
346-
other => Box::new(CalcExpr::Length(Box::new(other))),
347-
},
341+
CalcExpr::Length(length_expr) => length_expr.into_calc_expr(),
348342
other => Box::new(other),
349343
};
350344
match operator {

float-pigment-css/src/parser/property_value/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -223,18 +223,18 @@ fn parse_length_inner<'a, 't: 'a, 'i: 't>(
223223
env_default_value(parser, properties, st)
224224
})
225225
})?;
226-
return Ok(Length::Expr(LengthExpr::Env(
226+
return Ok(Length::Expr(Box::new(LengthExpr::Env(
227227
name.into(),
228228
Box::new(default_value.unwrap_or(Length::Undefined)),
229-
)));
229+
))));
230230
}
231231
"calc" => {
232232
return parse_calc_inner(parser, properties, st, ExpectValueType::NumberAndLength)
233233
.map(|ret| {
234234
if let Some(r) = ComputeCalcExpr::<Length>::try_compute(&ret) {
235235
return r;
236236
}
237-
Length::Expr(LengthExpr::Calc(Box::new(ret)))
237+
Length::Expr(Box::new(LengthExpr::Calc(Box::new(ret))))
238238
});
239239
}
240240
_ => {}
@@ -401,7 +401,7 @@ pub(crate) fn percentage<'a, 't: 'a, 'i: 't>(
401401
if let Some(r) = ComputeCalcExpr::<Length>::try_compute(&ret) {
402402
return r;
403403
}
404-
Length::Expr(LengthExpr::Calc(Box::new(ret)))
404+
Length::Expr(Box::new(LengthExpr::Calc(Box::new(ret))))
405405
});
406406
}
407407
}

float-pigment-css/src/typing.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ pub enum Length {
150150
Rpx(f32),
151151
Em(f32),
152152
Ratio(f32),
153-
Expr(LengthExpr),
153+
Expr(Box<LengthExpr>),
154154
Vmin(f32),
155155
Vmax(f32),
156156
}
@@ -188,6 +188,25 @@ impl Length {
188188
}
189189
}
190190

191+
/// Create a new `Length` from a `CalcExpr`.
192+
pub fn new_calc_expr(calc_expr: Box<CalcExpr>) -> Self {
193+
Self::Expr(Box::new(LengthExpr::Calc(calc_expr)))
194+
}
195+
196+
/// Convert to `Box<CalcExpr>`.
197+
///
198+
/// If the length is already a `CalcExpr`, it will be returned directly.
199+
/// Otherwise, it will be wrapped into a new `CalcExpr`.
200+
pub fn into_calc_expr(self) -> Box<CalcExpr> {
201+
match self {
202+
Length::Expr(x) => match *x {
203+
LengthExpr::Calc(x) => x,
204+
x => Box::new(CalcExpr::Length(Box::new(Length::Expr(Box::new(x))))),
205+
},
206+
x => Box::new(CalcExpr::Length(Box::new(x))),
207+
}
208+
}
209+
191210
/// Resolve the length value to `f32`.
192211
///
193212
/// The `relative_length` is used to calculate `...%` length.
@@ -214,7 +233,7 @@ impl Length {
214233
}
215234
}
216235
Length::Ratio(x) => relative_length * *x,
217-
Length::Expr(x) => match x {
236+
Length::Expr(x) => match &**x {
218237
LengthExpr::Invalid => None?,
219238
LengthExpr::Env(name, default_value) => match name.as_str() {
220239
"safe-area-inset-left" => media_query_status.env.safe_area_inset_left.to_f32(),
@@ -268,7 +287,7 @@ impl Length {
268287
) -> Option<L> {
269288
let r = match self {
270289
Length::Undefined | Length::Auto => None?,
271-
Length::Expr(x) => match x {
290+
Length::Expr(x) => match &**x {
272291
LengthExpr::Invalid => None?,
273292
LengthExpr::Env(name, default_value) => match name.as_str() {
274293
"safe-area-inset-left" => media_query_status.env.safe_area_inset_left,

float-pigment-css/src/typing_stringify.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ impl fmt::Display for Length {
273273
&tmp
274274
}
275275
Length::Expr(expr) => {
276-
match expr {
276+
match &**expr {
277277
LengthExpr::Calc(calc_expr) => {
278278
tmp = calc_expr.to_string();
279279
&tmp

0 commit comments

Comments
 (0)