Skip to content

Commit 252edb5

Browse files
authoredDec 7, 2022
perf(css/parser): Reduce number of function calls (#6587)
1 parent 259eb87 commit 252edb5

File tree

11 files changed

+2078
-166
lines changed

11 files changed

+2078
-166
lines changed
 

‎crates/swc_css_parser/src/lexer/mod.rs

+77-109
Large diffs are not rendered by default.

‎crates/swc_css_parser/src/macros.rs

+24-40
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,12 @@ macro_rules! tok {
3535
swc_css_ast::Token::BadUrl { .. }
3636
};
3737

38-
("[") => {
39-
swc_css_ast::Token::LBracket
38+
("{") => {
39+
swc_css_ast::Token::LBrace
4040
};
4141

42-
("]") => {
43-
swc_css_ast::Token::RBracket
42+
("}") => {
43+
swc_css_ast::Token::RBrace
4444
};
4545

4646
("(") => {
@@ -51,8 +51,12 @@ macro_rules! tok {
5151
swc_css_ast::Token::RParen
5252
};
5353

54-
("%") => {
55-
swc_css_ast::Token::Delim { value: '%', .. }
54+
("[") => {
55+
swc_css_ast::Token::LBracket
56+
};
57+
58+
("]") => {
59+
swc_css_ast::Token::RBracket
5660
};
5761

5862
(",") => {
@@ -63,36 +67,16 @@ macro_rules! tok {
6367
swc_css_ast::Token::Semi
6468
};
6569

66-
("!") => {
67-
swc_css_ast::Token::Delim { value: '!', .. }
68-
};
69-
7070
("?") => {
71-
swc_css_ast::Token::Delim { value: '?', .. }
72-
};
73-
74-
("{") => {
75-
swc_css_ast::Token::LBrace
76-
};
77-
78-
("}") => {
79-
swc_css_ast::Token::RBrace
80-
};
81-
82-
("[") => {
83-
swc_css_ast::Token::LBracket
84-
};
85-
86-
("]") => {
87-
swc_css_ast::Token::RBracket
71+
swc_css_ast::Token::Delim { value: '?' }
8872
};
8973

9074
(":") => {
9175
swc_css_ast::Token::Colon
9276
};
9377

9478
("*") => {
95-
swc_css_ast::Token::Delim { value: '*', .. }
79+
swc_css_ast::Token::Delim { value: '*' }
9680
};
9781

9882
("@") => {
@@ -104,27 +88,27 @@ macro_rules! tok {
10488
};
10589

10690
("&") => {
107-
swc_css_ast::Token::Delim { value: '&', .. }
91+
swc_css_ast::Token::Delim { value: '&' }
10892
};
10993

11094
("|") => {
111-
swc_css_ast::Token::Delim { value: '|', .. }
95+
swc_css_ast::Token::Delim { value: '|' }
11296
};
11397

11498
("$") => {
115-
swc_css_ast::Token::Delim { value: '$', .. }
99+
swc_css_ast::Token::Delim { value: '$' }
116100
};
117101

118102
("^") => {
119-
swc_css_ast::Token::Delim { value: '^', .. }
103+
swc_css_ast::Token::Delim { value: '^' }
120104
};
121105

122106
("~") => {
123-
swc_css_ast::Token::Delim { value: '~', .. }
107+
swc_css_ast::Token::Delim { value: '~' }
124108
};
125109

126110
("=") => {
127-
swc_css_ast::Token::Delim { value: '=', .. }
111+
swc_css_ast::Token::Delim { value: '=' }
128112
};
129113

130114
(" ") => {
@@ -140,26 +124,26 @@ macro_rules! tok {
140124
};
141125

142126
("+") => {
143-
swc_css_ast::Token::Delim { value: '+', .. }
127+
swc_css_ast::Token::Delim { value: '+' }
144128
};
145129

146130
("-") => {
147-
swc_css_ast::Token::Delim { value: '-', .. }
131+
swc_css_ast::Token::Delim { value: '-' }
148132
};
149133

150134
(".") => {
151-
swc_css_ast::Token::Delim { value: '.', .. }
135+
swc_css_ast::Token::Delim { value: '.' }
152136
};
153137

154138
("/") => {
155-
swc_css_ast::Token::Delim { value: '/', .. }
139+
swc_css_ast::Token::Delim { value: '/' }
156140
};
157141

158142
("<") => {
159-
swc_css_ast::Token::Delim { value: '<', .. }
143+
swc_css_ast::Token::Delim { value: '<' }
160144
};
161145

162146
(">") => {
163-
swc_css_ast::Token::Delim { value: '>', .. }
147+
swc_css_ast::Token::Delim { value: '>' }
164148
};
165149
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
a { animation: test; }
2+
a { animation: тест; }
3+
a { animation: т\ест; }
4+
a { animation: 😋; }
5+
a { animation: \\😋; }
6+
a { animation: \😋; }

‎crates/swc_css_parser/tests/fixture/value/ident/output.json

+700
Large diffs are not rendered by default.

‎crates/swc_css_parser/tests/fixture/value/ident/span.swc-stderr

+838
Large diffs are not rendered by default.

‎crates/swc_css_parser/tests/fixture/value/url/input.css

+1
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,5 @@ https://example.com/image.png
3636

3737
);
3838
background: URL(https://example.com/ima\)ge.png);
39+
background: url( "https://example.com/image.png" );
3940
}

‎crates/swc_css_parser/tests/fixture/value/url/output.json

+53-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22
"type": "Stylesheet",
33
"span": {
44
"start": 1,
5-
"end": 1336,
5+
"end": 1396,
66
"ctxt": 0
77
},
88
"rules": [
99
{
1010
"type": "QualifiedRule",
1111
"span": {
1212
"start": 1,
13-
"end": 1335,
13+
"end": 1395,
1414
"ctxt": 0
1515
},
1616
"prelude": {
@@ -74,7 +74,7 @@
7474
"type": "SimpleBlock",
7575
"span": {
7676
"start": 5,
77-
"end": 1335,
77+
"end": 1395,
7878
"ctxt": 0
7979
},
8080
"name": {
@@ -1426,6 +1426,56 @@
14261426
}
14271427
],
14281428
"important": null
1429+
},
1430+
{
1431+
"type": "Declaration",
1432+
"span": {
1433+
"start": 1338,
1434+
"end": 1392,
1435+
"ctxt": 0
1436+
},
1437+
"name": {
1438+
"type": "Ident",
1439+
"span": {
1440+
"start": 1338,
1441+
"end": 1348,
1442+
"ctxt": 0
1443+
},
1444+
"value": "background",
1445+
"raw": "background"
1446+
},
1447+
"value": [
1448+
{
1449+
"type": "Url",
1450+
"span": {
1451+
"start": 1350,
1452+
"end": 1392,
1453+
"ctxt": 0
1454+
},
1455+
"name": {
1456+
"type": "Ident",
1457+
"span": {
1458+
"start": 1350,
1459+
"end": 1353,
1460+
"ctxt": 0
1461+
},
1462+
"value": "url",
1463+
"raw": "url"
1464+
},
1465+
"value": {
1466+
"type": "String",
1467+
"span": {
1468+
"start": 1357,
1469+
"end": 1388,
1470+
"ctxt": 0
1471+
},
1472+
"value": "https://example.com/image.png",
1473+
"raw": "\"https://example.com/image.png\""
1474+
},
1475+
"modifiers": []
1476+
}
1477+
],
1478+
"important": null
14291479
}
14301480
]
14311481
}

‎crates/swc_css_parser/tests/fixture/value/url/span.swc-stderr

+98-14
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
36 | |
4040
37 | | );
4141
38 | | background: URL(https://example.com/ima\)ge.png);
42-
39 | `-> }
42+
39 | | background: url( "https://example.com/image.png" );
43+
40 | `-> }
4344
`----
4445

4546
x Rule
@@ -82,7 +83,8 @@
8283
36 | |
8384
37 | | );
8485
38 | | background: URL(https://example.com/ima\)ge.png);
85-
39 | `-> }
86+
39 | | background: url( "https://example.com/image.png" );
87+
40 | `-> }
8688
`----
8789

8890
x QualifiedRule
@@ -125,7 +127,8 @@
125127
36 | |
126128
37 | | );
127129
38 | | background: URL(https://example.com/ima\)ge.png);
128-
39 | `-> }
130+
39 | | background: url( "https://example.com/image.png" );
131+
40 | `-> }
129132
`----
130133

131134
x SelectorList
@@ -217,7 +220,8 @@
217220
36 | |
218221
37 | | );
219222
38 | | background: URL(https://example.com/ima\)ge.png);
220-
39 | `-> }
223+
39 | | background: url( "https://example.com/image.png" );
224+
40 | `-> }
221225
`----
222226

223227
x LBrace
@@ -2248,77 +2252,157 @@
22482252
37 | );
22492253
38 | background: URL(https://example.com/ima\)ge.png);
22502254
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2251-
39 | }
2255+
39 | background: url( "https://example.com/image.png" );
22522256
`----
22532257

22542258
x StyleBlock
22552259
,-[$DIR/tests/fixture/value/url/input.css:37:1]
22562260
37 | );
22572261
38 | background: URL(https://example.com/ima\)ge.png);
22582262
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2259-
39 | }
2263+
39 | background: url( "https://example.com/image.png" );
22602264
`----
22612265

22622266
x Declaration
22632267
,-[$DIR/tests/fixture/value/url/input.css:37:1]
22642268
37 | );
22652269
38 | background: URL(https://example.com/ima\)ge.png);
22662270
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2267-
39 | }
2271+
39 | background: url( "https://example.com/image.png" );
22682272
`----
22692273

22702274
x DeclarationName
22712275
,-[$DIR/tests/fixture/value/url/input.css:37:1]
22722276
37 | );
22732277
38 | background: URL(https://example.com/ima\)ge.png);
22742278
: ^^^^^^^^^^
2275-
39 | }
2279+
39 | background: url( "https://example.com/image.png" );
22762280
`----
22772281

