Skip to content

Commit 4ec4020

Browse files
committed
Reduce unecessary work (faster!)
1 parent f25a9f6 commit 4ec4020

18 files changed

+190
-37
lines changed

README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ cssnano: 542.956ms
6868
esbuild: 17.411ms
6969
160332 bytes
7070
71-
parcel-css: 4.961ms
72-
143662 bytes
71+
parcel-css: 4.74ms
72+
143985 bytes
7373
7474
7575
$ node bench.js animate.css
@@ -79,8 +79,8 @@ cssnano: 283.105ms
7979
esbuild: 11.858ms
8080
72183 bytes
8181
82-
parcel-css: 2.039ms
83-
23707 bytes
82+
parcel-css: 1.989ms
83+
23666 bytes
8484
8585
8686
$ node bench.js tailwind.css
@@ -90,6 +90,6 @@ cssnano: 2.198s
9090
esbuild: 107.668ms
9191
1961642 bytes
9292
93-
parcel-css: 42.568ms
94-
1905068 bytes
93+
parcel-css: 45.701ms
94+
1799209 bytes
9595
```

src/macros.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,18 +177,23 @@ macro_rules! shorthand_handler {
177177
$(
178178
pub $key: Option<$type>,
179179
)*
180+
has_any: bool
180181
}
181182

182183
impl PropertyHandler for $name {
183184
fn handle_property(&mut self, property: &Property, dest: &mut DeclarationList) -> bool {
184185
match property {
185186
$(
186-
Property::$prop(val) => self.$key = Some(val.clone()),
187+
Property::$prop(val) => {
188+
self.$key = Some(val.clone());
189+
self.has_any = true;
190+
},
187191
)+
188192
Property::$shorthand(val) => {
189193
$(
190194
self.$key = Some(val.$key.clone());
191195
)+
196+
self.has_any = true;
192197
}
193198
Property::Unparsed(val) if matches!(val.property_id, $( PropertyId::$prop | )+ PropertyId::$shorthand) => {
194199
self.finalize(dest);
@@ -201,6 +206,12 @@ macro_rules! shorthand_handler {
201206
}
202207

203208
fn finalize(&mut self, dest: &mut DeclarationList) {
209+
if !self.has_any {
210+
return
211+
}
212+
213+
self.has_any = false;
214+
204215
$(
205216
let $key = std::mem::take(&mut self.$key);
206217
)+

src/properties/align.rs

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,8 @@ pub(crate) struct AlignHandler {
730730
flex_align: Option<(FlexAlign, VendorPrefix)>,
731731
justify_items: Option<JustifyItems>,
732732
row_gap: Option<GapValue>,
733-
column_gap: Option<GapValue>
733+
column_gap: Option<GapValue>,
734+
has_any: bool
734735
}
735736

736737
impl AlignHandler {
@@ -761,7 +762,8 @@ impl PropertyHandler for AlignHandler {
761762
*val = $val.clone();
762763
*prefixes |= *$vp;
763764
} else {
764-
self.$prop = Some(($val.clone(), *$vp))
765+
self.$prop = Some(($val.clone(), *$vp));
766+
self.has_any = true;
765767
}
766768
}};
767769
}
@@ -791,7 +793,10 @@ impl PropertyHandler for AlignHandler {
791793
property!(align_self, val, vp);
792794
},
793795
FlexItemAlign(val, vp) => property!(flex_item_align, val, vp),
794-
JustifySelf(val) => self.justify_self = Some(val.clone()),
796+
JustifySelf(val) => {
797+
self.justify_self = Some(val.clone());
798+
self.has_any = true;
799+
},
795800
PlaceSelf(val) => {
796801
self.flex_item_align = None;
797802
property!(align_self, &val.align, &VendorPrefix::None);
@@ -804,18 +809,28 @@ impl PropertyHandler for AlignHandler {
804809
},
805810
BoxAlign(val, vp) => property!(box_align, val, vp),
806811
FlexAlign(val, vp) => property!(flex_align, val, vp),
807-
JustifyItems(val) => self.justify_items = Some(val.clone()),
812+
JustifyItems(val) => {
813+
self.justify_items = Some(val.clone());
814+
self.has_any = true;
815+
},
808816
PlaceItems(val) => {
809817
self.box_align = None;
810818
self.flex_align = None;
811819
property!(align_items, &val.align, &VendorPrefix::None);
812820
self.justify_items = Some(val.justify.clone());
813821
}
814-
RowGap(val) => self.row_gap = Some(val.clone()),
815-
ColumnGap(val) => self.column_gap = Some(val.clone()),
822+
RowGap(val) => {
823+
self.row_gap = Some(val.clone());
824+
self.has_any = true;
825+
},
826+
ColumnGap(val) => {
827+
self.column_gap = Some(val.clone());
828+
self.has_any = true;
829+
},
816830
Gap(val) => {
817831
self.row_gap = Some(val.row.clone());
818832
self.column_gap = Some(val.column.clone());
833+
self.has_any = true;
819834
}
820835
Unparsed(val) if is_align_property(&val.property_id) => {
821836
self.flush(dest);
@@ -834,6 +849,12 @@ impl PropertyHandler for AlignHandler {
834849

835850
impl AlignHandler {
836851
fn flush(&mut self, dest: &mut DeclarationList) {
852+
if !self.has_any {
853+
return
854+
}
855+
856+
self.has_any = false;
857+
837858
let mut align_content = std::mem::take(&mut self.align_content);
838859
let mut justify_content = std::mem::take(&mut self.justify_content);
839860
let mut align_self = std::mem::take(&mut self.align_self);

src/properties/animation.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,8 @@ pub(crate) struct AnimationHandler {
208208
directions: Option<(SmallVec<[AnimationDirection; 1]>, VendorPrefix)>,
209209
play_states: Option<(SmallVec<[AnimationPlayState; 1]>, VendorPrefix)>,
210210
delays: Option<(SmallVec<[Time; 1]>, VendorPrefix)>,
211-
fill_modes: Option<(SmallVec<[AnimationFillMode; 1]>, VendorPrefix)>
211+
fill_modes: Option<(SmallVec<[AnimationFillMode; 1]>, VendorPrefix)>,
212+
has_any: bool
212213
}
213214

214215
impl AnimationHandler {
@@ -245,7 +246,8 @@ impl PropertyHandler for AnimationHandler {
245246
*val = $val.clone();
246247
*prefixes |= *$vp;
247248
} else {
248-
self.$prop = Some(($val.clone(), *$vp))
249+
self.$prop = Some(($val.clone(), *$vp));
250+
self.has_any = true;
249251
}
250252
}};
251253
}
@@ -310,6 +312,12 @@ impl PropertyHandler for AnimationHandler {
310312

311313
impl AnimationHandler {
312314
fn flush(&mut self, dest: &mut DeclarationList) {
315+
if !self.has_any {
316+
return
317+
}
318+
319+
self.has_any = false;
320+
313321
let mut names = std::mem::take(&mut self.names);
314322
let mut durations = std::mem::take(&mut self.durations);
315323
let mut timing_functions = std::mem::take(&mut self.timing_functions);

src/properties/background.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,8 @@ pub(crate) struct BackgroundHandler {
374374
attachments: Option<SmallVec<[BackgroundAttachment; 1]>>,
375375
origins: Option<SmallVec<[BackgroundBox; 1]>>,
376376
clips: Option<SmallVec<[BackgroundClip; 1]>>,
377-
decls: Vec<Property>
377+
decls: Vec<Property>,
378+
has_any: bool
378379
}
379380

380381
impl BackgroundHandler {
@@ -445,6 +446,7 @@ impl PropertyHandler for BackgroundHandler {
445446
_ => return false
446447
}
447448

449+
self.has_any = true;
448450
true
449451
}
450452

@@ -462,6 +464,12 @@ impl PropertyHandler for BackgroundHandler {
462464

463465
impl BackgroundHandler {
464466
fn flush(&mut self, dest: &mut DeclarationList) {
467+
if !self.has_any {
468+
return
469+
}
470+
471+
self.has_any = false;
472+
465473
let color = std::mem::take(&mut self.color);
466474
let mut images = std::mem::take(&mut self.images);
467475
let mut x_positions = std::mem::take(&mut self.x_positions);

src/properties/border.rs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,8 @@ pub(crate) struct BorderHandler {
220220
border_inline_end: BorderShorthand,
221221
category: BorderCategory,
222222
border_image_handler: BorderImageHandler,
223-
border_radius_handler: BorderRadiusHandler
223+
border_radius_handler: BorderRadiusHandler,
224+
has_any: bool
224225
}
225226

226227
impl BorderHandler {
@@ -245,6 +246,7 @@ impl PropertyHandler for BorderHandler {
245246
}
246247
self.$key.$prop = Some($val.clone());
247248
self.category = $category;
249+
self.has_any = true;
248250
}};
249251
}
250252

@@ -255,6 +257,7 @@ impl PropertyHandler for BorderHandler {
255257
}
256258
self.$key.set_border($val);
257259
self.category = $category;
260+
self.has_any = true;
258261
}};
259262
}
260263

@@ -332,6 +335,7 @@ impl PropertyHandler for BorderHandler {
332335
self.border_block_end.width = None;
333336
self.border_inline_start.width = None;
334337
self.border_inline_end.width = None;
338+
self.has_any = true;
335339
}
336340
BorderStyle(val) => {
337341
self.border_top.style = Some(val.0.clone());
@@ -342,6 +346,7 @@ impl PropertyHandler for BorderHandler {
342346
self.border_block_end.style = None;
343347
self.border_inline_start.style = None;
344348
self.border_inline_end.style = None;
349+
self.has_any = true;
345350
}
346351
BorderColor(val) => {
347352
self.border_top.color = Some(val.0.clone());
@@ -352,6 +357,7 @@ impl PropertyHandler for BorderHandler {
352357
self.border_block_end.color = None;
353358
self.border_inline_start.color = None;
354359
self.border_inline_end.color = None;
360+
self.has_any = true;
355361
}
356362
Border(val) => {
357363
// dest.clear();
@@ -366,6 +372,7 @@ impl PropertyHandler for BorderHandler {
366372

367373
// Setting the `border` property resets `border-image`.
368374
self.border_image_handler.reset();
375+
self.has_any = true;
369376
}
370377
Unparsed(val) if is_border_property(&val.property_id) => {
371378
self.flush(dest);
@@ -388,6 +395,12 @@ impl PropertyHandler for BorderHandler {
388395

389396
impl BorderHandler {
390397
fn flush(&mut self, dest: &mut DeclarationList) {
398+
if !self.has_any {
399+
return
400+
}
401+
402+
self.has_any = false;
403+
391404
use Property::*;
392405

393406
macro_rules! flush_category {

src/properties/border_image.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,8 @@ pub(crate) struct BorderImageHandler {
249249
width: Option<Rect<BorderImageSideWidth>>,
250250
outset: Option<Rect<LengthOrNumber>>,
251251
repeat: Option<BorderImageRepeat>,
252-
vendor_prefix: VendorPrefix
252+
vendor_prefix: VendorPrefix,
253+
has_any: bool
253254
}
254255

255256
impl BorderImageHandler {
@@ -272,6 +273,7 @@ impl PropertyHandler for BorderImageHandler {
272273
}
273274
self.vendor_prefix = VendorPrefix::None;
274275
self.$name = Some($val.clone());
276+
self.has_any = true;
275277
}};
276278
}
277279

@@ -284,6 +286,7 @@ impl PropertyHandler for BorderImageHandler {
284286
BorderImage(val, vp) => {
285287
self.set_border_image(val);
286288
self.vendor_prefix |= *vp;
289+
self.has_any = true;
287290
},
288291
Unparsed(val) if is_border_image_property(&val.property_id) => {
289292
self.flush(dest);
@@ -327,6 +330,12 @@ impl BorderImageHandler {
327330
}
328331

329332
fn flush(&mut self, dest: &mut DeclarationList) {
333+
if !self.has_any {
334+
return
335+
}
336+
337+
self.has_any = false;
338+
330339
let source = std::mem::take(&mut self.source);
331340
let slice = std::mem::take(&mut self.slice);
332341
let width = std::mem::take(&mut self.width);

src/properties/border_radius.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ pub(crate) struct BorderRadiusHandler {
5757
top_right: Option<(Size2D<LengthPercentage>, VendorPrefix)>,
5858
bottom_left: Option<(Size2D<LengthPercentage>, VendorPrefix)>,
5959
bottom_right: Option<(Size2D<LengthPercentage>, VendorPrefix)>,
60-
logical: Vec<Property>
60+
logical: Vec<Property>,
61+
has_any: bool
6162
}
6263

6364
impl BorderRadiusHandler {
@@ -88,7 +89,8 @@ impl PropertyHandler for BorderRadiusHandler {
8889
*val = $val.clone();
8990
*prefixes |= *$vp;
9091
} else {
91-
self.$prop = Some(($val.clone(), *$vp))
92+
self.$prop = Some(($val.clone(), *$vp));
93+
self.has_any = true;
9294
}
9395
}};
9496
}
@@ -101,6 +103,7 @@ impl PropertyHandler for BorderRadiusHandler {
101103
BorderStartStartRadius(_) | BorderStartEndRadius(_) | BorderEndStartRadius(_) | BorderEndEndRadius(_) => {
102104
self.flush(dest);
103105
self.logical.push(property.clone());
106+
self.has_any = true;
104107
}
105108
BorderRadius(val, vp) => {
106109
self.logical.clear();
@@ -135,6 +138,12 @@ impl PropertyHandler for BorderRadiusHandler {
135138

136139
impl BorderRadiusHandler {
137140
fn flush(&mut self, dest: &mut DeclarationList) {
141+
if !self.has_any {
142+
return
143+
}
144+
145+
self.has_any = true;
146+
138147
let mut top_left = std::mem::take(&mut self.top_left);
139148
let mut top_right = std::mem::take(&mut self.top_right);
140149
let mut bottom_left = std::mem::take(&mut self.bottom_left);

src/properties/display.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,10 @@ impl PropertyHandler for DisplayHandler {
339339
}
340340

341341
fn finalize(&mut self, dest: &mut DeclarationList) {
342+
if self.display.is_none() {
343+
return
344+
}
345+
342346
dest.extend(&mut self.decls);
343347

344348
if let Some(display) = std::mem::take(&mut self.display) {

0 commit comments

Comments
 (0)