solid-optimizer 0.3.0 → 0.4.0
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 +26 -1
- package/dist/{compile-BroGlagZ.d.mts → compile-BQuBDA0Q.d.mts} +29 -2
- package/dist/compile-BQuBDA0Q.d.mts.map +1 -0
- package/dist/{compile-ocDYvJUW.mjs → compile-D3caSUtw.mjs} +401 -67
- package/dist/compile-D3caSUtw.mjs.map +1 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.mjs +1 -1
- package/dist/vite.d.mts +19 -1
- package/dist/vite.d.mts.map +1 -1
- package/dist/vite.mjs +419 -141
- package/dist/vite.mjs.map +1 -1
- package/package.json +1 -1
- package/src/ast.ts +12 -4
- package/src/compile.ts +61 -10
- package/src/context.ts +29 -0
- package/src/index.ts +1 -0
- package/src/inline.ts +225 -16
- package/src/memo.ts +43 -10
- package/src/provider.ts +65 -8
- package/src/size.ts +2 -0
- package/src/vite/index.ts +277 -34
- package/src/vite/inject.ts +41 -2
- package/src/vite/refs.ts +99 -0
- package/src/vite/report.ts +74 -0
- package/src/vite/runtime.ts +0 -20
- package/dist/compile-BroGlagZ.d.mts.map +0 -1
- package/dist/compile-ocDYvJUW.mjs.map +0 -1
package/src/memo.ts
CHANGED
|
@@ -37,7 +37,7 @@ import type {
|
|
|
37
37
|
} from 'oxc-parser';
|
|
38
38
|
import { isFunctionNode, isIntrinsicTag, unwrap } from './ast';
|
|
39
39
|
import type { PassContext } from './context';
|
|
40
|
-
import { solidExport, textOf } from './context';
|
|
40
|
+
import { recordSkip, solidExport, textOf } from './context';
|
|
41
41
|
import { usesFunctionContext } from './inline';
|
|
42
42
|
import type { Binding } from './scope';
|
|
43
43
|
import { lookup, scopeAt } from './scope';
|
|
@@ -104,6 +104,9 @@ class MemoInliner {
|
|
|
104
104
|
|
|
105
105
|
constructor(private readonly context: PassContext) {}
|
|
106
106
|
|
|
107
|
+
/** Why the last memo was turned down, for the report. Empty for a call that is not a memo. */
|
|
108
|
+
private reason = '';
|
|
109
|
+
|
|
107
110
|
private isCreateMemo(callee: Node): boolean {
|
|
108
111
|
if (callee.type !== 'Identifier') {
|
|
109
112
|
return false;
|
|
@@ -134,20 +137,24 @@ class MemoInliner {
|
|
|
134
137
|
initialValue?.type === 'SpreadElement' ||
|
|
135
138
|
(initialValue !== undefined && !isSideEffectFree(initialValue))
|
|
136
139
|
) {
|
|
140
|
+
this.reason = 'memo-computation';
|
|
137
141
|
return undefined;
|
|
138
142
|
}
|
|
139
143
|
const fn = unwrap(compute);
|
|
140
144
|
if (fn.type !== 'ArrowFunctionExpression' && fn.type !== 'FunctionExpression') {
|
|
145
|
+
this.reason = 'memo-computation';
|
|
141
146
|
return undefined;
|
|
142
147
|
}
|
|
143
148
|
const expression = returnedExpression(fn);
|
|
144
149
|
if (!expression) {
|
|
150
|
+
this.reason = 'memo-computation';
|
|
145
151
|
return undefined;
|
|
146
152
|
}
|
|
147
153
|
let alwaysNotifies = false;
|
|
148
154
|
if (options !== undefined) {
|
|
149
155
|
// Only `equals: false` is understood. Any other option changes what the memo does.
|
|
150
156
|
if (options.type !== 'ObjectExpression' || options.properties.length !== 1) {
|
|
157
|
+
this.reason = 'memo-options';
|
|
151
158
|
return undefined;
|
|
152
159
|
}
|
|
153
160
|
const property = options.properties.at(0);
|
|
@@ -159,11 +166,19 @@ class MemoInliner {
|
|
|
159
166
|
property.value.type !== 'Literal' ||
|
|
160
167
|
property.value.value !== false
|
|
161
168
|
) {
|
|
169
|
+
this.reason = 'memo-options';
|
|
162
170
|
return undefined;
|
|
163
171
|
}
|
|
164
172
|
alwaysNotifies = true;
|
|
165
173
|
}
|
|
166
|
-
|
|
174
|
+
if (alwaysNotifies || isFresh(expression)) {
|
|
175
|
+
return expression;
|
|
176
|
+
}
|
|
177
|
+
// A memo whose result can equal its last one stops updates, which is what it is for.
|
|
178
|
+
if (unwrap(expression).type === 'Literal') {
|
|
179
|
+
this.reason = 'memo-constant';
|
|
180
|
+
}
|
|
181
|
+
return undefined;
|
|
167
182
|
}
|
|
168
183
|
|
|
169
184
|
/**
|
|
@@ -325,31 +340,49 @@ class MemoInliner {
|
|
|
325
340
|
}
|
|
326
341
|
|
|
327
342
|
private tryInline(declarator: VariableDeclarator, binding: Binding): void {
|
|
343
|
+
this.reason = '';
|
|
344
|
+
if (this.inline(declarator, binding) === undefined && this.reason !== '') {
|
|
345
|
+
recordSkip(this.context, 'memos', this.reason, binding.name);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
private inline(declarator: VariableDeclarator, binding: Binding): true | undefined {
|
|
328
350
|
const expression = this.unstableComputation(declarator);
|
|
329
|
-
if (!expression
|
|
330
|
-
return;
|
|
351
|
+
if (!expression) {
|
|
352
|
+
return undefined;
|
|
353
|
+
}
|
|
354
|
+
if (binding.references.length !== 1) {
|
|
355
|
+
this.reason = 'memo-reads';
|
|
356
|
+
return undefined;
|
|
331
357
|
}
|
|
332
358
|
const statement = this.context.parents.get(declarator);
|
|
333
359
|
const fn = this.functionOf(declarator);
|
|
334
360
|
if (statement?.type !== 'VariableDeclaration' || statement.declarations.length !== 1 || !fn) {
|
|
335
|
-
|
|
361
|
+
this.reason = 'memo-declaration';
|
|
362
|
+
return undefined;
|
|
336
363
|
}
|
|
337
364
|
const reference = binding.references.at(0);
|
|
338
365
|
const call = reference ? this.context.parents.get(reference) : undefined;
|
|
366
|
+
if (call?.type !== 'CallExpression' || call.callee !== reference || call.arguments.length > 0) {
|
|
367
|
+
this.reason = 'memo-read';
|
|
368
|
+
return undefined;
|
|
369
|
+
}
|
|
370
|
+
if (!this.isSingleTrackedRead(call, fn)) {
|
|
371
|
+
this.reason = 'memo-read-place';
|
|
372
|
+
return undefined;
|
|
373
|
+
}
|
|
339
374
|
if (
|
|
340
|
-
call?.type !== 'CallExpression' ||
|
|
341
|
-
call.callee !== reference ||
|
|
342
|
-
call.arguments.length > 0 ||
|
|
343
|
-
!this.isSingleTrackedRead(call, fn) ||
|
|
344
375
|
!this.resolvesSameAt(expression, call, binding) ||
|
|
345
376
|
usesFunctionContext(this.context, expression)
|
|
346
377
|
) {
|
|
347
|
-
|
|
378
|
+
this.reason = 'memo-scope';
|
|
379
|
+
return undefined;
|
|
348
380
|
}
|
|
349
381
|
// The read sits in a JSX container, which takes any expression.
|
|
350
382
|
this.context.s.overwrite(call.start, call.end, textOf(this.context, expression));
|
|
351
383
|
this.context.s.remove(statement.start, statement.end);
|
|
352
384
|
this.changed = true;
|
|
385
|
+
return true;
|
|
353
386
|
}
|
|
354
387
|
|
|
355
388
|
run(): void {
|
package/src/provider.ts
CHANGED
|
@@ -40,7 +40,7 @@ import type {
|
|
|
40
40
|
} from 'oxc-parser';
|
|
41
41
|
import { forEachChild, isFunctionNode, isIntrinsicTag, unwrap } from './ast';
|
|
42
42
|
import type { PassContext } from './context';
|
|
43
|
-
import { isConstantDeclaration, solidExport } from './context';
|
|
43
|
+
import { isConstantDeclaration, recordSkip, solidExport } from './context';
|
|
44
44
|
import type { Binding } from './scope';
|
|
45
45
|
import { lookup, scopeAt } from './scope';
|
|
46
46
|
import { evaluate, isSideEffectFree, literalText } from './value';
|
|
@@ -301,6 +301,8 @@ export interface ClosedOptions {
|
|
|
301
301
|
readonly props?: Binding;
|
|
302
302
|
/** The value a read of a context gets, when the provider is known. */
|
|
303
303
|
readonly contextValue?: (target: Binding) => Expression | undefined;
|
|
304
|
+
/** Called with a short code for the code that is not visible, for the report. */
|
|
305
|
+
readonly onOpen?: (reason: string) => void;
|
|
304
306
|
}
|
|
305
307
|
|
|
306
308
|
/**
|
|
@@ -314,6 +316,10 @@ export interface ClosedOptions {
|
|
|
314
316
|
*/
|
|
315
317
|
export function isClosed(context: PassContext, root: Node, options: ClosedOptions): boolean {
|
|
316
318
|
let closed = true;
|
|
319
|
+
const open = (reason: string): void => {
|
|
320
|
+
closed = false;
|
|
321
|
+
options.onOpen?.(reason);
|
|
322
|
+
};
|
|
317
323
|
const visit = (node: Node): void => {
|
|
318
324
|
if (!closed) {
|
|
319
325
|
return;
|
|
@@ -327,25 +333,27 @@ export function isClosed(context: PassContext, root: Node, options: ClosedOption
|
|
|
327
333
|
case 'JSXSpreadAttribute':
|
|
328
334
|
// A view of the component's own props is expanded when it inlines.
|
|
329
335
|
if (!isOwnView(context, node.argument, options.props, 0)) {
|
|
330
|
-
|
|
336
|
+
open('open-spread');
|
|
331
337
|
return;
|
|
332
338
|
}
|
|
333
339
|
break;
|
|
334
|
-
case 'JSXSpreadChild':
|
|
335
340
|
case 'NewExpression':
|
|
341
|
+
open('open-new');
|
|
342
|
+
return;
|
|
343
|
+
case 'JSXSpreadChild':
|
|
336
344
|
case 'TaggedTemplateExpression':
|
|
337
345
|
case 'ImportExpression':
|
|
338
|
-
|
|
346
|
+
open('open-other');
|
|
339
347
|
return;
|
|
340
348
|
case 'JSXElement':
|
|
341
349
|
if (!isVisibleElement(context, node, options)) {
|
|
342
|
-
|
|
350
|
+
open('open-component');
|
|
343
351
|
return;
|
|
344
352
|
}
|
|
345
353
|
break;
|
|
346
354
|
case 'CallExpression':
|
|
347
355
|
if (!isVisibleCall(context, node, options)) {
|
|
348
|
-
|
|
356
|
+
open('open-call');
|
|
349
357
|
return;
|
|
350
358
|
}
|
|
351
359
|
break;
|
|
@@ -355,7 +363,7 @@ export function isClosed(context: PassContext, root: Node, options: ClosedOption
|
|
|
355
363
|
// binding declared under `root`, like a callback's parameter, is a value.
|
|
356
364
|
const binding = context.scopes.references.get(node);
|
|
357
365
|
if (binding && runsOutsideCode(context, binding, root, options, 0)) {
|
|
358
|
-
|
|
366
|
+
open('open-outer');
|
|
359
367
|
return;
|
|
360
368
|
}
|
|
361
369
|
break;
|
|
@@ -784,22 +792,69 @@ class ProviderRemover {
|
|
|
784
792
|
const overlaps = this.removed.some(
|
|
785
793
|
(other) => element.start < other.end && other.start < element.end,
|
|
786
794
|
);
|
|
787
|
-
if (
|
|
795
|
+
if (overlaps) {
|
|
796
|
+
continue;
|
|
797
|
+
}
|
|
798
|
+
if (hasNestedProvider(this.context, provider)) {
|
|
799
|
+
this.skip(provider, 'provider-nested');
|
|
800
|
+
} else if (this.tryRemove(provider)) {
|
|
788
801
|
this.removed.push(element);
|
|
789
802
|
}
|
|
790
803
|
}
|
|
804
|
+
if (this.context.skips) {
|
|
805
|
+
this.reportShapes();
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
|
|
809
|
+
private skip(provider: Provider, reason: string): void {
|
|
810
|
+
recordSkip(this.context, 'contexts', reason, provider.context.name);
|
|
811
|
+
}
|
|
812
|
+
|
|
813
|
+
/**
|
|
814
|
+
* Records each context tag that is not a provider this pass understands,
|
|
815
|
+
* such as one with more attributes than `value`.
|
|
816
|
+
*/
|
|
817
|
+
private reportShapes(): void {
|
|
818
|
+
const visit = (node: Node): void => {
|
|
819
|
+
if (node.type === 'JSXElement') {
|
|
820
|
+
const binding = providerContext(this.context, node.openingElement.name);
|
|
821
|
+
if (binding && !providerOf(this.context, node)) {
|
|
822
|
+
// A lone `value` is turned down because it could be `undefined`.
|
|
823
|
+
const { attributes } = node.openingElement;
|
|
824
|
+
const attribute = attributes.at(0);
|
|
825
|
+
const onlyValue =
|
|
826
|
+
attributes.length === 1 &&
|
|
827
|
+
attribute?.type === 'JSXAttribute' &&
|
|
828
|
+
attribute.name.type === 'JSXIdentifier' &&
|
|
829
|
+
attribute.name.name === 'value';
|
|
830
|
+
recordSkip(
|
|
831
|
+
this.context,
|
|
832
|
+
'contexts',
|
|
833
|
+
onlyValue && !node.openingElement.selfClosing ? 'provider-undefined' : 'provider-shape',
|
|
834
|
+
binding.name,
|
|
835
|
+
);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
forEachChild(node, visit);
|
|
839
|
+
};
|
|
840
|
+
visit(this.context.program);
|
|
791
841
|
}
|
|
792
842
|
|
|
793
843
|
private tryRemove(provider: Provider): boolean {
|
|
794
844
|
const { element } = provider;
|
|
795
845
|
const children = element.children;
|
|
846
|
+
let reason = 'open-other';
|
|
796
847
|
const closed = children.every((child) =>
|
|
797
848
|
isClosed(this.context, child, {
|
|
798
849
|
component: () => false,
|
|
799
850
|
contextValue: (target) => (target === provider.context ? provider.value : undefined),
|
|
851
|
+
onOpen: (open) => {
|
|
852
|
+
reason = open;
|
|
853
|
+
},
|
|
800
854
|
}),
|
|
801
855
|
);
|
|
802
856
|
if (!closed) {
|
|
857
|
+
this.skip(provider, reason);
|
|
803
858
|
return false;
|
|
804
859
|
}
|
|
805
860
|
const reads = children.flatMap((child) => contextReads(this.context, child, provider.context));
|
|
@@ -807,6 +862,7 @@ class ProviderRemover {
|
|
|
807
862
|
for (const read of reads) {
|
|
808
863
|
const text = stableValueText(this.context, provider, read);
|
|
809
864
|
if (text === undefined) {
|
|
865
|
+
this.skip(provider, 'provider-value');
|
|
810
866
|
return false;
|
|
811
867
|
}
|
|
812
868
|
texts.push(text);
|
|
@@ -815,6 +871,7 @@ class ProviderRemover {
|
|
|
815
871
|
if (reads.length === 0 && !isSideEffectFree(provider.value)) {
|
|
816
872
|
const stable = stableValueText(this.context, provider, element);
|
|
817
873
|
if (stable === undefined) {
|
|
874
|
+
this.skip(provider, 'provider-value');
|
|
818
875
|
return false;
|
|
819
876
|
}
|
|
820
877
|
}
|
package/src/size.ts
CHANGED