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

Đang mở
#757 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức phù hợp với người mới
12/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
rust, typescript
Lĩnh vực
compilers, performance

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
TypeScript
Star
1.6k
Fork
88
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Chúng tôi chưa kiểm tra các tệp thiết lập môi trường của dự án này. Hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của pocket-stack/pocketjs

Tất cả issue của pocket-stack/pocketjs

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.