OPTIM-IDEAS-2 — Comprehensive Optimization Strategy (author-supplied, 2026-10-01)
Источник:
dev/fera/project-design/context/OPTIM-IDEAS-2.md(read-only; сгенерировано scripts/sync-docs.mjs)
OPTIM-IDEAS-2 — Comprehensive Optimization Strategy (author-supplied, 2026-10-01)
Section titled “OPTIM-IDEAS-2 — Comprehensive Optimization Strategy (author-supplied, 2026-10-01)”Status: входящий материал от автора, накопление до «стоп». Обсуждение потом.
1. Profiling & auditing first
Section titled “1. Profiling & auditing first”- Performance + memory profilers: Linux perf, FlameGraphs, tokio-console. Power via OS power counters / external meters.
- Identify: hot code paths, large allocations, energy-draw bursts.
- Hotspots to expect: JIT compilation, dynamic module loading, frequent synchronization.
- Target hot paths first: chat-actor message dispatch, vtable/pointer lookups — constantly invoked, must be lean. Inline small functions, cache results, reduce branching.
- Rare events (module swaps, plugin loads) may carry overhead but must be batched/deferred so they never stall the system.
2. Memory reclamation (EBR / QSBR / HP) — strategy
Section titled “2. Memory reclamation (EBR / QSBR / HP) — strategy”- EBR: threads run in epochs; removed objects → garbage pile tagged with epoch; reclaimed when all threads advance past that epoch. Low reader overhead; writer may wait for grace period. (crossbeam-epoch model.)
- QSBR: threads announce quiescent points (onQuiescentState()); the reclaimer waits until all threads reached quiescence before freeing. Reads are free; requires tuning safe-point placement — e.g. actor loop / task boundaries.
- Hazard pointers: readers publish the pointer they hold; writer scans before freeing. Predictable reclamation latency; overhead on reads.
- Consensus: QSBR/EBR as default (reads almost free), hazard pointers reserved for specific hotspots / critical modules where waiting for a grace period becomes unpredictable (stuck thread). HP on big/primary module loads; QSBR/EBR for the many small ones.
3. Runtime & plugin strategy
Section titled “3. Runtime & plugin strategy”- WASI backend / interpreter / AOT mode for plugins: less JIT → far lower energy draw. Benchmark JIT vs WASI to validate power reduction.
- Plugin linking via EBR: pointer tables (vtables) for all loaded modules, epoch-protected — entries removed only when safe; unloaded modules sit in the reclamation queue until no thread can reach them.
- Module storage: compact cache-friendly structures — flat arrays / contiguous memory for function pointers; no sparse maps; pool- or inline-allocated metadata; compress rarely-used data segments; mmap code so the OS pages it. The runtime-library fork removes generic code we don’t need (unused WASI syscalls, debug metadata) and rewrites internal allocation for our patterns.
4. Actor model & threads
Section titled “4. Actor model & threads”- Tokio multithreaded work-stealing; many small short tasks = effects/messages — already efficient.
- Blocking work (disk IO, media decode, heavy crypto) → tokio::spawn_blocking on dedicated OS threads.
- Configurable pools: ChatSupervisor spawns N ChatActors; N=1–2 on 1–2 cores (effectively single-threaded), N=cores on desktop. Correctness in 1-thread mode; scaling in many-thread mode. Do not spawn more actors than needed; keep tasks granular and short.
- No second ad-hoc threading model on top of Tokio — Tokio pool handles async work; spawn_blocking for the rest.
- Thread parking & idling: parked threads cost ~nothing. Background/ idle mode: pause actor timers, stop dispatching, park everything except one control thread + one network thread. Remove polling timers and background GC work when inactive.
5. Energy- and memory-saving mode (aggressive steps)
Section titled “5. Energy- and memory-saving mode (aggressive steps)”- Minimize active threads (park/terminate all but essential).
- Reduce clock frequency / OS governor if permitted; avoid busy-wait.
- Compress or drop caches: shrink/serialize image and blob caches to disk; clear and compact actor state caches on idle.
- Disable non-essentials: logging, telemetry, visual updates; periodic jobs tick far less often or only on wake.
- Lazy load/unload: unload plugin modules / resources not needed; even unload code for rarely used commands until needed again (protected by EBR).
- Memory layout: consolidate heaps, release memory to OS, clear big structures (std::mem::take), shrink vector capacities.
- Efficient data structures: value types/small structs over boxed; arena/pool allocation for actor messages (anti-fragmentation); reuse pools over HashMap-drops.
- Batching: process batched inputs in bulk on wake (fewer context switches); accumulate-then-process when idling (fewer wake-ups).
6. Build-level
Section titled “6. Build-level”- LTO / thin-LTO, strip symbols. Enable CPU features when hardware known. In the forked runtime: no debug checks, release-mode math + vectorization, shrink data types (32-bit indices where safe).
7. Implementation checklist
Section titled “7. Implementation checklist”- Continuous profiling (CPU, heap, power) in normal + idle modes.
- QSBR at safe points or crossbeam-epoch; integrate with plugin loads/unloads; HP on a small subset if needed.
- WASI backend: benchmark JIT vs WASI for power.
- Module table: compact fixed-size (Vec/ArrayVec), epoch-protected; measure pointer lookup; inline, branchless dispatch.
- Tokio config per platform (single/multi-thread); spawn_blocking for blocking; limit actor pool by config.
- Idle: stop spawning, let threads park; optional Runtime::shutdown_timeout() to suspend background threads.
- Caches must be clearable; compress large RAM-resident data.
- Battery/power APIs → detect low battery / power hints → aggressive mode.
- Testing: high-load + idle scenarios; verify CPU/memory drop significantly in eco mode; wake-up latency acceptable.
(Sources per author: crossbeam-epoch docs for EBR, preshing.com for QSBR, Wikipedia for HP, Tokio docs for work-stealing/spawn_blocking.)