Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 48ea5e3

Browse filesBrowse files
lghuahuazyyv
andauthored
perf(core): cache rule index to reduce query index (#4892)
Co-authored-by: Chris <hizyyv@gmail.com>
1 parent 79c71f6 commit 48ea5e3
Copy full SHA for 48ea5e3

File tree

Expand file treeCollapse file tree

5 files changed

+31
-13
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

5 files changed

+31
-13
lines changed
Open diff view settings
Collapse file

‎packages-engine/core/src/config.ts‎

Copy file name to clipboardExpand all lines: packages-engine/core/src/config.ts
+13-10Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ContentOptions, FilterPattern, Preset, PresetFactory, PresetFactoryAwaitable, PresetOrFactoryAwaitable, ResolvedConfig, Rule, Shortcut, ToArray, UserConfig, UserConfigDefaults, UserShortcuts } from './types'
1+
import type { ContentOptions, DynamicRule, FilterPattern, Preset, PresetFactory, PresetFactoryAwaitable, PresetOrFactoryAwaitable, ResolvedConfig, Rule, Shortcut, ToArray, UserConfig, UserConfigDefaults, UserShortcuts } from './types'
22
import { DEFAULT_LAYERS } from './constants'
33
import { extractorSplit } from './extractors'
44
import { clone, isStaticRule, mergeDeep, normalizeVariant, toArray, uniq, uniqueBy } from './utils'
@@ -155,22 +155,25 @@ export async function resolveConfig<Theme extends object = object>(
155155
extractors.sort((a, b) => (a.order || 0) - (b.order || 0))
156156

157157
const rules = getMerged('rules')
158+
const rulesSize = rules.length
158159
const rulesStaticMap: ResolvedConfig<Theme>['rulesStaticMap'] = {}
160+
const rulesDynamic: ResolvedConfig<Theme>['rulesDynamic'] = []
159161

160-
const rulesSize = rules.length
162+
for (const [index, rule] of rules.entries()) {
163+
const meta = rule[2] ?? (rule[2] = {})
164+
meta.__index = index
161165

162-
const rulesDynamic = rules
163-
.filter((rule) => {
164-
if (!isStaticRule(rule))
165-
return true
166+
if (isStaticRule(rule)) {
166167
// Put static rules into the map for faster lookup
167-
const prefixes = toArray(rule[2]?.prefix || '')
168+
const prefixes = toArray(meta.prefix ?? '')
168169
prefixes.forEach((prefix) => {
169170
rulesStaticMap[prefix + rule[0]] = rule
170171
})
171-
return false
172-
})
173-
.reverse() as ResolvedConfig<Theme>['rulesDynamic']
172+
}
173+
else {
174+
(rulesDynamic as DynamicRule<Theme>[]).unshift(rule)
175+
}
176+
}
174177

175178
const autocomplete = {
176179
templates: uniq(sources.flatMap(p => toArray(p.autocomplete?.templates))),
Collapse file

‎packages-engine/core/src/generator.ts‎

Copy file name to clipboardExpand all lines: packages-engine/core/src/generator.ts
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,12 +672,11 @@ class UnoGeneratorInternal<Theme extends object = object> {
672672
context.rules!.push(rule)
673673
}
674674
context.generator.activatedRules.add(rule)
675-
const index = context.generator.config.rules.indexOf(rule)
676675
const meta = rule[2]
677676

678677
return entries.map((css): ParsedUtil | RawUtil => {
679678
if (isString(css))
680-
return [index, css, meta]
679+
return [meta!.__index!, css, meta]
681680

682681
// Extract variants from special symbols
683682
let variants = context.variantHandlers
@@ -729,7 +728,7 @@ class UnoGeneratorInternal<Theme extends object = object> {
729728
}
730729
}
731730

732-
return [index, raw, css as CSSEntries, entryMeta, variants]
731+
return [meta!.__index!, raw, css as CSSEntries, entryMeta, variants]
733732
})
734733
}
735734
}
Collapse file

‎packages-engine/core/src/types.ts‎

Copy file name to clipboardExpand all lines: packages-engine/core/src/types.ts
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,13 @@ export interface RuleMeta {
228228
*/
229229
__hash?: string
230230

231+
/**
232+
* Internal index of the rulelist
233+
* @internal
234+
* @private
235+
*/
236+
__index?: number
237+
231238
/**
232239
* Custom metadata
233240
*/
Collapse file

‎packages-engine/core/test/__snapshots__/extended-info.test.ts.snap‎

Copy file name to clipboardExpand all lines: packages-engine/core/test/__snapshots__/extended-info.test.ts.snap
+6Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Map {
1111
"name:bar1;",
1212
undefined,
1313
{
14+
"__index": 0,
1415
"layer": "a",
1516
"sort": undefined,
1617
},
@@ -24,6 +25,7 @@ Map {
2425
"name": "bar1",
2526
},
2627
{
28+
"__index": 0,
2729
"layer": "a",
2830
},
2931
],
@@ -61,6 +63,7 @@ Map {
6163
"name:bar2;",
6264
undefined,
6365
{
66+
"__index": 1,
6467
"layer": "b",
6568
"sort": undefined,
6669
},
@@ -74,6 +77,7 @@ Map {
7477
"name": "bar2",
7578
},
7679
{
80+
"__index": 1,
7781
"layer": "b",
7882
},
7983
],
@@ -111,6 +115,7 @@ Map {
111115
"name:1;",
112116
undefined,
113117
{
118+
"__index": 2,
114119
"layer": "c",
115120
"sort": undefined,
116121
},
@@ -122,6 +127,7 @@ Map {
122127
/\\^c\\(\\\\d\\+\\)\\$/,
123128
[Function],
124129
{
130+
"__index": 2,
125131
"layer": "c",
126132
},
127133
],
Collapse file

‎packages-integrations/svelte-scoped/src/_preprocess/transformApply/getUtils.test.ts‎

Copy file name to clipboardExpand all lines: packages-integrations/svelte-scoped/src/_preprocess/transformApply/getUtils.test.ts
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe('getUtils', async () => {
3434
"margin-bottom:0.25rem;margin-right:0.25rem;",
3535
undefined,
3636
{
37+
"__index": 90,
3738
"layer": undefined,
3839
"sort": undefined,
3940
},
@@ -46,6 +47,7 @@ describe('getUtils', async () => {
4647
"margin-right:0.5rem;",
4748
undefined,
4849
{
50+
"__index": 90,
4951
"layer": undefined,
5052
"sort": 24,
5153
},
@@ -69,6 +71,7 @@ describe('getUtils', async () => {
6971
"margin-right:0.25rem;font-weight:500;",
7072
undefined,
7173
{
74+
"__index": 90,
7275
"layer": undefined,
7376
"sort": 24,
7477
},

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.