solid-optimizer 1.0.0-next.5 → 1.0.0-next.7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +6 -2
- package/dist/{compile-By9Fecbx.mjs → compile-BuPR3pVT.mjs} +478 -73
- package/dist/compile-BuPR3pVT.mjs.map +1 -0
- package/dist/compile-X_SMDFr2.d.mts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/vite.mjs +1 -1
- package/package.json +5 -2
- package/src/compile.ts +21 -0
- package/src/fold.ts +41 -3
- package/src/inline.ts +15 -19
- package/src/jsx.ts +31 -16
- package/src/memo.ts +287 -22
- package/src/nesting.ts +325 -0
- package/dist/compile-By9Fecbx.mjs.map +0 -1
package/src/memo.ts
CHANGED
|
@@ -44,6 +44,31 @@
|
|
|
44
44
|
*
|
|
45
45
|
* becomes `<div><b>x</b></div>`.
|
|
46
46
|
*
|
|
47
|
+
* A signal that nothing writes is a constant too. When its setter is left
|
|
48
|
+
* out or never referenced, and its initial value is a literal, every read
|
|
49
|
+
* becomes the literal:
|
|
50
|
+
*
|
|
51
|
+
* ```jsx
|
|
52
|
+
* const [size] = createSignal(3);
|
|
53
|
+
* return <p>{size()}</p>;
|
|
54
|
+
* ```
|
|
55
|
+
*
|
|
56
|
+
* becomes `<p>{3}</p>`. A signal with a value that is not a function takes no
|
|
57
|
+
* owner and no hydration id, so removing it changes nothing else.
|
|
58
|
+
*
|
|
59
|
+
* A plain arrow that only returns an expression, like a derived accessor,
|
|
60
|
+
* has no cache and no owner, so each call already runs its body again. The
|
|
61
|
+
* calls can become the body:
|
|
62
|
+
*
|
|
63
|
+
* ```jsx
|
|
64
|
+
* const label = () => (props.open ? 'Close' : 'Open');
|
|
65
|
+
* return <button>{label()}</button>;
|
|
66
|
+
* ```
|
|
67
|
+
*
|
|
68
|
+
* becomes `<button>{(props.open ? 'Close' : 'Open')}</button>`. With more than
|
|
69
|
+
* one call, the body is copied only when the copies are no larger than the
|
|
70
|
+
* arrow and its calls.
|
|
71
|
+
*
|
|
47
72
|
* # Accepted differences
|
|
48
73
|
*
|
|
49
74
|
* - The computation first runs when its reader first runs, instead of when
|
|
@@ -51,6 +76,9 @@
|
|
|
51
76
|
* - The memo no longer takes a slot in the owner tree, which shifts the
|
|
52
77
|
* hydration keys after it. A server build and its client build must
|
|
53
78
|
* compile with the same options.
|
|
79
|
+
* - A call that runs before the declaration, from a hoisted function the
|
|
80
|
+
* declaration comes before, throws on the original and returns the value
|
|
81
|
+
* on the output.
|
|
54
82
|
*/
|
|
55
83
|
import type {
|
|
56
84
|
ArrowFunctionExpression,
|
|
@@ -59,14 +87,16 @@ import type {
|
|
|
59
87
|
Function as FunctionNode,
|
|
60
88
|
JSXExpressionContainer,
|
|
61
89
|
Node,
|
|
90
|
+
ObjectExpression,
|
|
62
91
|
VariableDeclarator,
|
|
63
92
|
} from 'oxc-parser';
|
|
64
|
-
import { isFunctionNode, isIntrinsicTag, unwrap } from './ast';
|
|
93
|
+
import { isFunctionNode, isIntrinsicTag, isPrimary, unwrap } from './ast';
|
|
65
94
|
import type { PassContext } from './context';
|
|
66
95
|
import { getText, recordSkip, solidExport } from './context';
|
|
67
96
|
import { usesFunctionContext } from './inline';
|
|
68
97
|
import type { Binding } from './scope';
|
|
69
98
|
import { lookup, scopeAt } from './scope';
|
|
99
|
+
import { compactLength } from './size';
|
|
70
100
|
|
|
71
101
|
/**
|
|
72
102
|
* Props of Solid's built-in components that are read inside a memo, once per update.
|
|
@@ -78,34 +108,107 @@ const TRACKED_PROPS = new Map([
|
|
|
78
108
|
['Repeat', 'count'],
|
|
79
109
|
]);
|
|
80
110
|
|
|
111
|
+
/**
|
|
112
|
+
* Global constructors whose instances are never thenable or async iterable.
|
|
113
|
+
*/
|
|
114
|
+
const SYNC_CONSTRUCTORS = new Set([
|
|
115
|
+
'Array',
|
|
116
|
+
'Date',
|
|
117
|
+
'Error',
|
|
118
|
+
'Map',
|
|
119
|
+
'RegExp',
|
|
120
|
+
'Set',
|
|
121
|
+
'URL',
|
|
122
|
+
'URLSearchParams',
|
|
123
|
+
'WeakMap',
|
|
124
|
+
'WeakSet',
|
|
125
|
+
]);
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Whether an object literal can have a `then` method or `Symbol.asyncIterator`,
|
|
129
|
+
* its own or from a `__proto__` it sets.
|
|
130
|
+
*/
|
|
131
|
+
function mayBeThenable(node: ObjectExpression): boolean {
|
|
132
|
+
for (const property of node.properties) {
|
|
133
|
+
if (property.type === 'SpreadElement' || property.computed) {
|
|
134
|
+
return true;
|
|
135
|
+
}
|
|
136
|
+
const { key } = property;
|
|
137
|
+
let name: unknown;
|
|
138
|
+
if (key.type === 'Identifier') {
|
|
139
|
+
name = key.name;
|
|
140
|
+
} else if ('value' in key) {
|
|
141
|
+
name = key.value;
|
|
142
|
+
}
|
|
143
|
+
if (name === 'then' || name === '__proto__') {
|
|
144
|
+
return true;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return false;
|
|
148
|
+
}
|
|
149
|
+
|
|
81
150
|
/**
|
|
82
151
|
* Whether evaluating `node` always creates a new value, which reference
|
|
83
|
-
* equality never finds equal to the last one.
|
|
152
|
+
* equality never finds equal to the last one. The value is never a promise
|
|
153
|
+
* or an async iterable, which a memo waits for and an inlined read does not.
|
|
154
|
+
* `isGlobal` tells whether a name is one the module does not declare.
|
|
84
155
|
*/
|
|
85
|
-
function isFresh(node: Node): boolean {
|
|
156
|
+
function isFresh(node: Node, isGlobal: (node: Node) => boolean): boolean {
|
|
86
157
|
switch (node.type) {
|
|
87
|
-
case 'ObjectExpression':
|
|
88
158
|
case 'ArrayExpression':
|
|
89
159
|
case 'ArrowFunctionExpression':
|
|
90
160
|
case 'FunctionExpression':
|
|
91
|
-
case 'ClassExpression':
|
|
92
|
-
case 'NewExpression':
|
|
93
161
|
case 'JSXElement':
|
|
94
162
|
case 'JSXFragment':
|
|
95
163
|
return true;
|
|
164
|
+
case 'ObjectExpression':
|
|
165
|
+
return !mayBeThenable(node);
|
|
166
|
+
case 'NewExpression':
|
|
167
|
+
return (
|
|
168
|
+
node.callee.type === 'Identifier' &&
|
|
169
|
+
SYNC_CONSTRUCTORS.has(node.callee.name) &&
|
|
170
|
+
isGlobal(node.callee)
|
|
171
|
+
);
|
|
96
172
|
case 'ParenthesizedExpression':
|
|
97
|
-
return isFresh(node.expression);
|
|
173
|
+
return isFresh(node.expression, isGlobal);
|
|
98
174
|
case 'ConditionalExpression':
|
|
99
|
-
return isFresh(node.consequent) && isFresh(node.alternate);
|
|
175
|
+
return isFresh(node.consequent, isGlobal) && isFresh(node.alternate, isGlobal);
|
|
100
176
|
case 'SequenceExpression': {
|
|
101
177
|
const last = node.expressions.at(-1);
|
|
102
|
-
return last !== undefined && isFresh(last);
|
|
178
|
+
return last !== undefined && isFresh(last, isGlobal);
|
|
103
179
|
}
|
|
104
180
|
default:
|
|
105
181
|
return false;
|
|
106
182
|
}
|
|
107
183
|
}
|
|
108
184
|
|
|
185
|
+
/**
|
|
186
|
+
* Whether `node` evaluates to a value that is never a promise or an async
|
|
187
|
+
* iterable. A memo waits for one of those, and an inlined read does not.
|
|
188
|
+
*/
|
|
189
|
+
function isSync(node: Node, isGlobal: (node: Node) => boolean): boolean {
|
|
190
|
+
switch (node.type) {
|
|
191
|
+
case 'Literal':
|
|
192
|
+
case 'TemplateLiteral':
|
|
193
|
+
case 'UnaryExpression':
|
|
194
|
+
case 'UpdateExpression':
|
|
195
|
+
case 'BinaryExpression':
|
|
196
|
+
return true;
|
|
197
|
+
case 'ParenthesizedExpression':
|
|
198
|
+
return isSync(node.expression, isGlobal);
|
|
199
|
+
case 'ConditionalExpression':
|
|
200
|
+
return isSync(node.consequent, isGlobal) && isSync(node.alternate, isGlobal);
|
|
201
|
+
case 'LogicalExpression':
|
|
202
|
+
return isSync(node.left, isGlobal) && isSync(node.right, isGlobal);
|
|
203
|
+
case 'SequenceExpression': {
|
|
204
|
+
const last = node.expressions.at(-1);
|
|
205
|
+
return last !== undefined && isSync(last, isGlobal);
|
|
206
|
+
}
|
|
207
|
+
default:
|
|
208
|
+
return isFresh(node, isGlobal);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
109
212
|
/**
|
|
110
213
|
* The expression a compute function returns, when that is all it does.
|
|
111
214
|
*/
|
|
@@ -152,6 +255,9 @@ function returnedExpression(fn: FunctionNode | ArrowFunctionExpression): Express
|
|
|
152
255
|
class MemoInliner {
|
|
153
256
|
changed = false;
|
|
154
257
|
|
|
258
|
+
/** Ranges of the source rewritten so far in this run. */
|
|
259
|
+
private readonly edited: [number, number][] = [];
|
|
260
|
+
|
|
155
261
|
constructor(private readonly context: PassContext) {}
|
|
156
262
|
|
|
157
263
|
/** Why the last memo was turned down, for the report. Empty for a call that is not a memo. */
|
|
@@ -225,7 +331,7 @@ class MemoInliner {
|
|
|
225
331
|
}
|
|
226
332
|
|
|
227
333
|
/**
|
|
228
|
-
* Replaces every read of a memo whose
|
|
334
|
+
* Replaces every read of a memo or signal whose value is a literal with the
|
|
229
335
|
* literal. Each reference has to be a call after the declaration, since a
|
|
230
336
|
* call before it would throw.
|
|
231
337
|
*/
|
|
@@ -233,10 +339,11 @@ class MemoInliner {
|
|
|
233
339
|
declarator: VariableDeclarator,
|
|
234
340
|
binding: Binding,
|
|
235
341
|
text: string,
|
|
342
|
+
kind: 'memo' | 'signal' = 'memo',
|
|
236
343
|
): true | undefined {
|
|
237
344
|
const statement = this.context.parents.get(declarator);
|
|
238
345
|
if (statement?.type !== 'VariableDeclaration' || statement.declarations.length !== 1) {
|
|
239
|
-
this.reason =
|
|
346
|
+
this.reason = `${kind}-declaration`;
|
|
240
347
|
return undefined;
|
|
241
348
|
}
|
|
242
349
|
const calls: CallExpression[] = [];
|
|
@@ -248,7 +355,7 @@ class MemoInliner {
|
|
|
248
355
|
call.arguments.length > 0 ||
|
|
249
356
|
call.start < declarator.end
|
|
250
357
|
) {
|
|
251
|
-
this.reason =
|
|
358
|
+
this.reason = `${kind}-constant`;
|
|
252
359
|
return undefined;
|
|
253
360
|
}
|
|
254
361
|
calls.push(call);
|
|
@@ -264,8 +371,10 @@ class MemoInliner {
|
|
|
264
371
|
parent?.type === 'Property' ||
|
|
265
372
|
(parent?.type === 'CallExpression' && parent.callee !== call);
|
|
266
373
|
this.context.s.overwrite(call.start, call.end, bare ? text : `(${text})`);
|
|
374
|
+
this.edited.push([call.start, call.end]);
|
|
267
375
|
}
|
|
268
376
|
this.context.s.remove(statement.start, statement.end);
|
|
377
|
+
this.edited.push([statement.start, statement.end]);
|
|
269
378
|
this.changed = true;
|
|
270
379
|
return true;
|
|
271
380
|
}
|
|
@@ -462,8 +571,16 @@ class MemoInliner {
|
|
|
462
571
|
if (constant !== undefined) {
|
|
463
572
|
return this.inlineConstant(declarator, binding, constant);
|
|
464
573
|
}
|
|
574
|
+
const isGlobal = (node: Node): boolean =>
|
|
575
|
+
this.context.scopes.references.get(node) === undefined;
|
|
465
576
|
// A memo whose result can equal its last one stops updates, which is what it is for.
|
|
466
|
-
if (!alwaysNotifies && !isFresh(expression)) {
|
|
577
|
+
if (!alwaysNotifies && !isFresh(expression, isGlobal)) {
|
|
578
|
+
return undefined;
|
|
579
|
+
}
|
|
580
|
+
// A memo waits for a promise or an async iterable. The insert an inlined
|
|
581
|
+
// read becomes takes the value as it is.
|
|
582
|
+
if (alwaysNotifies && !isSync(expression, isGlobal)) {
|
|
583
|
+
this.reason = 'memo-async';
|
|
467
584
|
return undefined;
|
|
468
585
|
}
|
|
469
586
|
}
|
|
@@ -507,30 +624,178 @@ class MemoInliner {
|
|
|
507
624
|
if (kind === 'children' && container) {
|
|
508
625
|
// The JSX takes the place of its container, so it joins the template around it.
|
|
509
626
|
this.context.s.overwrite(container.start, container.end, getText(this.context, expression));
|
|
627
|
+
this.edited.push([container.start, container.end]);
|
|
510
628
|
} else {
|
|
511
629
|
// The read sits in a JSX container, which takes any expression.
|
|
512
630
|
this.context.s.overwrite(call.start, call.end, getText(this.context, expression));
|
|
631
|
+
this.edited.push([call.start, call.end]);
|
|
513
632
|
}
|
|
514
633
|
this.context.s.remove(statement.start, statement.end);
|
|
634
|
+
this.edited.push([statement.start, statement.end]);
|
|
515
635
|
this.changed = true;
|
|
516
636
|
return true;
|
|
517
637
|
}
|
|
518
638
|
|
|
639
|
+
/**
|
|
640
|
+
* The literal a signal always holds: `const [get] = createSignal(literal)`,
|
|
641
|
+
* or `const [get, set] = ...` with nothing referencing `set`. Options can
|
|
642
|
+
* change what a read does, such as `unobserved`, so a signal with options
|
|
643
|
+
* stays.
|
|
644
|
+
*/
|
|
645
|
+
private signalConstant(declarator: VariableDeclarator, binding: Binding): string | undefined {
|
|
646
|
+
const pattern = declarator.id;
|
|
647
|
+
const init = declarator.init ? unwrap(declarator.init) : undefined;
|
|
648
|
+
if (
|
|
649
|
+
pattern.type !== 'ArrayPattern' ||
|
|
650
|
+
pattern.elements.at(0) !== binding.identifier ||
|
|
651
|
+
pattern.elements.length > 2 ||
|
|
652
|
+
init?.type !== 'CallExpression' ||
|
|
653
|
+
init.callee.type !== 'Identifier' ||
|
|
654
|
+
init.arguments.length !== 1
|
|
655
|
+
) {
|
|
656
|
+
return undefined;
|
|
657
|
+
}
|
|
658
|
+
const callee = this.context.scopes.references.get(init.callee);
|
|
659
|
+
if (!callee || solidExport(callee, this.context.options) !== 'createSignal') {
|
|
660
|
+
return undefined;
|
|
661
|
+
}
|
|
662
|
+
const value = init.arguments.at(0);
|
|
663
|
+
if (!value || value.type === 'SpreadElement') {
|
|
664
|
+
return undefined;
|
|
665
|
+
}
|
|
666
|
+
// Other modules can read an exported getter.
|
|
667
|
+
const statement = this.context.parents.get(declarator);
|
|
668
|
+
if (statement && this.context.parents.get(statement)?.type === 'ExportNamedDeclaration') {
|
|
669
|
+
return undefined;
|
|
670
|
+
}
|
|
671
|
+
const setter = pattern.elements.at(1);
|
|
672
|
+
if (setter) {
|
|
673
|
+
const setterBinding =
|
|
674
|
+
setter.type === 'Identifier' ? binding.scope.bindings.get(setter.name) : undefined;
|
|
675
|
+
if (setterBinding?.identifier !== setter || setterBinding.references.length > 0) {
|
|
676
|
+
return undefined;
|
|
677
|
+
}
|
|
678
|
+
}
|
|
679
|
+
return constantText(this.context, value);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
private tryInlineSignal(declarator: VariableDeclarator, binding: Binding): void {
|
|
683
|
+
const text = this.signalConstant(declarator, binding);
|
|
684
|
+
if (text === undefined) {
|
|
685
|
+
return;
|
|
686
|
+
}
|
|
687
|
+
this.reason = '';
|
|
688
|
+
if (this.inlineConstant(declarator, binding, text, 'signal') === undefined) {
|
|
689
|
+
recordSkip(this.context, 'memos', this.reason, binding.name);
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* Replaces every call of `const name = () => expression` with the
|
|
695
|
+
* expression. Each reference has to be a call with no arguments after the
|
|
696
|
+
* declaration, and every name in the body has to mean the same at the call.
|
|
697
|
+
*/
|
|
698
|
+
private tryInlineArrow(declarator: VariableDeclarator, binding: Binding): void {
|
|
699
|
+
const fn = declarator.init ? unwrap(declarator.init) : undefined;
|
|
700
|
+
if (fn?.type !== 'ArrowFunctionExpression' || fn.async || fn.params.length > 0) {
|
|
701
|
+
return;
|
|
702
|
+
}
|
|
703
|
+
const body = returnedExpression(fn);
|
|
704
|
+
const statement = this.context.parents.get(declarator);
|
|
705
|
+
if (
|
|
706
|
+
!body ||
|
|
707
|
+
statement?.type !== 'VariableDeclaration' ||
|
|
708
|
+
statement.declarations.length !== 1 ||
|
|
709
|
+
// Other modules can call an exported arrow.
|
|
710
|
+
this.context.parents.get(statement)?.type === 'ExportNamedDeclaration'
|
|
711
|
+
) {
|
|
712
|
+
return;
|
|
713
|
+
}
|
|
714
|
+
const calls: CallExpression[] = [];
|
|
715
|
+
for (const reference of binding.references) {
|
|
716
|
+
const call = this.context.parents.get(reference);
|
|
717
|
+
if (
|
|
718
|
+
call?.type !== 'CallExpression' ||
|
|
719
|
+
call.callee !== reference ||
|
|
720
|
+
call.optional ||
|
|
721
|
+
call.arguments.length > 0 ||
|
|
722
|
+
call.start < declarator.end
|
|
723
|
+
) {
|
|
724
|
+
// The arrow is used as a value, like an event handler. That is not a derivation.
|
|
725
|
+
return;
|
|
726
|
+
}
|
|
727
|
+
// `l()()` calls the result with no `this`, but `(a.b)()` passes `a`, and
|
|
728
|
+
// `delete (a.b)` deletes what `delete l()` leaves alone.
|
|
729
|
+
const parent = this.context.parents.get(call);
|
|
730
|
+
if (
|
|
731
|
+
(parent?.type === 'CallExpression' && parent.callee === call) ||
|
|
732
|
+
(parent?.type === 'TaggedTemplateExpression' && parent.tag === call) ||
|
|
733
|
+
(parent?.type === 'UnaryExpression' && parent.operator === 'delete')
|
|
734
|
+
) {
|
|
735
|
+
return;
|
|
736
|
+
}
|
|
737
|
+
calls.push(call);
|
|
738
|
+
}
|
|
739
|
+
if (calls.length === 0) {
|
|
740
|
+
return;
|
|
741
|
+
}
|
|
742
|
+
// The body is copied from the source, so it must not hold an edit from this run.
|
|
743
|
+
// The next round sees the edit.
|
|
744
|
+
for (const [start, end] of this.edited) {
|
|
745
|
+
if (start < statement.end && end > statement.start) {
|
|
746
|
+
return;
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
const text = getText(this.context, body);
|
|
750
|
+
// Minified, the arrow costs `const a=()=>body;`, 13 characters besides the
|
|
751
|
+
// body, and each call `a()` costs 3. Copying adds the body to every call
|
|
752
|
+
// but one, and removes the rest.
|
|
753
|
+
const length = compactLength(text);
|
|
754
|
+
if ((calls.length - 1) * length > calls.length * 3 + 13) {
|
|
755
|
+
return;
|
|
756
|
+
}
|
|
757
|
+
if (usesFunctionContext(this.context, fn)) {
|
|
758
|
+
return;
|
|
759
|
+
}
|
|
760
|
+
for (const call of calls) {
|
|
761
|
+
if (!this.resolvesSameAt(body, call, binding)) {
|
|
762
|
+
return;
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
const bare = isPrimary(body);
|
|
766
|
+
for (const call of calls) {
|
|
767
|
+
const parent = this.context.parents.get(call);
|
|
768
|
+
const inContainer =
|
|
769
|
+
parent?.type === 'JSXExpressionContainer' && body.type !== 'SequenceExpression';
|
|
770
|
+
this.context.s.overwrite(call.start, call.end, bare || inContainer ? text : `(${text})`);
|
|
771
|
+
this.edited.push([call.start, call.end]);
|
|
772
|
+
}
|
|
773
|
+
this.context.s.remove(statement.start, statement.end);
|
|
774
|
+
this.edited.push([statement.start, statement.end]);
|
|
775
|
+
this.changed = true;
|
|
776
|
+
}
|
|
777
|
+
|
|
519
778
|
run(): void {
|
|
520
779
|
for (const scope of new Set(this.context.scopes.scopes.values())) {
|
|
521
|
-
// A memo outside any function has no owner, and disposes differently.
|
|
522
|
-
if (scope.parent === undefined) {
|
|
523
|
-
continue;
|
|
524
|
-
}
|
|
525
780
|
for (const binding of scope.bindings.values()) {
|
|
526
781
|
const { declaration } = binding;
|
|
527
782
|
if (
|
|
528
|
-
binding.kind
|
|
529
|
-
|
|
530
|
-
declaration.type
|
|
531
|
-
declaration.id === binding.identifier
|
|
783
|
+
binding.kind !== 'const' ||
|
|
784
|
+
binding.mutated ||
|
|
785
|
+
declaration.type !== 'VariableDeclarator'
|
|
532
786
|
) {
|
|
533
|
-
|
|
787
|
+
continue;
|
|
788
|
+
}
|
|
789
|
+
if (declaration.id.type === 'ArrayPattern') {
|
|
790
|
+
// A signal takes no owner, so one outside any function works the same.
|
|
791
|
+
this.tryInlineSignal(declaration, binding);
|
|
792
|
+
} else if (declaration.id === binding.identifier) {
|
|
793
|
+
// An arrow has no owner, so one outside any function works the same.
|
|
794
|
+
this.tryInlineArrow(declaration, binding);
|
|
795
|
+
// A memo outside any function has no owner, and disposes differently.
|
|
796
|
+
if (scope.parent !== undefined) {
|
|
797
|
+
this.tryInline(declaration, binding);
|
|
798
|
+
}
|
|
534
799
|
}
|
|
535
800
|
}
|
|
536
801
|
}
|