我构建了一个具有不可变对象且没有全局解释器锁(GIL)的 C++ 运行时。
在过去的几个月里,我一直在重新思考动态语言运行时如何与现代硬件进行交互。最终的成果是ProtoCore及其首个主要实现ProtoJS。
大多数动态运行时(如Python、Ruby,甚至是JS引擎)通过全局解释器锁(GIL)或复杂的内存屏障来处理并发,因为在多个线程之间管理可变状态是非常困难的。
在ProtoCore中,我采取了不同的路径,基于三个支柱:
1. 默认不可变性:所有核心数据结构都是不可变的。我们不使用锁,而是通过结构共享来提高内存效率。这在本质上消除了对象级别的数据竞争。
2. 硬件感知内存模型:对象与缓存行对齐(64字节单元),以防止伪共享并优化缓存局部性。
3. 标记指针:我们为小整数使用56位嵌入负载,这意味着大多数数值操作不需要堆分配。
为了验证这一架构,我构建了ProtoJS。它使用QuickJS进行解析,但用ProtoCore原语替换了整个运行时。这允许真正的工作线程执行(“延迟”),在不复制或涉及GIL相关争用的情况下,共享不可变对象。
当前状态:
- ProtoCore:100%测试通过率(50/50测试),并于今天完成了全面的技术审计。
- ProtoJS:第一阶段完成,展示了真正的并行执行和低于1毫秒的垃圾回收暂停。
我是一名电子工程师(现在是一名大学教授),我想看看应用低级硬件原理是否能够解决高级并发的“混乱”。
我很想听听你对这种以不可变性为首的系统编程方法的权衡看法。
ProtoCore(引擎):https://github.com/numaes/protoCore
ProtoJS(JS运行时):https://github.com/gamarino/protoJS
查看原文
I’ve spent the last few months rethinking how a dynamic language runtime should interact with modern hardware. The result is ProtoCore and its first major implementation, ProtoJS.<p>Most dynamic runtimes (Python, Ruby, and even JS engines) handle concurrency through Global Interpreter Locks (GIL) or complex memory barriers because managing mutable state across threads is notoriously difficult.<p>With ProtoCore, I took a different path based on three pillars:<p>Immutability by Default: All core data structures are immutable. Instead of locking, we use structural sharing for memory efficiency. This inherently eliminates data races at the object level.<p>Hardware-Aware Memory Model: Objects are cache-line aligned (64-byte cells) to prevent false sharing and optimize cache locality.<p>Tagged Pointers: We use a 56-bit embedded payload for SmallIntegers, meaning zero heap allocation for most numeric operations.<p>To prove the architecture, I built ProtoJS. It uses QuickJS for parsing but replaces the entire runtime with ProtoCore primitives. This allows for real worker thread execution ("Deferred") where immutable objects are shared across threads without copying or GIL-related contention.<p>Current Status:<p>ProtoCore: 100% test pass rate (50/50 tests) and a comprehensive technical audit completed today.<p>ProtoJS: Phase 1 complete, demonstrating real parallel execution and sub-1ms GC pauses.<p>I’m an Electronic Engineer by training (now a university professor), and I wanted to see if applying low-level hardware principles could fix the high-level concurrency "mess."<p>I’d love to hear your thoughts on the trade-offs of this immutable-first approach in systems programming.<p>ProtoCore (The engine): https://github.com/numaes/protoCore ProtoJS (The JS runtime): https://github.com/gamarino/protoJS