solid-optimizer 1.0.0-next.5 → 1.0.0-next.6
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-DnfP-Quf.mjs} +112 -13
- package/dist/compile-DnfP-Quf.mjs.map +1 -0
- package/dist/index.mjs +1 -1
- package/dist/vite.mjs +1 -1
- package/package.json +1 -1
- package/src/fold.ts +26 -1
- package/src/memo.ts +196 -13
- package/dist/compile-By9Fecbx.mjs.map +0 -1
package/README.md
CHANGED
|
@@ -142,7 +142,7 @@ Each line is a pass, a reason, how many times it happened, and the names it happ
|
|
|
142
142
|
- `lazy`, `imported`, `nested`, and `not-a-function` mean the tag is not a component declared at the top of the same module or chunk.
|
|
143
143
|
- A reason that starts with `copy-` is about copying a component from another module. `copy-package` means it comes from a package in `node_modules` that `optimizer.packages` does not list, `copy-cycle` means the two modules import each other, `copy-wrapper` means the component only returns a call of another component in its module, and `copy-size` means the copy was larger.
|
|
144
144
|
|
|
145
|
-
The provider pass reports `open-component`, `open-call`, `open-new`, `open-spread`, and `open-outer` for code under the provider that it cannot see into, and `provider-value`, `provider-undefined`, and `provider-shape` for a value or provider it cannot handle. The memo pass reports `memo-reads`, `memo-read-place`, `memo-constant`, and a few more for a memo it keeps.
|
|
145
|
+
The provider pass reports `open-component`, `open-call`, `open-new`, `open-spread`, and `open-outer` for code under the provider that it cannot see into, and `provider-value`, `provider-undefined`, and `provider-shape` for a value or provider it cannot handle. The memo pass reports `memo-reads`, `memo-read-place`, `memo-constant`, and a few more for a memo it keeps, and `signal-constant` for a signal that nothing writes but that is used other than by calling it.
|
|
146
146
|
|
|
147
147
|
### Limits
|
|
148
148
|
|
|
@@ -306,6 +306,8 @@ becomes `<p style={{ color: color() }} />`. See [strategy.md](https://github.com
|
|
|
306
306
|
|
|
307
307
|
A memo whose computation folds to a literal never changes. Every call of it becomes the literal, however many there are, when each call comes after the declaration and nothing else uses the memo.
|
|
308
308
|
|
|
309
|
+
So does a signal that nothing writes: `const [size] = createSignal(3)`, or one whose setter nothing references. With a literal and no options, every call of the getter becomes the literal, under the same conditions. This also works outside a function, since a signal takes no owner.
|
|
310
|
+
|
|
309
311
|
```jsx
|
|
310
312
|
const size = createMemo(() => 1 + 2);
|
|
311
313
|
return <p>{size() * 2}</p>;
|
|
@@ -313,6 +315,8 @@ return <p>{size() * 2}</p>;
|
|
|
313
315
|
|
|
314
316
|
becomes `<p>{6}</p>`.
|
|
315
317
|
|
|
318
|
+
A plain arrow that only returns an expression, like `const label = () => (props.open ? 'Close' : 'Open')`, has no cache and no owner, so each call becomes its body. That needs every call to come after the declaration with no arguments, nothing else to use the arrow, and every name in the body to mean the same at the call. With more than one call, the body is copied only when the copies are no larger than the arrow and its calls, minified.
|
|
319
|
+
|
|
316
320
|
Solid's `children` helper over JSX, read once as an element's child, becomes that JSX. The helper resolves JSX the same way inserting it does, and nothing makes it run again.
|
|
317
321
|
|
|
318
322
|
```jsx
|
|
@@ -352,7 +356,7 @@ This follows the `optimize` option of `@solidjs/compiler` ([solidjs/solid#3231](
|
|
|
352
356
|
|
|
353
357
|
A built-in only folds when the tag resolves to Solid's component. The tag must bind to nothing, or be imported from one of `moduleSources`. A tag with a spread attribute or a function child never folds. `<Portal>`, `<Loading>`, `<Errored>`, and `<Reveal>` never fold.
|
|
354
358
|
|
|
355
|
-
A call to Solid's `lazy` is marked `/* @__PURE__ */`, so the bundler drops one that a folded branch left unused, along with its chunk.
|
|
359
|
+
A call to Solid's `lazy` is marked `/* @__PURE__ */`, so the bundler drops one that a folded branch left unused, along with its chunk. `createContext` is marked too, and so are `createSignal` and `createStore` whose first argument is a literal, an object, or an array.
|
|
356
360
|
|
|
357
361
|
A condition that is discarded but has side effects still runs. `[effect()] && value` becomes `([effect()], value)`.
|
|
358
362
|
|
|
@@ -1301,6 +1301,18 @@ const EMPTY = { kind: "empty" };
|
|
|
1301
1301
|
/** Solid's calls that only create a value, so the bundler can drop an unused one. */
|
|
1302
1302
|
const PURE_CALLS = /* @__PURE__ */ new Set(["lazy", "createContext"]);
|
|
1303
1303
|
/**
|
|
1304
|
+
* Solid's calls that only create a value when their first argument is not a
|
|
1305
|
+
* function. Given a function, they create a computation, which runs it.
|
|
1306
|
+
*/
|
|
1307
|
+
const PURE_VALUE_CALLS = /* @__PURE__ */ new Set(["createSignal", "createStore"]);
|
|
1308
|
+
/** Expression types that are never a function. */
|
|
1309
|
+
const NOT_FUNCTION = /* @__PURE__ */ new Set([
|
|
1310
|
+
"Literal",
|
|
1311
|
+
"TemplateLiteral",
|
|
1312
|
+
"ObjectExpression",
|
|
1313
|
+
"ArrayExpression"
|
|
1314
|
+
]);
|
|
1315
|
+
/**
|
|
1304
1316
|
* The globals worth folding. They only apply to a reference that binds to
|
|
1305
1317
|
* nothing, which is when the global is what runs.
|
|
1306
1318
|
*/
|
|
@@ -1385,6 +1397,12 @@ function keyName(node) {
|
|
|
1385
1397
|
if (node.type === "Identifier") return node.name;
|
|
1386
1398
|
return node.type === "Literal" && typeof node.value === "string" ? node.value : void 0;
|
|
1387
1399
|
}
|
|
1400
|
+
/** Whether a call has no first argument, or one that is never a function. */
|
|
1401
|
+
function isValueArgument(call) {
|
|
1402
|
+
if (call.arguments.length === 0) return true;
|
|
1403
|
+
const first = call.arguments[0];
|
|
1404
|
+
return first.type !== "SpreadElement" && NOT_FUNCTION.has(unwrap(first).type);
|
|
1405
|
+
}
|
|
1388
1406
|
/**
|
|
1389
1407
|
* The text of an expression that always gives the same primitive: a literal
|
|
1390
1408
|
* other than a regular expression, a template literal without expressions,
|
|
@@ -1490,12 +1508,17 @@ var Folder = class {
|
|
|
1490
1508
|
* branch can leave it unused, and the annotation lets the bundler drop it
|
|
1491
1509
|
* along with its dynamic import. `createContext` only creates the context,
|
|
1492
1510
|
* which inlining can leave unused.
|
|
1511
|
+
*
|
|
1512
|
+
* `createSignal` and `createStore` with a first argument that is not a
|
|
1513
|
+
* function only create a node. It takes no owner and no hydration id, so
|
|
1514
|
+
* dropping an unused one changes nothing else.
|
|
1493
1515
|
*/
|
|
1494
1516
|
annotatePure(call) {
|
|
1495
1517
|
if (call.callee.type !== "Identifier") return;
|
|
1496
1518
|
const binding = this.context.scopes.references.get(call.callee);
|
|
1497
1519
|
const identity = binding ? solidExport(binding, this.context.options) : void 0;
|
|
1498
|
-
if (identity === void 0
|
|
1520
|
+
if (identity === void 0) return;
|
|
1521
|
+
if (!PURE_CALLS.has(identity) && !(PURE_VALUE_CALLS.has(identity) && isValueArgument(call))) return;
|
|
1499
1522
|
const before = this.context.code.slice(0, call.start).trimEnd();
|
|
1500
1523
|
if (/(#|@)__PURE__\s*\*\/$/.test(before)) return;
|
|
1501
1524
|
this.context.s.prependLeft(call.start, "/* @__PURE__ */ ");
|
|
@@ -4543,6 +4566,7 @@ var MemoInliner = class {
|
|
|
4543
4566
|
constructor(context) {
|
|
4544
4567
|
this.context = context;
|
|
4545
4568
|
this.changed = false;
|
|
4569
|
+
this.edited = [];
|
|
4546
4570
|
this.reason = "";
|
|
4547
4571
|
}
|
|
4548
4572
|
/** Whether a callee is Solid's `createMemo` or `children`. */
|
|
@@ -4601,21 +4625,21 @@ var MemoInliner = class {
|
|
|
4601
4625
|
};
|
|
4602
4626
|
}
|
|
4603
4627
|
/**
|
|
4604
|
-
* Replaces every read of a memo whose
|
|
4628
|
+
* Replaces every read of a memo or signal whose value is a literal with the
|
|
4605
4629
|
* literal. Each reference has to be a call after the declaration, since a
|
|
4606
4630
|
* call before it would throw.
|
|
4607
4631
|
*/
|
|
4608
|
-
inlineConstant(declarator, binding, text) {
|
|
4632
|
+
inlineConstant(declarator, binding, text, kind = "memo") {
|
|
4609
4633
|
const statement = this.context.parents.get(declarator);
|
|
4610
4634
|
if (statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1) {
|
|
4611
|
-
this.reason =
|
|
4635
|
+
this.reason = `${kind}-declaration`;
|
|
4612
4636
|
return;
|
|
4613
4637
|
}
|
|
4614
4638
|
const calls = [];
|
|
4615
4639
|
for (const reference of binding.references) {
|
|
4616
4640
|
const call = this.context.parents.get(reference);
|
|
4617
4641
|
if (call?.type !== "CallExpression" || call.callee !== reference || call.arguments.length > 0 || call.start < declarator.end) {
|
|
4618
|
-
this.reason =
|
|
4642
|
+
this.reason = `${kind}-constant`;
|
|
4619
4643
|
return;
|
|
4620
4644
|
}
|
|
4621
4645
|
calls.push(call);
|
|
@@ -4624,8 +4648,10 @@ var MemoInliner = class {
|
|
|
4624
4648
|
const parent = this.context.parents.get(call);
|
|
4625
4649
|
const bare = parent?.type === "JSXExpressionContainer" || parent?.type === "VariableDeclarator" || parent?.type === "ReturnStatement" || parent?.type === "ArrayExpression" || parent?.type === "Property" || parent?.type === "CallExpression" && parent.callee !== call;
|
|
4626
4650
|
this.context.s.overwrite(call.start, call.end, bare ? text : `(${text})`);
|
|
4651
|
+
this.edited.push([call.start, call.end]);
|
|
4627
4652
|
}
|
|
4628
4653
|
this.context.s.remove(statement.start, statement.end);
|
|
4654
|
+
this.edited.push([statement.start, statement.end]);
|
|
4629
4655
|
this.changed = true;
|
|
4630
4656
|
return true;
|
|
4631
4657
|
}
|
|
@@ -4776,18 +4802,91 @@ var MemoInliner = class {
|
|
|
4776
4802
|
this.reason = "memo-scope";
|
|
4777
4803
|
return;
|
|
4778
4804
|
}
|
|
4779
|
-
if (kind === "children" && container)
|
|
4780
|
-
|
|
4805
|
+
if (kind === "children" && container) {
|
|
4806
|
+
this.context.s.overwrite(container.start, container.end, getText(this.context, expression));
|
|
4807
|
+
this.edited.push([container.start, container.end]);
|
|
4808
|
+
} else {
|
|
4809
|
+
this.context.s.overwrite(call.start, call.end, getText(this.context, expression));
|
|
4810
|
+
this.edited.push([call.start, call.end]);
|
|
4811
|
+
}
|
|
4781
4812
|
this.context.s.remove(statement.start, statement.end);
|
|
4813
|
+
this.edited.push([statement.start, statement.end]);
|
|
4782
4814
|
this.changed = true;
|
|
4783
4815
|
return true;
|
|
4784
4816
|
}
|
|
4817
|
+
/**
|
|
4818
|
+
* The literal a signal always holds: `const [get] = createSignal(literal)`,
|
|
4819
|
+
* or `const [get, set] = ...` with nothing referencing `set`. Options can
|
|
4820
|
+
* change what a read does, such as `unobserved`, so a signal with options
|
|
4821
|
+
* stays.
|
|
4822
|
+
*/
|
|
4823
|
+
signalConstant(declarator, binding) {
|
|
4824
|
+
const pattern = declarator.id;
|
|
4825
|
+
const init = declarator.init ? unwrap(declarator.init) : void 0;
|
|
4826
|
+
if (pattern.type !== "ArrayPattern" || pattern.elements.at(0) !== binding.identifier || pattern.elements.length > 2 || init?.type !== "CallExpression" || init.callee.type !== "Identifier" || init.arguments.length !== 1) return;
|
|
4827
|
+
const callee = this.context.scopes.references.get(init.callee);
|
|
4828
|
+
if (!callee || solidExport(callee, this.context.options) !== "createSignal") return;
|
|
4829
|
+
const value = init.arguments.at(0);
|
|
4830
|
+
if (!value || value.type === "SpreadElement") return;
|
|
4831
|
+
const statement = this.context.parents.get(declarator);
|
|
4832
|
+
if (statement && this.context.parents.get(statement)?.type === "ExportNamedDeclaration") return;
|
|
4833
|
+
const setter = pattern.elements.at(1);
|
|
4834
|
+
if (setter) {
|
|
4835
|
+
const setterBinding = setter.type === "Identifier" ? binding.scope.bindings.get(setter.name) : void 0;
|
|
4836
|
+
if (setterBinding?.identifier !== setter || setterBinding.references.length > 0) return;
|
|
4837
|
+
}
|
|
4838
|
+
return constantText(this.context, value);
|
|
4839
|
+
}
|
|
4840
|
+
tryInlineSignal(declarator, binding) {
|
|
4841
|
+
const text = this.signalConstant(declarator, binding);
|
|
4842
|
+
if (text === void 0) return;
|
|
4843
|
+
this.reason = "";
|
|
4844
|
+
if (this.inlineConstant(declarator, binding, text, "signal") === void 0) recordSkip(this.context, "memos", this.reason, binding.name);
|
|
4845
|
+
}
|
|
4846
|
+
/**
|
|
4847
|
+
* Replaces every call of `const name = () => expression` with the
|
|
4848
|
+
* expression. Each reference has to be a call with no arguments after the
|
|
4849
|
+
* declaration, and every name in the body has to mean the same at the call.
|
|
4850
|
+
*/
|
|
4851
|
+
tryInlineArrow(declarator, binding) {
|
|
4852
|
+
const fn = declarator.init ? unwrap(declarator.init) : void 0;
|
|
4853
|
+
if (fn?.type !== "ArrowFunctionExpression" || fn.async || fn.params.length > 0) return;
|
|
4854
|
+
const body = returnedExpression(fn);
|
|
4855
|
+
const statement = this.context.parents.get(declarator);
|
|
4856
|
+
if (!body || statement?.type !== "VariableDeclaration" || statement.declarations.length !== 1 || this.context.parents.get(statement)?.type === "ExportNamedDeclaration") return;
|
|
4857
|
+
const calls = [];
|
|
4858
|
+
for (const reference of binding.references) {
|
|
4859
|
+
const call = this.context.parents.get(reference);
|
|
4860
|
+
if (call?.type !== "CallExpression" || call.callee !== reference || call.optional || call.arguments.length > 0 || call.start < declarator.end) return;
|
|
4861
|
+
const parent = this.context.parents.get(call);
|
|
4862
|
+
if (parent?.type === "CallExpression" && parent.callee === call || parent?.type === "TaggedTemplateExpression" && parent.tag === call || parent?.type === "UnaryExpression" && parent.operator === "delete") return;
|
|
4863
|
+
calls.push(call);
|
|
4864
|
+
}
|
|
4865
|
+
if (calls.length === 0) return;
|
|
4866
|
+
for (const [start, end] of this.edited) if (start < statement.end && end > statement.start) return;
|
|
4867
|
+
const text = getText(this.context, body);
|
|
4868
|
+
const length = compactLength(text);
|
|
4869
|
+
if ((calls.length - 1) * length > calls.length * 3 + 13) return;
|
|
4870
|
+
if (usesFunctionContext(this.context, fn)) return;
|
|
4871
|
+
for (const call of calls) if (!this.resolvesSameAt(body, call, binding)) return;
|
|
4872
|
+
const bare = isPrimary(body);
|
|
4873
|
+
for (const call of calls) {
|
|
4874
|
+
const inContainer = this.context.parents.get(call)?.type === "JSXExpressionContainer" && body.type !== "SequenceExpression";
|
|
4875
|
+
this.context.s.overwrite(call.start, call.end, bare || inContainer ? text : `(${text})`);
|
|
4876
|
+
this.edited.push([call.start, call.end]);
|
|
4877
|
+
}
|
|
4878
|
+
this.context.s.remove(statement.start, statement.end);
|
|
4879
|
+
this.edited.push([statement.start, statement.end]);
|
|
4880
|
+
this.changed = true;
|
|
4881
|
+
}
|
|
4785
4882
|
run() {
|
|
4786
|
-
for (const scope of new Set(this.context.scopes.scopes.values())) {
|
|
4787
|
-
|
|
4788
|
-
|
|
4789
|
-
|
|
4790
|
-
|
|
4883
|
+
for (const scope of new Set(this.context.scopes.scopes.values())) for (const binding of scope.bindings.values()) {
|
|
4884
|
+
const { declaration } = binding;
|
|
4885
|
+
if (binding.kind !== "const" || binding.mutated || declaration.type !== "VariableDeclarator") continue;
|
|
4886
|
+
if (declaration.id.type === "ArrayPattern") this.tryInlineSignal(declaration, binding);
|
|
4887
|
+
else if (declaration.id === binding.identifier) {
|
|
4888
|
+
this.tryInlineArrow(declaration, binding);
|
|
4889
|
+
if (scope.parent !== void 0) this.tryInline(declaration, binding);
|
|
4791
4890
|
}
|
|
4792
4891
|
}
|
|
4793
4892
|
}
|
|
@@ -5237,4 +5336,4 @@ function readModuleConstants(code, options = {}) {
|
|
|
5237
5336
|
//#endregion
|
|
5238
5337
|
export { lookup as a, forEachChild as c, unwrap as d, analyzeScopes as i, isIntrinsicTag as l, readModuleConstants as n, scopeAt as o, CONTEXT_SAFE_PRIMITIVES as r, collectParents as s, compile as t, parse as u };
|
|
5239
5338
|
|
|
5240
|
-
//# sourceMappingURL=compile-
|
|
5339
|
+
//# sourceMappingURL=compile-DnfP-Quf.mjs.map
|