File contents 性能优化与多后端适配
触发条件
用户关注性能、体积或多后端支持 时激活。
决策树
优化目标?
├── 减小 Wasm 体积
│ ├── release 模式 → moon build --target wasm-gc --release
│ ├── tree-shaking → 最小化 pub 导出
│ └── 内联标记 → #[inline] 热点函数
├── 减小 JS 输出体积
│ ├── 最小化导出 → priv 标记内部函数
│ ├── 惰性初始化 → 延迟加载重型数据
│ └── 避免泛型膨胀 → 特化关键路径
├── 提升运行时性能
│ ├── 算法优化 → 先分析复杂度
│ ├── 内存分配 → 减少不必要的拷贝
│ └── 基准测试定位瓶颈 → moon bench
├── 多后端支持
│ ├── Wasm-GC + JS 双目标 → 平台抽象 Trait
│ └── Native 特定功能 → 条件编译 target("native")
└── 发布 npm 包
└── JS 后端 + package.json + TypeScript 声明
执行步骤
Step 1: 基准测试(定位瓶颈)
moon bench # 运行基准测试
# 分析输出:哪些函数耗时最长
Step 2: 选择优化策略
场景
策略
预期效果
Wasm 体积大
--release + 最小导出
-30%~50%
JS 体积大
priv 标记 + 惰性初始化
类似 MoonBash 434KB gzip
运行时慢
算法优化 + 减少拷贝
取决于瓶颈
需要多平台
双目标 + 平台抽象 Trait
一次编写多端运行
Step 3: 后端特定优化
Wasm-GC : 参考 references/multi-backend.md Chapter 7-8
JS : 参考 MoonBash 的 434KB gzip 成果(references/real-world-examples.md)
Native : link-options + LTO
Step 4: 验证优化效果
# 对比优化前后
moon build --target <后端> --release
ls -la target/*/release/
# Wasm: 看 .wasm 文件大小
# JS: 看 .mjs gzip 大小
Step 5: 回归测试
moon test # 确保优化未破坏功能
moon bench # 确认性能确实提升
经验数据(来自真实项目)
项目
优化策略
结果
MoonBash
priv 导出 + 惰性初始化 + 内联
434 KB gzip (原 ~1.8MB)
moon-lottie
Wasm-GC + JS 双目标
同一套代码两个产物
mbtgraph
半存储优化(无向图 O(V+E))
空间节省 ~50%
详细知识
🔗 references/multi-backend.md — 多后端开发完整指南
🔗 references/real-world-examples.md — 真实项目优化案例
🔗 references/library-design.md — Builder/Converter 模式(影响性能的设计决策)
1 --- 2 name: 5-optimize 3 description: 性能优化与多后端适配 4 --- 5 6 # 性能优化与多后端适配 7 8 ## 触发条件 9 用户关注**性能、体积或多后端支持**时激活。 10 11 ## 决策树 12 13 ``` 14 优化目标? 15 ├── 减小 Wasm 体积 16 │ ├── release 模式 → moon build --target wasm-gc --release 17 │ ├── tree-shaking → 最小化 pub 导出 18 │ └── 内联标记 → #[inline] 热点函数 19 ├── 减小 JS 输出体积 20 │ ├── 最小化导出 → priv 标记内部函数 21 │ ├── 惰性初始化 → 延迟加载重型数据 22 │ └── 避免泛型膨胀 → 特化关键路径 23 ├── 提升运行时性能 24 │ ├── 算法优化 → 先分析复杂度 25 │ ├── 内存分配 → 减少不必要的拷贝 26 │ └── 基准测试定位瓶颈 → moon bench 27 ├── 多后端支持 28 │ ├── Wasm-GC + JS 双目标 → 平台抽象 Trait 29 │ └── Native 特定功能 → 条件编译 target("native") 30 └── 发布 npm 包 31 └── JS 后端 + package.json + TypeScript 声明 32 ``` 33 34 ## 执行步骤 35 36 ### Step 1: 基准测试(定位瓶颈) 37 ```bash 38 moon bench # 运行基准测试 39 # 分析输出:哪些函数耗时最长 40 ``` 41 42 ### Step 2: 选择优化策略 43 | 场景 | 策略 | 预期效果 | 44 |------|------|---------| 45 | Wasm 体积大 | `--release` + 最小导出 | -30%~50% | 46 | JS 体积大 | priv 标记 + 惰性初始化 | 类似 MoonBash 434KB gzip | 47 | 运行时慢 | 算法优化 + 减少拷贝 | 取决于瓶颈 | 48 | 需要多平台 | 双目标 + 平台抽象 Trait | 一次编写多端运行 | 49 50 ### Step 3: 后端特定优化 51 - **Wasm-GC**: 参考 `references/multi-backend.md` Chapter 7-8 52 - **JS**: 参考 MoonBash 的 434KB gzip 成果(`references/real-world-examples.md`) 53 - **Native**: link-options + LTO 54 55 ### Step 4: 验证优化效果 56 ```bash 57 # 对比优化前后 58 moon build --target <后端> --release 59 ls -la target/*/release/ 60 # Wasm: 看 .wasm 文件大小 61 # JS: 看 .mjs gzip 大小 62 ``` 63 64 ### Step 5: 回归测试 65 ```bash 66 moon test # 确保优化未破坏功能 67 moon bench # 确认性能确实提升 68 ``` 69 70 ## 经验数据(来自真实项目) 71 72 | 项目 | 优化策略 | 结果 | 73 |------|---------|------| 74 | MoonBash | priv 导出 + 惰性初始化 + 内联 | **434 KB** gzip (原 ~1.8MB) | 75 | moon-lottie | Wasm-GC + JS 双目标 | 同一套代码两个产物 | 76 | mbtgraph | 半存储优化(无向图 O(V+E)) | 空间节省 ~50% | 77 78 ## 详细知识 79 🔗 `references/multi-backend.md` — 多后端开发完整指南 80 🔗 `references/real-world-examples.md` — 真实项目优化案例 81 🔗 `references/library-design.md` — Builder/Converter 模式(影响性能的设计决策)
morning-start/agent-skills/tree/main/lang/moonbit/skills/5-optimize commit 676d87f664
Frequently asked questions How do I install the 5 Optimize skill? Run npx skillmds@latest add morning-start/5-optimize in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
What does the 5 Optimize skill do? 性能优化与多后端适配 It is listed under Coding & Dev Tools on SkillMD.
Is 5 Optimize safe to use? This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
Which AI agents work with 5 Optimize? This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Is 5 Optimize free to use? Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Who published 5 Optimize? morning-start (@morning-start) published this skill. Their other Agent Skills are listed on their SkillMD profile.