Files
hakorune/docs/ideas/other/2025-08-26-gc-switchable-language.md
Moe Charm 61800a37a7 feat: Phase 10 reorganization + GC switchable runtime + VM improvements
## 📚 Documentation Updates
- Phase 10 reorganized with comprehensive README
  - Cranelift JIT as main implementation
  - NEW: Phase 10.4 GC Switchable Runtime (world's first\!)
  - Phase 10.5 Self-hosting (parallel)
  - Application migration tests
- Phase 11 created for LLVM AOT research (deferred)
  - Moved phase10_aot_scaffolding.md → Phase 11
  - Moved phase_10_x_llvm_backend_skeleton.md → Phase 11
- Master roadmap updated with GC runtime feature
- Ideas: GC switchable language concept documented

## 🚀 VM Implementation Progress (by ChatGPT5)
- src/backend/vm.rs: Enhanced VM execution
- src/backend/vm_instructions.rs: Instruction improvements
- src/runtime/type_meta.rs: NEW - Type metadata system
- src/boxes/buffer/mod.rs: Buffer optimizations
- src/runtime/mod.rs & plugin_ffi_common.rs: Runtime enhancements

## 🌟 Revolutionary Feature: GC Switchable Runtime
- Development mode: GC on (convenience)
- Production mode: GC off (performance)
- Technical feasibility confirmed by Codex GPT-5
- Implementation plan: After Cranelift JIT

## 📋 Phase 10 Structure
Phase 10.0: Cranelift JIT foundation
Phase 10.1-10.3: JIT implementation & optimization
Phase 10.4: GC Switchable Runtime ← NEW\!
Phase 10.5: Self-hosting (String/Array/Map in Nyash)
Phase 10.9: Application migration tests

🤖 ChatGPT5 says: Ready for Phase 10\! どきどきにゃ!
2025-08-27 01:03:55 +09:00

158 lines
3.9 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# GC切り替え可能言語 - 史上初の柔軟なメモリ管理
Status: Research
Created: 2025-08-26
Priority: Medium
Related: メモリ管理、参照カウント、開発効率
## 🌟 コンセプト
Nyashを**世界初のGC切り替え可能言語**にする革新的アイデア。開発時はGCオンで快適に、本番ではGCオフで高性能に。
## 🎯 基本アイデア
```nyash
// 開発時: メモリリークを気にせず開発
nyash --gc-mode=ref-counting app.nyash
// 本番: 最高性能で実行
nyash --gc-mode=explicit app.nyash
```
## 📊 メモリ管理モード
### 1. Explicit Modeデフォルト
- 現在のNyashの動作
- スコープ抜けたら即fini()
- 予測可能な性能
- リアルタイムアプリ向け
### 2. Reference Counting Mode新規
- 参照カウントが0になったらfini()
- share_box()で参照カウント++
- 循環参照はweak参照で解決
- 一般アプリ向け
## 🔧 実装戦略
### インタープリター(簡単)
```rust
// 実行時のモード切り替えで対応
match memory_mode {
RefCounting => {
// 代入時に参照カウント操作
old_value.dec_ref();
new_value.inc_ref();
}
Explicit => {
// 現在の動作
}
}
```
### MIR中程度
```rust
// 新しい抽象命令を追加
enum MirInstruction {
Acquire(temp_id), // 値の取得GCならref++
Release(temp_id), // 値の解放GCならref--/fini
}
// MIRは同じ、実行時に挙動を変える
```
### VMやや複雑
- Acquire/Release命令の実装
- モードフラグによる分岐
- 参照カウント0検出時のfini呼び出し
### Cranelift JIT複雑
```rust
// モード別のコード生成
if gc_mode == Explicit {
// Acquire/Release命令をスキップ
// 直接fini呼び出し
} else {
// 参照カウント操作のインライン展開
// 条件付きfini呼び出し
}
```
## 🚀 実装フェーズ
### Phase 1: 基礎実装1-2週間
- [ ] BoxBaseに参照カウント追加
- [ ] インタープリターでのモード切り替え
- [ ] 基本的なテスト
### Phase 2: MIR/VM対応1ヶ月
- [ ] Acquire/Release命令の設計
- [ ] MIRビルダーの対応
- [ ] VM実行時の最適化
### Phase 3: JIT最適化Cranelift後
- [ ] モード別コード生成
- [ ] デッドコード除去
- [ ] インライン最適化
## 💡 革新的な使い方
### 開発フロー
```bash
# 1. 開発中: GCオンで快適開発
nyash --gc-mode=ref-counting --detect-leaks dev.nyash
# 2. テスト: メモリリーク検出
nyash --gc-mode=ref-counting --memory-report test.nyash
# 3. 本番: GCオフで最高性能
nyash --gc-mode=explicit --optimize prod.nyash
```
### ハイブリッドモード
```nyash
// ファイル単位での制御
// @gc-mode: explicit
box RealtimeAudioProcessor { }
// @gc-mode: ref-counting
box UIController { }
```
## 🤔 技術的課題
### 1. 統一的なMIR表現
- GCモードに依存しないMIR設計
- 実行時の最適化余地を残す
### 2. JITコード生成の複雑化
- モード別の最適化パス
- コードキャッシュの管理
### 3. デバッグ情報の保持
- どのモードで実行されているか
- 参照カウントの可視化
## 🎉 期待される効果
1. **開発効率**: GCありで快適開発
2. **実行性能**: GCなしで最高速
3. **教育価値**: メモリ管理の学習に最適
4. **柔軟性**: アプリケーションに応じた選択
## 📚 参考資料
- Rust: 所有権システム(コンパイル時)
- Swift: ARC自動参照カウント
- Go/Java: 強制GC
- C/C++: 手動管理
## 🔮 将来の拡張
- Mark & Sweep GCモードの追加
- 世代別GC
- リージョンベースメモリ管理
- プロファイルベース自動選択
## 実装タイミング
- Cranelift JIT実装後が理想的
- まずはインタープリターで実験
- 実用性を確認してから本格実装