solid-optimizer 0.3.0 → 0.4.1

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/src/inline.ts CHANGED
@@ -9,7 +9,8 @@
9
9
  *
10
10
  * - It is declared once at the top level of the module, with a capitalized
11
11
  * name, as a function declaration or a `const` bound to a function.
12
- * - It takes no parameter, or one identifier that is only read as `props.name`.
12
+ * - It takes no parameter, or one identifier that is only read as `props.name`,
13
+ * or spread onto an element as `{...props}`.
13
14
  * - Its body is statements followed by one final `return` of JSX, or an arrow
14
15
  * function whose body is JSX.
15
16
  * - It does not use `this`, `arguments`, `new.target`, or `super`.
@@ -86,7 +87,7 @@ import {
86
87
  unwrap,
87
88
  } from './ast';
88
89
  import type { PassContext, ResolvedOptions } from './context';
89
- import { isConstantDeclaration, textOf } from './context';
90
+ import { isConstantDeclaration, recordSkip, solidExport, textOf } from './context';
90
91
  import type { Piece } from './jsx';
91
92
  import type { ClosedOptions, Provider } from './provider';
92
93
  import {
@@ -473,6 +474,29 @@ function attributesOf(element: JSXElement): Map<string, JSXAttribute> | undefine
473
474
  return attributes;
474
475
  }
475
476
 