22782282
x Ident
22792283
,-[$DIR/tests/fixture/value/url/input.css:37:1]
22802284
37 | );
22812285
38 | background: URL(https://example.com/ima\)ge.png);
22822286
: ^^^^^^^^^^
2283-
39 | }
2287+
39 | background: url( "https://example.com/image.png" );
22842288
`----
22852289

22862290
x ComponentValue
22872291
,-[$DIR/tests/fixture/value/url/input.css:37:1]
22882292
37 | );
22892293
38 | background: URL(https://example.com/ima\)ge.png);
22902294
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2291-
39 | }
2295+
39 | background: url( "https://example.com/image.png" );
22922296
`----
22932297

22942298
x Url
22952299
,-[$DIR/tests/fixture/value/url/input.css:37:1]
22962300
37 | );
22972301
38 | background: URL(https://example.com/ima\)ge.png);
22982302
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2299-
39 | }
2303+
39 | background: url( "https://example.com/image.png" );
23002304
`----
23012305

23022306
x Ident
23032307
,-[$DIR/tests/fixture/value/url/input.css:37:1]
23042308
37 | );
23052309
38 | background: URL(https://example.com/ima\)ge.png);
23062310
: ^^^
2307-
39 | }
2311+
39 | background: url( "https://example.com/image.png" );
23082312
`----
23092313

