RFC-008: Runtime Concurrency Model and Scheduler Decoupling Design
⚠️ Alignment Note: This document has been aligned with RFC-024 New Concurrency Model. The former whole-program DAG analysis,
@block/@eagerannotations, and L1/L2/L3 tier model have been replaced by thespawn {}block parallelism primitive. DAG analysis now applies only insidespawn {}blocks.
References:
Summary
This document defines key design aspects of the Runtime architecture:
- Three-tier runtime architecture: Embedded (immediate execution) → Standard (spawn + DAG scheduling) → Full (work stealing)
- Compilation/runtime separation: The compilation stage is identical; only the runtime execution method differs
- Dual-backend model: VM (development and debugging) and LLVM AOT (production release), with identical behavior
- Scheduler = static library: During AOT compilation, the scheduler is linked into the exe, approximately 200-500KB, with no GC
- Synchronization is merely a special case of scheduling: num_workers=1 means synchronous mode
Key Clarification: This Is Not Java
Java: .java → .class → JVM(解释/JIT + GC) ← 永远需要虚拟机
YaoXiang 开发: .yx → IR → VM 执行(快速迭代、步进调试)
YaoXiang 生产: .yx → IR → LLVM → 原生 exe(调度器链接进去)
VM 是开发工具,不是运行时本质。跟 Go 的 go run vs go build 一样。
最终 exe = 你的原生代码 + 调度器静态库 + 反射元数据。没有解释器,没有 JIT,没有 GC。Motivation
Core Contradiction
| Contradiction | Description |
|---|---|
| Transparency vs. control | spawn blocks provide explicit concurrency control, while ordinary code executes sequentially |
| Core vs. optional | spawn is the core parallel primitive; WorkStealing is an advanced feature when num_workers>1 |
| Single-threaded vs. concurrent | In single-threaded mode, concurrency appears asynchronous; synchronization is merely a special case of scheduling |
Proposal
1. Three-Tier Runtime Architecture
┌──────────────────────────────────────────────────────────────────┐
│ 编译阶段(所有模式相同) │
│ │
│ Source Code → Lexer → Parser → TypeCheck → Codegen → IR │
│ │
│ ⚠️ 同一套语法解析、类型检查、代码生成、IR 输出 │
└──────────────────────────────────────────────────────────────────┘
│
┌────────────────────┼────────────────────┐
▼ ▼ ▼
┌──────────────────┐ ┌───────────────┐ ┌──────────────────┐
│ 🟢 Embedded │ │ 🔵 Standard │ │ 🟣 Full │
│ 即时执行器 │ │ spawn + DAG │ │ Full 调度器 │
│ 同步执行 │ │ spawn 块内并发 │ │ 并行优化 │
│ 无 spawn 支持 │ │ 自动并发 │ │ 工作窃取 │
└──────────────────┘ └───────────────┘ └──────────────────┘| Stage | Embedded | Standard | Full |
|---|---|---|---|
| Compilation | Same | Same | Same |
| Execution mode | Synchronous | Concurrency within spawn blocks | Parallel |
| Memory footprint | Low | Medium | High |
| Concurrency capabilities | None | Within spawn blocks | Within spawn blocks + parallel |
| spawn support | ❌ | ✅ | ✅ |
| DAG analysis | None | Within spawn blocks | Within spawn blocks |
| WorkStealer | None | None | ✅ |
Embedded Runtime: Targets WASM, game scripts, and rule engines. An immediate executor with no spawn support, offering high performance with low overhead.
Standard Runtime: Targets web services and data pipelines. Supports spawn {} blocks and performs DAG analysis and automatic concurrency within spawn blocks. num_workers=1 means single-threaded asynchronous execution.
Full Runtime: Targets scientific computing and large-scale parallelism. Standard + WorkStealer load balancing.
2. Scheduler Decoupling: Generics + Injection
The core principle is that the VM does not directly depend on a concrete scheduler and calls it through the generic parameter [S].
# Scheduler interface definition
Scheduler: Type = {
spawn: (Task) -> TaskId,
await: (TaskId) -> Result,
spawn_with_deps: (Task, List(TaskId)) -> TaskId,
await_all: (List(TaskId)) -> List(Result),
stats: () -> SchedulerStats,
}
# Single-threaded scheduler
SingleThreadScheduler: Scheduler = {
spawn: (task) => { task_queue.push(task); generate_task_id() },
await: (task_id) => { ... },
spawn_with_deps: (task, deps) => { ... },
await_all: (task_ids) => { ... },
stats: () => { queue_size: task_queue.len() },
}
# Multi-threaded scheduler
MultiThreadScheduler: Scheduler = {
spawn: (task) => { work_queue.push(task); generate_task_id() },
await: (task_id) => { wait_for_completion(task_id) },
spawn_with_deps: (task, deps) => { ... },
await_all: (task_ids) => { ... },
stats: () => { workers: get_worker_stats() },
}
# VM uses the scheduler through generics
create_vm: [S: Scheduler](scheduler: S) -> VM = (scheduler) => {
VM(scheduler: scheduler, memory: create_memory(), dag: create_dag())
}Key points:
- Compile-time polymorphism with zero runtime overhead
- No Trait objects needed
- The generic type constraint
[S: Scheduler]has already been defined in RFC-011
3. Synchronization = A Special Case of Scheduling
❌ 误解:禁用调度器
✅ 正确:使用单 worker 的调度器
num_workers = 1 → 单线程异步调度
num_workers > 1 → 多线程并行调度
同一个调度器接口,只是配置不同。消除特殊情况。4. The Role of DAGs
Important Change: DAG analysis no longer applies to the whole program and is performed only inside
spawn {}blocks. Ordinary code (outside spawn blocks) executes sequentially and does not require DAG analysis.
| Tier | spawn support | DAG analysis scope | Description |
|---|---|---|---|
| Core Runtime | ✅ | Within spawn blocks | Concurrency core |
| Standard Runtime | ✅ | Within spawn blocks | spawn + DAG scheduling |
| Embedded Runtime | ❌ | None | Immediate execution, no concurrency |
5. Bottom-Up Execution Model (Within spawn Blocks)
Important Change: Bottom-up DAG analysis is now performed only inside
spawn {}blocks and is no longer applied to the whole program.
用户代码(spawn 块内并发):
(a, b) = spawn {
fetch(url0),
fetch(url1)
}
print(a)
编译时分析(spawn 块内自底向上):
fetch(url0) 和 fetch(url1) 无相互依赖 → 可并行执行
spawn 块外的 print(a) → 顺序执行,等待 spawn 完成
运行时调度(spawn 块内从叶子开始):
fetch(url0) ┐
├→ 并行执行
fetch(url1) ┘
print(a) ← spawn 块外,顺序执行Key points:
- Bottom-up dependency analysis is limited to the inside of
spawn {}blocks - Tasks without dependencies within a spawn block execute in parallel
- Code outside a spawn block executes sequentially and waits for the spawn block to finish
6. Compilation Model: Dual Backends + Statically Linked Runtime
6.1 Two Backends, One Behavior
┌─────────────────────┐
│ 编译前端(统一) │
│ Lexer → Parser │
│ → TypeCheck │
│ → spawn 块内 DAG 分析│
│ → 逃逸分析 │
│ → 环检测 │
└──────────┬──────────┘
│
┌────────────┴────────────┐
▼ ▼
┌───────────────────┐ ┌───────────────────┐
│ VM 后端(开发) │ │ LLVM 后端(生产) │
│ │ │ │
│ 生成 IR/字节码 │ │ 生成原生代码 │
│ VM 解释执行 │ │ 链接运行时静态库 │
│ 支持步进调试 │ │ 输出 .exe │
│ 快速迭代 │ │ 零解释开销 │
└───────────────────┘ └───────────────────┘
│ │
▼ ▼
行为完全一致行为完全一致VM backend: Used during development. Modify code → run immediately → step-through debugging → rapid iteration. Its behavior exactly matches that of the final exe.
LLVM backend: Used for release. AOT-compiles to native code, with the scheduler linked in as a static library. No interpreter, no JIT.
6.2 Scheduler = Static Library, Not a Virtual Machine
最终 exe 的内部结构:
┌────────────────────────────────────────────┐
│ 你的代码(原生机器码) │
│ ├── 编译期已确定的 DAG 执行计划 │
│ ├── 内联的 Move/ref/clone 操作 │
│ └── RAII 释放代码 │
├────────────────────────────────────────────┤
│ 运行时静态库(~200-500KB) │
│ ├── 线程池(固定大小 = num_workers) │
│ ├── 事件循环(libuv / io_uring) │
│ ├── 工作窃取队列(仅 Full Runtime) │
│ ├── 内存分配器(jemalloc / mimalloc) │
│ └── 反射元数据(按需加载,不常驻内存) │
├────────────────────────────────────────────┤
│ 没有: │
│ ❌ 字节码解释器 │
│ ❌ JIT 编译器 │
│ ❌ GC │
│ ❌ 虚拟机 │
└────────────────────────────────────────────┘Comparison:
| Language | Java | Go | YaoXiang |
|---|---|---|---|
| Build output | Bytecode | Native code | Native code |
| Execution | JVM interpretation/JIT | Direct execution | Direct execution |
| Runtime size | ~200MB (JVM) | ~1-2MB (including GC) | ~200-500KB (no GC) |
| Memory management | GC | GC | RAII (deterministic) |
| Reflection | Resident in memory | Resident in memory | Stored in the exe, loaded on demand |
6.3 Why Scheduler Performance Is Constant
Key insight: Most of the work is completed at compile time; the runtime only performs the "execution".
编译期(一次性,不进入运行时):
├── 分析 spawn 块内 DAG:谁依赖谁
├── 拓扑排序:确定 spawn 块内执行顺序
├── 识别可并行任务:spawn 块内无依赖的子树
├── 逃逸分析:ref → Rc 还是 Arc
├── 环检测:自动降级 Weak 或报错
└── 内联:小函数直接展开
运行时(每次执行,数据结构固定):
├── 按编译期确定的 spawn 块 DAG 顺序分发任务到线程池
├── 遇到 I/O → 挂起当前任务,事件循环接管
├── 任务就绪 → 放回就绪队列
└── 就这些。The scheduler itself is a fixed-size data structure: a thread pool, event loop, and work queues. There is no dynamic growth, no adaptive re-optimization, and no GC scanning. Its behavior is completely predictable.
Compilation has already determined what to schedule within spawn blocks, so the runtime only performs the "execution". This differs from tokio—tokio dynamically constructs the Future chain at runtime. YaoXiang's DAG is static and limited to within spawn blocks.
6.4 Reflection: Stored but Not Resident
Reflection metadata is generated at compile time and stored in a separate section of the exe. It is not loaded when the program starts. On the first reflection request, it is mmap'd into memory on demand. This is similar to:
exe 布局:
.text ← 你的代码
.rodata ← 常量
.reflect ← 反射元数据(类型信息、函数签名等)
mmap 按需加载,不访问不占内存Trade-off: The exe grows in size because it includes reflection data, but there is no memory overhead if the data is not accessed at runtime. The first access incurs a loading delay similar to JIT warm-up, while subsequent accesses have zero overhead.
src/
├── lib.rs
├── main.rs
├── backends/ # Runtime backends
│ ├── common/ # Shared by all backends (values, heap, opcodes)
│ │ ├── allocator.rs
│ │ ├── heap.rs
│ │ ├── opcode.rs
│ │ └── value.rs
│ ├── dev/ # REPL + debugger
│ │ ├── debugger.rs
│ │ ├── shell.rs
│ │ └── repl/
│ ├── interpreter/ # 🟢 Tree-walking interpreter (formerly Embedded/VM)
│ │ ├── ffi.rs
│ │ ├── frames.rs
│ │ ├── registers.rs
│ │ ├── runtime.rs
│ │ └── executor/
│ └── runtime/ # 🔵 Compiled VM runtime
│ ├── engine.rs
│ ├── facade.rs
│ └── task.rs
├── frontend/ # Compilation frontend (shared by all backends)
│ ├── compiler.rs
│ ├── config.rs
│ ├── pipeline.rs
│ ├── core/
│ │ ├── lexer/
│ │ ├── parser/
│ │ ├── typecheck/
│ │ │ ├── checker.rs
│ │ │ ├── spawn_placement.rs # ★ DAG/concurrency analysis within spawn blocks (formerly frontend/dag/)
│ │ │ ├── inference/
│ │ │ └── traits/
│ │ └── types/
│ ├── events/
│ ├── module/
│ └── pipeline/
├── middle/ # Middle end
│ ├── core/ # IR & bytecode
│ │ ├── bytecode.rs
│ │ ├── ir.rs # IR definitions (shared by VM and LLVM)
│ │ └── ir_gen.rs
│ └── passes/ # Compilation passes
│ ├── codegen/ # Code generation (formerly codegen/)
│ ├── lifetime/ # Lifetime/borrow analysis
│ └── mono/ # Monomorphization
├── lsp/ # Language Server
├── formatter/ # Source formatter
├── package/ # Package manager
├── std/ # Standard library
│ ├── concurrent.rs
│ ├── io.rs
│ ├── list.rs
│ ├── math.rs
│ ├── net.rs
│ ├── string.rs
│ └── weak.rs
└── util/ # Utilities
├── diagnostic/
├── i18n/
└── config/Directory Mapping (Old → New):
| Old directory | New location | Description |
|---|---|---|
frontend/dag/ | frontend/core/typecheck/spawn_placement.rs | DAG analysis within spawn blocks has been integrated into type checking |
codegen/ | middle/passes/codegen/ | Code generation has moved into a middle-end pass |
embedded/ | backends/interpreter/ | Tree-walking interpreter |
runtime/ | backends/runtime/ | Compiled VM runtime |
vm/ | backends/interpreter/ | Merged with embedded |
full/ | (Not implemented) | Full Runtime + work stealing, planned for a later version |
reflect/ | (Not implemented) | Reflection metadata, planned for a later version |
core/ | backends/common/ | Shared values/heap/opcodes |
Trade-offs
Advantages
- Clear layering: Embedded / Standard / Full tiers
- Compilation reuse: Frontend code is fully shared
- Generic decoupling: Compile-time polymorphism with zero overhead
- Consistency: Synchronization is merely a special case of scheduling
- Embedded-friendly: High performance + low memory + fast startup
Disadvantages
- Initial complexity: Scheduler interfaces and multiple runtime variants need to be defined
- Compile-time binding: The scheduler type is determined at compile time
Design Decision Record
| Decision Area | Decision | Date |
|---|---|---|
| Scheduler decoupling approach | Generics + injection | 2025-01-05 |
| Single-threaded mode | Synchronization is a special case of scheduling | 2025-01-05 |
| Asynchronous implementation | Natively supported by DAG | 2025-01-05 |
| WorkStealer | Advanced Full Runtime feature | 2025-01-05 |
| Embedded design | Immediate execution, no DAG scheduling | 2025-01-05 |
| Compilation stage | All runtimes share the same frontend | 2025-01-05 |
| Runtime tiering | Embedded / Standard / Full | 2025-01-05 |
| Type constraint | Defined in RFC-011 | 2025-01-25 |
| Dependency graph construction | Static dependency graph, determined at compile time | 2025-01-05 |
| Dual-backend model | VM (development/debugging) + LLVM AOT (production), identical behavior | 2026-05-11 |
| Scheduler form | Static library linked into the exe, ~200-500KB, no GC | 2026-05-11 |
| Reflection metadata | Compiled into a separate exe section, mmap loaded on demand | 2026-05-11 |
| Scheduler performance | DAG analysis completed at compile time; runtime only executes | 2026-05-11 |
| DAG scope alignment | DAG analysis limited to spawn blocks, aligned with RFC-024 | 2026-06-05 |
| Three-tier architecture update | Embedded has no spawn; Standard supports spawn | 2026-06-05 |
References
- Concurrency Model Specification (RFC-024)
- RFC-011: Generic Type System Design
- Rust async Runtime Design
- Go Scheduler Design
Lifecycle and Disposition
| Status | Location | Description |
|---|---|---|
| Draft | docs/design/rfc/ | Author draft |
| Under review | docs/design/rfc/ | Open community discussion |
| Accepted | docs/design/accepted/ | Formal design document |
| Rejected | docs/design/rfc/ | Retained in the RFC directory |
