AST, IR, bytecode and friends - standards and inovation
Spin off from https://github.com/nodejs/CTC/issues/100#issuecomment-294159465
The premise: people in nodejs have ideas that require manipulation of the AST or of IR. The people of V8 at present have no plan to open the engine's AST as API, but are close to achieving useable bytecode modularization (nodejs/node#11842).
Discussion points:
- what can we do to promote language based innovation and optimization?
- can we / do we want to do bytecode caching and optimization a la python's
.pyc? - should we consider embedding one of the userland transpilers?
/cc @addaleax @hashseed @MylesBorins @indutny
[update 2017-04-20] Some motivations:
- @Qard (https://github.com/nodejs/CTC/issues/104#issuecomment-295557941) is using AST manipulation for instrumentation of multiple version of
express - New solutions such as
util.awaitable/promisify(#12 or nodejs/node#12442), could come with an easy way to for userland to apply to everything. - The need for an AOP crosscutting-concern point-cut mechanism. e.g. Cross-cutting concern such as optimization and de-deopting (nodejs/node#12456) clash with readability/maintainability/DRY (also nodejs/node#12471)
- Enter the TC39 stage process as a "compatible implementation"
Feel free to edit my title/desc...
So I was asking, should we consider embedding babel as a frontend to complement V8 as a back end? Making us innovators not only in performance and portability, but also in the language aspect.
/cc @Qard nodejs/node#12349
Just to be clear: V8's Ignition bytecode is not a serialized AST. Its format is not designed to be public, but rather an implementation detail. The bytecode encodes data specific to V8's design choices (value representation, tagging scheme, call convention, object layout, metadata slot indices, etc.). Currently it is evolving quickly. We cannot afford to freeze the bytecode format or even specify it formally.
I agree that it is desirable to have an intermediate format to avoid some parsing, but Ignition bytecode is not that format. When you think of bytecode, you probably think of Java bytecode. Ignition bytecode is very unlike Java bytecode in that it is very much intertwined with V8's implementation details, whereas Java bytecode is a public format shared between many Java implementations.
WebAssembly is much closer, though with caveats.
One thing that I'm really interested in is how we are going to be representing the DAG of an ESModule based import before handing it over to V8 to execute. This is something that @bmeck could comment on. I'm unsure if there are plans to have lifecycle hooks here, but this could potentially give us a defined DAG to crawl and also a way of dumping an intermediate representation for faster boot times (this is completely theoretical btw).
In the past I helped organize an AST summit involving stakeholders from Esprima, Acorn, and Babel... perhaps some of those folks could chime in. I'm not 100% sure if that a standard is something that should live in core, but perhaps it is something we could collaborate on.
/cc @ariya @mikesherov @thejameskyle @kittens @hzoo
Not entirely sure I understand the DAG needs for the format besides mapping of specifier to resolved IR subgraph. I think it might be possible to create bundled DAG of ESM, but it might not always be possible (like if a dep is CommonJS) which we have to do some odd things outside of ESM source code and pass refs into ESM.
The V8 team has been looking into parsing speed a lot during the last 12 months, since this is highly important for page load these days. The V8 parser improved a lot during that time, and might improve even further. Since the engine cannot just blindly trust the bytecode, some verification has to happen as well, plus the external bytecode must be compiled to our internal bytecode. All of this takes time, and at this point we don't have reasons to believe that it'll be beneficial, based on the data we have.
While I'm working on ways to better support AST manipulation in core right now, I don't believe it's currently the right time to bundle the AST manipulation part itself into core. V8 is a moving target in that regard, and it would be a substantial issue for the VM-agnostic efforts to deal with. I also don't think we should be pulling larger codebases like esprima into core.
In my opinion, the best way forward is minimal changes to the require system now and evaluating whether a standard interface across VMs for AST manipulation is worth pursuing. (My gut feeling is no, but who knows?)
I'm not sure I understand the value in having Node standardize around an AST. But if I was to suggest are starting place if would be the Shift AST: https://github.com/shapesecurity/shift-spec
I believe this is an interesting topic, but:
The premise: people in nodejs have ideas that require manipulation of the AST or of IR.
Who? Why?
Application Performance Monitoring vendors want it for safer and better performing instrumentation than monkey-patching everything async in the environment with a bunch of closures.
It's also useful for language transpiling, applying code coverage tracking instrumentation, stubbing files for mocks in a test suite and numerous other possibilities.
@Qard is this a lack of hooks problem, or are people actually modifying how non-exported code runs?
If the Node Foundation wants to get more involved in the JS-to-JS compiler pipeline for JavaScript I can introduce the Babel team, I know that they are looking for more support, and Babel is effectively the de-facto tool for this in the community.
@Qard thanks! Do you mind showing me an example of how one would use such hypothetical tools?
@benjamingr not sure what you mean by hypothetical, both @Qard and I as well as others in the APM vendor space do this. Here is actual code:
var Module = Object.getPrototypeOf(module)
Module.__compile = Module._compile
Module._compile = function (content, filename) {
if (fileTest(filename)) {
content = tx.transform(content, filename)
}
return this.__compile(content, filename)
}
}
Our tx.transform adds function entry/exit timing information using a source code transform. @Qard's probably does something similar, though what everyone does is specific to the type of information their APM wants to extract.
@Qard @sam-github would https://github.com/v8/v8/blob/master/include/v8.h#L6288 or similar being exposed fix that?
@bmeck In our APM work we do need to patch non-exported module internals fairly frequently.
@matthewloring is that to modify behavior or get timing etc.?
It is usually to either collect timing information or to fix userspace queuing issues (see 5 under desired features) that break async context propagation.
The FunctionEntryHook is quite heavyweight and will run on every generated functions when most APM solutions only care about a small number.
@bmeck I don't know enough about the V8 to know if that would help, but it looks to me like as @matthewloring says, it runs for every single function, almost certainly cannot be used to reenter into V8 and run js code, probably is a singleton (it can't be used by an APM module and by the profiler), and it can't be used to pass closure info to solve the user-spacce queuing, aka "connection pooling", problem (though the async-hooks APIs may eventually help with that).