microts: AOT inlines every child component instance into each ancestor and also emits an unreachable standalone view per component

Aperta
#757 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
12/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust, typescript

Direzione di ricerca

Start in microts/compiler/aot-codegen.ts: read Codegen.expand (lines 354–366) to see why non-factory children are expanded into each parent, then the emission loop at line 1113 that walks program.components. Reproduce with the pocket.ts compile --aot command and grep -c '"zero"' gen/app.rs to confirm the 7 copies, and inspect how factory components stay as mounted child views. Done means unreachable standalone views are no longer emitted and per-component types are shared across instances, dropping the grep count and generated size.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

bug

Version

PocketJS main (557ad61e). macOS 27.2 arm64, Bun 1.4.2.

Repro

pocket.host.json, pocket.host.config.ts (aot: true, model: "compiled") and main.tsx as in #720.

// app.ts
import { createSignal } from "solid-js";
import type { i32 } from "@pocketjs/framework/solid/std";
export const [count, setCount] = createSignal<i32>(0);
// Leaf.tsx
import { Show } from "solid-js";
import { Text, View } from "@pocketjs/framework/solid/components";
import type { i32 } from "@pocketjs/framework/solid/std";

export default function Leaf(props: { value: i32 }) {
  return (
    <View class="w-[60] h-[20]">
      <Show when={props.value === 0}><Text class="text-xs text-slate-900">zero</Text></Show>
      <Show when={props.value === 1}><Text class="text-xs text-slate-900">one</Text></Show>
      <Show when={props.value === 2}><Text class="text-xs text-slate-900">two</Text></Show>
    </View>
  );
}
// Middle.tsx
import { View } from "@pocketjs/framework/solid/components";
import type { i32 } from "@pocketjs/framework/solid/std";
import Leaf from "./Leaf.tsx";

export default function Middle(props: { value: i32 }) {
  return (
    <View class="flex-col">
      <Leaf value={props.value} />
      <Leaf value={props.value + 1} />
    </View>
  );
}
// app.tsx
import { View } from "@pocketjs/framework/solid/components";
import { count, setCount } from "./app";
import Middle from "./Middle.tsx";

export default function App() {
  return (
    <View class="w-full h-full p-4 bg-slate-100" focusable onPress={() => setCount(count() + 1)}>
      <Middle value={count()} />
      <Middle value={count() + 2} />
    </View>
  );
}
bun $POCKETJS/tools/pocket.ts compile --config pocket.host.config.ts --aot --out gen
grep -c '"zero"' gen/app.rs

Actual

gen/app.rs holds the Leaf subtree 7 times: "zero" occurs 7 times. One copy is the standalone LeafView, two are inlined into the standalone MiddleView, and four are inlined into AppView.

Prefix Structs
App (AppBlock, AppNode, …) 54
Middle 27
Leaf 13

AppView's code refers to no Leaf* or Middle* type. The standalone LeafView and MiddleView, with their blocks and nodes, are not reachable from the root, but rustc still parses, type-checks and compiles them.

The size grows with the product of the instance counts along each path, plus one standalone copy per ancestor. In an application whose views are about 4,000 lines of TSX, where a 12-branch glyph component is used in ten colours inside a watch-face component inside the root, app.rs is 100 MB (2.8 million lines, 55,577 impl items):

Generated prefix Lines
root component 1,155,193
watch-face component (standalone, unreachable) 581,466
glyph component (standalone, unreachable) 322,090
others 757,853

rustc (opt-level = "s", Xtensa target) takes 2 minutes and 5.1 GB of RSS for that crate on a 14-core host. On a 2-core, 7 GB CI runner it ran for 9.5 minutes and was killed by the OOM killer (SIGKILL).

Expected

  • pocket compile --aot emits the views reachable from the root component: without a component that is only ever inlined, its standalone view is dead code.
  • Instances of one component share their generated types (one block/node set per component, mounted per instance), so the generated size is the sum of the component sizes instead of the product of the instance counts along each path.

Cause

Codegen.expand (microts/compiler/aot-codegen.ts:354–366) expands the node tree of every non-factory child component into its parent (this.expand(component.nodes, …)); a factory component is kept as a child view. The emission loop (aot-codegen.ts:1113) generates a view for every component in program.components, whether or not anything mounts it.

Workaround

None in the view source: giving a component a model factory makes it a mounted child view, but changes its model ownership. Splitting the views does not help, because every instance is still expanded.

Lingua principale
TypeScript
Stelle
1.6k
Fork
88
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Preparare l'ambiente

Non abbiamo ancora controllato i file di configurazione di questo progetto. Parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di pocket-stack/pocketjs

Tutte le issue di pocket-stack/pocketjs

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.