Abstract machines / op-code-ish VMs in Go: any (beyond-simple-intro-tutorial) cool repos / benchs / blogs you know of?

agolangf · · 468 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<p><strong>TL,DR: looking for repos/bloggers/projects that focus (usually in &#34;interpreter&#34; contexts I guess) on highest-perf approaches to <em>runtime</em> encoding (then execution) of general-purpose turing-complete computations — whether via stack-based VMs with own minimalist opcode/bytecode &#39;languages&#39; or other approaches — ie. regardless of source language, just focusing mostly on what internal representation and &#39;compilation&#39; schemes allow for fastest ultimate evaluation/execution. Specifically in a Go context (because failing that, I can <em>still</em> proceed to easily locate stuff in other-langs/lang-independent landscapes if need be).</strong></p> <p>I&#39;m currently <a href="https://github.com/metaleap/go-corelang">implementing from papers some early-90s FP VMs</a> but it occurs to me that the FP aspect doesn&#39;t matter too much for the part where you want to (ponder how to most optimally) encode (to then perform fast-ish) &#39;computation in general&#39; at runtime.</p> <p>What I&#39;m talking about is &#39;compiling&#39; into an in-memory &#39;machine&#39; state that can then (by very simple state transition rules) run fairly fast until termination (if any), eg. separating one-time transformations/validations/etc tasks from the actual computations, aka compilation (just to memory, not to an executable binary file).</p> <p>Do any of you know of some &#39;advanced&#39; projects in Go that (by their real-world nature, user base size, or project maturity) go beyond &#34;naive eval traversal of some AST&#34; / interpreterbook / the ubiquitous stuff that advertises itself as &#34;demo to showcase basics focusing on readability/user-friendliness not performance&#34;? (All useful stuff, just not what I&#39;m looking for right here, right now.)</p> <p>To better gauge the area where I&#39;m looking to learn new tricks, let&#39;s say this is my &#34;byte-code IL&#34;:</p> <p><a href="https://github.com/metaleap/go-corelang/blob/8a5ae65b1393140d41aa956aa1c2711bb558ac1c/impl-02-gmachine-mark6/instruction.go#L20">https://github.com/metaleap/go-corelang/blob/8a5ae65b1393140d41aa956aa1c2711bb558ac1c/impl-02-gmachine-mark6/instruction.go#L20</a></p> <p>Speed of compilation-to-it (gm-compile.go) is not the most pressing issue, vs. execution starting in (and everything below) line #34 <a href="https://github.com/metaleap/go-corelang/blob/8a5ae65b1393140d41aa956aa1c2711bb558ac1c/impl-02-gmachine-mark6/gm-basics.go#L34">https://github.com/metaleap/go-corelang/blob/8a5ae65b1393140d41aa956aa1c2711bb558ac1c/impl-02-gmachine-mark6/gm-basics.go#L34</a></p> <p>I tried a jump-table <a href="https://github.com/metaleap/go-corelang/blob/8a5ae65b1393140d41aa956aa1c2711bb558ac1c/impl-02-gmachine-mark6/gm-basics.go#L100">L100-106</a> based on anecdotes I read about big-switch-blocks, but that slowed down execution a fair bit (approx. doubling the time cost) — even now all the instructions in separate methods was a slowdown vs. an earlier all-instructions-inlined-in-the-one-big-switch-block in <code>step()</code>.</p> <p>In the end I&#39;ll probably go for a more minimalist IL and one more general-purpose / less graph-reduction-focused/inspired, and am sure I&#39;ll keep running into opportunities-galore to reduce compile-time generation of redundant instructions. But on a slightly higher level, I&#39;m wondering if there have been efforts (ideally by doers tinkerers &amp; hackers vs. PhD for comprehensibility reasons &amp; metrics priorization) to evaluate whether a stacks+heap-based VM is really the most optimal design for &#34;runtime-generated executable code generation&#34;, blogging-or-FOSSing enthusiasts probing various ideas from the design space and coming to conclusions or discovering tricks that would be relevant to me here. <strong>Heard of any?</strong> (I&#39;ve only run into stuff that looks no less messy, perf-insensitive and &#34;first throw&#34; than my own stuff during my initial look-arounds.)</p> <p>Last note, I&#39;m sure lots of relevant stuff abounds in languages that wouldn&#39;t be very applicable/transferable to the Go backdrop here, especially in even lower-level languages — I reckon I can later on as the last resort utilize <code>unsafe</code> for the last-mile perf squeeze (I&#39;m thinking omitting bounds checks the various stacks and instruction-stream slices etc) — but for now I&#39;m interested in slightly higher(&#34;just above pointer-arith or C/asm-level shenanigans&#34;)-level performance considerations / investigations / benches / ponderings — cheers guys, thanks in advance for sharing what you know of that I should, too!</p> <p>I should add that my benchmark is simply <code>factorial 15</code> essentially. This is about 1000-10000x slower (ever-varying) via the VM than compiled Go (ensuring of course that the say 15 is never constant to the compiler) — ie. the difference between 150-200ns and &#34;anywhere between 200 and 2000 µs&#34;. I&#39;d rather hope &amp; aim for an at-worst 10x slow-down, rather than 1000-10000x of course ;) am doing too many allocations still, I&#39;m sure — the <code>instruction</code> shouldn&#39;t be such a big struct either — will also manually inline <code>step</code> into <code>eval</code> obviously — <em>just looking as a side-track for other prior work out there playing in very similar sandboxes</em>..</p> <p>As a general rule I&#39;ll never want to emit actual physical-machine code, assembly or LLVM byte-code or anything-like-that, so really-not-married to the notion of &#34;VMs / state-transition machines emulating stack-based machines&#34; at all. But other there any other?</p> <hr/>**评论:**<br/><br/>sbinet: <pre><p>a few pointers:</p> <ul> <li><a href="https://github.com/glycerine/zygomys" rel="nofollow">https://github.com/glycerine/zygomys</a></li> <li><a href="https://github.com/mattn/anko" rel="nofollow">https://github.com/mattn/anko</a> (used by buffalo)</li> <li><a href="https://github.com/go-interpreter/wagon" rel="nofollow">https://github.com/go-interpreter/wagon</a> (not used much (yet?))</li> </ul></pre>meta_leap: <pre><p>Most promising, thx! Let&#39;s not forget your own that I just happened upon: <a href="https://github.com/sbinet/go-eval" rel="nofollow">https://github.com/sbinet/go-eval</a> ;)</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889

468 次点击  
加入收藏 微博
暂无回复
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传