477
+ /**
478
+ * Why `attributesOf` turns down a call site, for the report.
479
+ */
480
+ function callSiteReason(element: JSXElement): string {
481
+ const names = new Set<string>();
482
+ for (const attribute of element.openingElement.attributes) {
483
+ if (attribute.type !== 'JSXAttribute') {
484
+ return 'call-spread';
485
+ }
486
+ if (attribute.name.type !== 'JSXIdentifier') {
487
+ return 'call-namespace';
488
+ }
489
+ if (attribute.name.name === 'ref') {
490
+ return 'call-ref';
491
+ }
492
+ if (names.has(attribute.name.name)) {
493
+ break;
494
+ }
495
+ names.add(attribute.name.name);
496
+ }
497
+ return 'call-duplicate';
498
+ }
499
+
476
500
  function isAssignmentTarget(context: PassContext, node: Node): boolean {
477
501
  const parent = context.parents.get(node);
478
502
  if (!parent) {
@@ -517,6 +541,44 @@ class Inliner {
517
541
 
518
542
  constructor(private readonly context: PassContext) {}
519
543
 
544
+ /** Why the last component or call site was turned down, for the report. */
545
+ private reason = '';
546
+
547
+ /**
548
+ * Whether a binding holds a component made by Solid's `lazy`, for the
549
+ * report. In a chunk, `lazy` can be a local declaration under another
550
+ * name, so a call given a function with a dynamic import counts too.
551
+ */
552
+ private isLazy(binding: Binding): boolean {
553
+ const { declaration } = binding;
554
+ const init =
555
+ declaration.type === 'VariableDeclarator' && declaration.init
556
+ ? unwrap(declaration.init)
557
+ : undefined;
558
+ if (init?.type !== 'CallExpression' || init.callee.type !== 'Identifier') {
559
+ return false;
560
+ }
561
+ const callee = this.context.scopes.references.get(init.callee);
562
+ if (callee && solidExport(callee, this.context.options) === 'lazy') {
563
+ return true;
564
+ }
565
+ const loader = init.arguments.at(0);
566
+ if (!loader || !isFunctionNode(loader)) {
567
+ return false;
568
+ }
569
+ let imports = false;
570
+ const visit = (node: Node): void => {
571
+ if (node.type === 'ImportExpression') {
572
+ imports = true;
573
+ }
574
+ if (!imports) {
575
+ forEachChild(node, visit);
576
+ }
577
+ };
578
+ visit(loader);
579
+ return imports;
580
+ }
581
+
520
582
  // ---------------------------------------------------------------------------
521
583
  // Components
522
584
  // ---------------------------------------------------------------------------
@@ -546,15 +608,29 @@ class Inliner {
546
608
  }
547
609
 
548
610
  private analyzeComponent(binding: Binding): Component | undefined {
549
- if (!isCapitalized(binding.name) || binding.mutated) {
611
+ if (!isCapitalized(binding.name)) {
612
+ return undefined;
613
+ }
614
+ if (binding.mutated) {
615
+ this.reason = 'component-mutated';
550
616
  return undefined;
551
617
  }
552
618
  const fn = functionOf(binding, this.context.options);
553
- if (!fn || fn.async || fn.generator || !fn.body || fn.params.length > 1) {
619
+ if (!fn?.body) {
620
+ this.reason = this.isLazy(binding) ? 'lazy' : 'not-a-function';
621
+ return undefined;
622
+ }
623
+ if (fn.async || fn.generator) {
624
+ this.reason = 'component-async';
625
+ return undefined;
626
+ }
627
+ if (fn.params.length > 1) {
628
+ this.reason = 'component-params';
554
629
  return undefined;
555
630
  }
556
631
  const scope = this.scopeOf(fn);
557
632
  if (!scope) {
633
+ this.reason = 'not-a-function';
558
634
  return undefined;
559
635
  }
560
636
 
@@ -565,6 +641,7 @@ class Inliner {
565
641
  const body = fn.body.body;
566
642
  const last = body.at(-1);
567
643
  if (last?.type !== 'ReturnStatement' || !last.argument) {
644
+ this.reason = 'component-body';
568
645
  return undefined;
569
646
  }
570
647
  const rest: Statement[] = [];
@@ -573,7 +650,12 @@ class Inliner {
573
650
  // TypeScript's AST gives every expression statement a `directive`
574
651
  // key, which is `null` unless the statement is a directive.
575
652
  const directive = 'directive' in statement ? statement.directive : null;
576
- if (typeof directive === 'string' || containsReturn(statement)) {
653
+ if (typeof directive === 'string') {
654
+ this.reason = 'component-directive';
655
+ return undefined;
656
+ }
657
+ if (containsReturn(statement)) {
658
+ this.reason = 'component-return';
577
659
  return undefined;
578
660
  }
579
661
  rest.push(statement);
@@ -584,9 +666,11 @@ class Inliner {
584
666
  root = unwrap(fn.body);
585
667
  }
586
668
  if (root.type !== 'JSXElement' && root.type !== 'JSXFragment') {
669
+ this.reason = 'component-body';
587
670
  return undefined;
588
671
  }
589
672
  if (usesFunctionContext(this.context, fn.body)) {
673
+ this.reason = 'component-this';
590
674
  return undefined;
591
675
  }
592
676
 
@@ -595,15 +679,18 @@ class Inliner {
595
679
  let paramBinding: Binding | undefined;
596
680
  if (param) {
597
681
  if (param.type !== 'Identifier') {
682
+ this.reason = 'component-params';
598
683
  return undefined;
599
684
  }
600
685
  paramBinding = scope.bindings.get(param.name);
601
686
  if (!paramBinding || paramBinding.mutated) {
687
+ this.reason = 'props-use';
602
688
  return undefined;
603
689
  }
604
690
  }
605
691
  const views = this.analyzeViews(fn, scope, statements, paramBinding);
606
692
  if (!views) {
693
+ this.reason = 'view-use';
607
694
  return undefined;
608
695
  }
609
696
  const props: PropRead[] = [];
@@ -613,6 +700,7 @@ class Inliner {
613
700
  }
614
701
  const read = this.propRead(fn, reference);
615
702
  if (!read) {
703
+ this.reason = 'props-use';
616
704
  return undefined;
617
705
  }
618
706
  props.push(read);
@@ -641,6 +729,7 @@ class Inliner {
641
729
  }
642
730
  // A component that renders itself cannot be inlined into itself.
643
731
  if (target === binding) {
732
+ this.reason = 'component-recursive';
644
733
  return undefined;
645
734
  }
646
735
  if (target && isWithinScope(target.scope, scope)) {
@@ -780,24 +869,32 @@ class Inliner {
780
869
 
781
870
  const reads: ViewRead[] = [];
782
871
  const spreads: ViewSpread[] = [];
872
+ // `{...props}` spreads every prop, like a view of the props with nothing hidden.
873
+ const whole: View = { sources: [{ defaults: undefined, hidden: new Set(), only: undefined }] };
874
+ for (const reference of param?.references ?? []) {
875
+ const spread = this.spreadOnElement(reference);
876
+ if (spread) {
877
+ args.add(reference);
878
+ spreads.push({
879
+ attribute: spread.attribute,
880
+ element: spread.element,
881
+ view: whole,
882
+ repeated: this.isRepeated(fn, spread.attribute),
883
+ });
884
+ }
885
+ }
783
886
  for (const [binding, view] of views) {
784
887
  for (const reference of binding.references) {
785
888
  if (args.has(reference)) {
786
889
  continue;
787
890
  }
788
- const parent = this.context.parents.get(reference);
789
- const opening = parent ? this.context.parents.get(parent) : undefined;
790
- const element = opening ? this.context.parents.get(opening) : undefined;
791
- if (
792
- parent?.type === 'JSXSpreadAttribute' &&
793
- opening?.type === 'JSXOpeningElement' &&
794
- element?.type === 'JSXElement'
795
- ) {
891
+ const spread = this.spreadOnElement(reference);
892
+ if (spread) {
796
893
  spreads.push({
797
- attribute: parent,
798
- element,
894
+ attribute: spread.attribute,
895
+ element: spread.element,
799
896
  view,
800
- repeated: this.isRepeated(fn, parent),
897
+ repeated: this.isRepeated(fn, spread.attribute),
801
898
  });
802
899
  continue;
803
900
  }
@@ -811,6 +908,20 @@ class Inliner {
811
908
  return { bindings: new Set(views.keys()), arguments: args, declarations, reads, spreads };
812
909
  }
813
910
 
911
+ /** The spread a reference is the whole argument of, and the element it is on. */
912
+ private spreadOnElement(
913
+ reference: Node,
914
+ ): { attribute: JSXSpreadAttribute; element: JSXElement } | undefined {
915
+ const parent = this.context.parents.get(reference);
916
+ const opening = parent ? this.context.parents.get(parent) : undefined;
917
+ const element = opening ? this.context.parents.get(opening) : undefined;
918
+ return parent?.type === 'JSXSpreadAttribute' &&
919
+ opening?.type === 'JSXOpeningElement' &&
920
+ element?.type === 'JSXElement'
921
+ ? { attribute: parent, element }
922
+ : undefined;
923
+ }
924
+
814
925
  /**
815
926
  * The sources a view argument stands for: the props, a view, or a nested
816
927
  * `mergeProps()` call.
@@ -1195,19 +1306,23 @@ class Inliner {
1195
1306
  private prepare(component: Component, element: JSXElement): PreparedSite | undefined {
1196
1307
  const attributes = attributesOf(element);
1197
1308
  if (!attributes) {
1309
+ this.reason = callSiteReason(element);
1198
1310
  return undefined;
1199
1311
  }
1200
1312
  const hasChildren = element.children.some((child) => !isInsignificant(child));
1201
1313
  if (hasChildren && attributes.has('children')) {
1314
+ this.reason = 'call-children';
1202
1315
  return undefined;
1203
1316
  }
1204
1317
  for (const attribute of attributes.values()) {
1205
1318
  if (attribute.value && usesFunctionContext(this.context, attribute.value)) {
1319
+ this.reason = 'call-this';
1206
1320
  return undefined;
1207
1321
  }
1208
1322
  }
1209
1323
  for (const child of element.children) {
1210
1324
  if (usesFunctionContext(this.context, child)) {
1325
+ this.reason = 'call-this';
1211
1326
  return undefined;
1212
1327
  }
1213
1328
  }
@@ -1215,6 +1330,7 @@ class Inliner {
1215
1330
  // What each view read and spread resolves to at this call site.
1216
1331
  const views = this.resolveViews(component, element, attributes, hasChildren);
1217
1332
  if (!views) {
1333
+ this.reason = 'view-conflict';
1218
1334
  return undefined;
1219
1335
  }
1220
1336
 
@@ -1249,6 +1365,7 @@ class Inliner {
1249
1365
  value = this.propValue(attributes.get(name), reads, fresh, snapshots);
1250
1366
  }
1251
1367
  if (!value) {
1368
+ this.reason = 'prop-value';
1252
1369
  return undefined;
1253
1370
  }
1254
1371
  values.set(name, value);
@@ -1258,6 +1375,7 @@ class Inliner {
1258
1375
  const scope = scopeAt(this.context.scopes, this.context.parents, element);
1259
1376
  for (const reference of component.outer) {
1260
1377
  if (lookup(scope, reference.name) !== reference.binding) {
1378
+ this.reason = 'shadowed';
1261
1379
  return undefined;
1262
1380
  }
1263
1381
  }
@@ -1265,11 +1383,13 @@ class Inliner {
1265
1383
  const hoisted = component.statements.length > 0 || snapshots.length > 0;
1266
1384
  const host = hoisted ? this.hostOf(element) : undefined;
1267
1385
  if (hoisted && !host) {
1386
+ this.reason = 'no-host';
1268
1387
  return undefined;
1269
1388
  }
1270
1389
 
1271
1390
  const contextTexts = this.contextTexts(component, element);
1272
1391
  if (!contextTexts) {
1392
+ this.reason = 'context-value';
1273
1393
  return undefined;
1274
1394
  }
1275
1395
 
@@ -1909,13 +2029,20 @@ class Inliner {
1909
2029
 
1910
2030
  run(): void {
1911
2031
  const components: Component[] = [];
2032
+ const rejected = new Map<Binding, string>();
1912
2033
  for (const binding of this.context.scopes.root.bindings.values()) {
2034
+ this.reason = 'not-a-function';
1913
2035
  const component = this.analyzeComponent(binding);
1914
2036
  if (component) {
1915
2037
  components.push(component);
2038
+ } else {
2039
+ rejected.set(binding, this.reason);
1916
2040
  }
1917
2041
  }
2042
+ // Why each call site that stays a call was not inlined, for the report.
2043
+ const siteReasons = new Map<JSXElement, string>();
1918
2044
  if (components.length === 0) {
2045
+ this.report(rejected, siteReasons);
1919
2046
  return;
1920
2047
  }
1921
2048
 
@@ -1962,6 +2089,9 @@ class Inliner {
1962
2089
  inlined.set(site.component, elements);
1963
2090
  } else {
1964
2091
  otherUses.set(site.component, (otherUses.get(site.component) ?? 0) + 1);
2092
+ if (this.context.skips && !postponed) {
2093
+ siteReasons.set(site.element, this.siteReason(site, worthwhile));
2094
+ }
1965
2095
  }
1966
2096
  // A component holding a call that was inlined now, or postponed to the
1967
2097
  // next pass, waits too. A copy made now would copy the call as it is.
@@ -1975,6 +2105,7 @@ class Inliner {
1975
2105
  }
1976
2106
 
1977
2107
  this.writeHosts();
2108
+ this.report(rejected, siteReasons);
1978
2109
 
1979
2110
  // A component nothing uses anymore is removed. A copy made in this pass
1980
2111
  // comes from the original code, so a component used inside a copied
@@ -1993,6 +2124,84 @@ class Inliner {
1993
2124
  }
1994
2125
  }
1995
2126
 
2127
+ /**
2128
+ * Why a call site of a component was not inlined. A component that is
2129
+ * not worth inlining is only turned down for its size when the call site
2130
+ * could be inlined.
2131
+ */
2132
+ private siteReason(site: CallSite, worthwhile: ReadonlySet<Component>): string {
2133
+ if (worthwhile.has(site.component)) {
2134
+ return this.reason;
2135
+ }
2136
+ this.scratchNames = new Set(this.context.scopes.names);
2137
+ try {
2138
+ return this.prepare(site.component, site.element) ? 'size' : this.reason;
2139
+ } finally {
2140
+ this.scratchNames = undefined;
2141
+ }
2142
+ }
2143
+
2144
+ /**
2145
+ * Records each component call that stays a call, and why. Solid's
2146
+ * built-ins and context providers are left out. The provider pass
2147
+ * reports providers.
2148
+ */
2149
+ private report(
2150
+ rejected: ReadonlyMap<Binding, string>,
2151
+ siteReasons: ReadonlyMap<JSXElement, string>,
2152
+ ): void {
2153
+ if (!this.context.skips) {
2154
+ return;
2155
+ }
2156
+ const visit = (node: Node): void => {
2157
+ if (node.type === 'JSXElement') {
2158
+ const reason = this.callReason(node, rejected, siteReasons);
2159
+ if (reason) {
2160
+ recordSkip(this.context, 'inline', reason.reason, reason.name);
2161
+ }
2162
+ }
2163
+ forEachChild(node, visit);
2164
+ };
2165
+ visit(this.context.program);
2166
+ }
2167
+
2168
+ private callReason(
2169
+ element: JSXElement,
2170
+ rejected: ReadonlyMap<Binding, string>,
2171
+ siteReasons: ReadonlyMap<JSXElement, string>,
2172
+ ): { reason: string; name: string } | undefined {
2173
+ const tag = element.openingElement.name;
2174
+ // The provider pass reports providers.
2175
+ if (providerContext(this.context, tag)) {
2176
+ return undefined;
2177
+ }
2178
+ if (tag.type === 'JSXMemberExpression') {
2179
+ return { reason: 'member-tag', name: textOf(this.context, tag) };
2180
+ }
2181
+ if (tag.type !== 'JSXIdentifier' || !isCapitalized(tag.name)) {
2182
+ return undefined;
2183
+ }
2184
+ const { name } = tag;
2185
+ const binding = this.context.scopes.references.get(tag);
2186
+ const identity = binding ? solidExport(binding, this.context.options) : name;
2187
+ if (identity !== undefined && this.context.options.builtIns.has(identity)) {
2188
+ return undefined;
2189
+ }
2190
+ if (!binding) {
2191
+ return { reason: 'global', name };
2192
+ }
2193
+ if (binding.kind === 'import') {
2194
+ // A chunk imports other chunks through placeholders the bundler fills in later.
2195
+ const source = (binding.source ?? '').replaceAll(/!~\{\w+\}~/g, '*');
2196
+ return { reason: 'imported', name: `${name} (${source})` };
2197
+ }
2198
+ if (binding.scope.parent !== undefined) {
2199
+ return { reason: 'nested', name };
2200
+ }
2201
+ const reason = rejected.get(binding) ?? siteReasons.get(element);
2202
+ return reason === undefined ? undefined : { reason, name };
2203
+ }
2204
+
1996
2205
  /**
1997
2206
  * The whitespace before `position` on its line.
1998
2207
  */
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
- return alwaysNotifies || isFresh(expression) ? expression : undefined;
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 || binding.references.length !== 1) {
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
- return;
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
- return;
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 {