23102314
x UrlValue
23112315
,-[$DIR/tests/fixture/value/url/input.css:37:1]
23122316
37 | );
23132317
38 | background: URL(https://example.com/ima\)ge.png);
23142318
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2315-
39 | }
2319+
39 | background: url( "https://example.com/image.png" );
23162320
`----
23172321

23182322
x UrlValueRaw
23192323
,-[$DIR/tests/fixture/value/url/input.css:37:1]
23202324
37 | );
23212325
38 | background: URL(https://example.com/ima\)ge.png);
23222326
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2323-
39 | }
2327+
39 | background: url( "https://example.com/image.png" );
2328+
`----
2329+
2330+
x ComponentValue
2331+
,-[$DIR/tests/fixture/value/url/input.css:38:1]
2332+
38 | background: URL(https://example.com/ima\)ge.png);
2333+
39 | background: url( "https://example.com/image.png" );
2334+
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2335+
40 | }
2336+
`----
2337+
2338+
x StyleBlock
2339+
,-[$DIR/tests/fixture/value/url/input.css:38:1]
2340+
38 | background: URL(https://example.com/ima\)ge.png);
2341+
39 | background: url( "https://example.com/image.png" );
2342+
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2343+
40 | }
2344+
`----
2345+
2346+
x Declaration
2347+
,-[$DIR/tests/fixture/value/url/input.css:38:1]
2348+
38 | background: URL(https://example.com/ima\)ge.png);
2349+
39 | background: url( "https://example.com/image.png" );
2350+
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2351+
40 | }
2352+
`----
2353+
2354+
x DeclarationName
2355+
,-[$DIR/tests/fixture/value/url/input.css:38:1]
2356+
38 | background: URL(https://example.com/ima\)ge.png);
2357+
39 | background: url( "https://example.com/image.png" );
2358+
: ^^^^^^^^^^
2359+
40 | }
2360+
`----
2361+
2362+
x Ident
2363+
,-[$DIR/tests/fixture/value/url/input.css:38:1]
2364+
38 | background: URL(https://example.com/ima\)ge.png);
2365+
39 | background: url( "https://example.com/image.png" );
2366+
: ^^^^^^^^^^
2367+
40 | }
2368+
`----
2369+
2370+
x ComponentValue
2371+
,-[$DIR/tests/fixture/value/url/input.css:38:1]
2372+
38 | background: URL(https://example.com/ima\)ge.png);
2373+
39 | background: url( "https://example.com/image.png" );
2374+
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2375+
40 | }
2376+
`----
2377+
2378+
x Url
2379+
,-[$DIR/tests/fixture/value/url/input.css:38:1]
2380+
38 | background: URL(https://example.com/ima\)ge.png);
2381+
39 | background: url( "https://example.com/image.png" );
2382+
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2383+
40 | }
2384+
`----
2385+
2386+
x Ident
2387+
,-[$DIR/tests/fixture/value/url/input.css:38:1]
2388+
38 | background: URL(https://example.com/ima\)ge.png);
2389+
39 | background: url( "https://example.com/image.png" );
2390+
: ^^^
2391+
40 | }
2392+
`----
2393+
2394+
x UrlValue
2395+
,-[$DIR/tests/fixture/value/url/input.css:38:1]
2396+
38 | background: URL(https://example.com/ima\)ge.png);
2397+
39 | background: url( "https://example.com/image.png" );
2398+
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2399+
40 | }
2400+
`----
2401+
2402+
x Str
2403+
,-[$DIR/tests/fixture/value/url/input.css:38:1]
2404+
38 | background: URL(https://example.com/ima\)ge.png);
2405+
39 | background: url( "https://example.com/image.png" );
2406+
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2407+
40 | }
23242408
`----
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// test
2+
a {
3+
color: red;
4+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
{
2+
"type": "Stylesheet",
3+
"span": {
4+
"start": 9,
5+
"end": 31,
6+
"ctxt": 0
7+
},
8+
"rules": [
9+
{
10+
"type": "QualifiedRule",
11+
"span": {
12+
"start": 9,
13+
"end": 30,
14+
"ctxt": 0
15+
},
16+
"prelude": {
17+
"type": "SelectorList",
18+
"span": {
19+
"start": 9,
20+
"end": 10,
21+
"ctxt": 0
22+
},
23+
"children": [
24+
{
25+
"type": "ComplexSelector",
26+
"span": {
27+
"start": 9,
28+
"end": 10,
29+
"ctxt": 0
30+
},
31+
"children": [
32+
{
33+
"type": "CompoundSelector",
34+
"span": {
35+
"start": 9,
36+
"end": 10,
37+
"ctxt": 0
38+
},
39+
"nestingSelector": null,
40+
"typeSelector": {
41+
"type": "TagNameSelector",
42+
"span": {
43+
"start": 9,
44+
"end": 10,
45+
"ctxt": 0
46+
},
47+
"name": {
48+
"type": "WqName",
49+
"span": {
50+
"start": 9,
51+
"end": 10,
52+
"ctxt": 0
53+
},
54+
"prefix": null,
55+
"value": {
56+
"type": "Ident",
57+
"span": {
58+
"start": 9,
59+
"end": 10,
60+
"ctxt": 0
61+
},
62+
"value": "a",
63+
"raw": "a"
64+
}
65+
}
66+
},
67+
"subclassSelectors": []
68+
}
69+
]
70+
}
71+
]
72+
},
73+
"block": {
74+
"type": "SimpleBlock",
75+
"span": {
76+
"start": 11,
77+
"end": 30,
78+
"ctxt": 0
79+
},
80+
"name": {
81+
"type": "PreservedToken",
82+
"span": {
83+
"start": 11,
84+
"end": 12,
85+
"ctxt": 0
86+
},
87+
"token": "LBrace"
88+
},
89+
"value": [
90+
{
91+
"type": "Declaration",
92+
"span": {
93+
"start": 17,
94+
"end": 27,
95+
"ctxt": 0
96+
},
97+
"name": {
98+
"type": "Ident",
99+
"span": {
100+
"start": 17,
101+
"end": 22,
102+
"ctxt": 0
103+
},
104+
"value": "color",
105+
"raw": "color"
106+
},
107+
"value": [
108+
{
109+
"type": "Ident",
110+
"span": {
111+
"start": 24,
112+
"end": 27,
113+
"ctxt": 0
114+
},
115+
"value": "red",
116+
"raw": "red"
117+
}
118+
],
119+
"important": null
120+
}
121+
]
122+
}
123+
}
124+
]
125+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
2+
x Stylesheet
3+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:1:1]
4+
1 | // test
5+
2 | ,-> a {
6+
3 | | color: red;
7+
4 | `-> }
8+
`----
9+
10+
x Rule
11+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:1:1]
12+
1 | // test
13+
2 | ,-> a {
14+
3 | | color: red;
15+
4 | `-> }
16+
`----
17+
18+
x QualifiedRule
19+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:1:1]
20+
1 | // test
21+
2 | ,-> a {
22+
3 | | color: red;
23+
4 | `-> }
24+
`----
25+
26+
x SelectorList
27+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:1:1]
28+
1 | // test
29+
2 | a {
30+
: ^
31+
3 | color: red;
32+
`----
33+
34+
x ComplexSelector
35+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:1:1]
36+
1 | // test
37+
2 | a {
38+
: ^
39+
3 | color: red;
40+
`----
41+
42+
x CompoundSelector
43+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:1:1]
44+
1 | // test
45+
2 | a {
46+
: ^
47+
3 | color: red;
48+
`----
49+
50+
x TypeSelector
51+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:1:1]
52+
1 | // test
53+
2 | a {
54+
: ^
55+
3 | color: red;
56+
`----
57+
58+
x TagNameSelector
59+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:1:1]
60+
1 | // test
61+
2 | a {
62+
: ^
63+
3 | color: red;
64+
`----
65+
66+
x WqName
67+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:1:1]
68+
1 | // test
69+
2 | a {
70+
: ^
71+
3 | color: red;
72+
`----
73+
74+
x Ident
75+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:1:1]
76+
1 | // test
77+
2 | a {
78+
: ^
79+
3 | color: red;
80+
`----
81+
82+
x SimpleBlock
83+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:1:1]
84+
1 | // test
85+
2 | ,-> a {
86+
3 | | color: red;
87+
4 | `-> }
88+
`----
89+
90+
x LBrace
91+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:1:1]
92+
1 | // test
93+
2 | a {
94+
: ^
95+
3 | color: red;
96+
`----
97+
98+
x ComponentValue
99+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:2:1]
100+
2 | a {
101+
3 | color: red;
102+
: ^^^^^^^^^^
103+
4 | }
104+
`----
105+
106+
x StyleBlock
107+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:2:1]
108+
2 | a {
109+
3 | color: red;
110+
: ^^^^^^^^^^
111+
4 | }
112+
`----
113+
114+
x Declaration
115+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:2:1]
116+
2 | a {
117+
3 | color: red;
118+
: ^^^^^^^^^^
119+
4 | }
120+
`----
121+
122+
x DeclarationName
123+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:2:1]
124+
2 | a {
125+
3 | color: red;
126+
: ^^^^^
127+
4 | }
128+
`----
129+
130+
x Ident
131+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:2:1]
132+
2 | a {
133+
3 | color: red;
134+
: ^^^^^
135+
4 | }
136+
`----
137+
138+
x ComponentValue
139+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:2:1]
140+
2 | a {
141+
3 | color: red;
142+
: ^^^
143+
4 | }
144+
`----
145+
146+
x Ident
147+
,-[$DIR/tests/line-comment/css-in-js/11/input.css:2:1]
148+
2 | a {
149+
3 | color: red;
150+
: ^^^
151+
4 | }
152+
`----

0 commit comments

Comments
 (0)
Please sign in to comment.