2 ms·
"In many cases, there is a lot of dynamic behavior when an app server starts up, but later on, the system settles down, there's no fluidity in type information,
by dkersten 16y ago
"In many cases, there is a lot of dynamic behavior when an app server starts up, but later on, the system settles down, there's no fluidity in type information, and the system might as well be statically typed."
I am quietly (and very slowly, its very much a side project) working on an experimental language, which, while statically typed (using type inference), allows for many dynamic operations as you would expect from a dynamic language. What I am trying to do is detect as much as possible during compilation and storing the rest as metadata. The metadata also contains a lot of other information, such as register usage. Then the runtime is a hybrid between AOT and JIT compilation, where most of the program is native AOT compiled, but that the runtime can choose to recompile segments of code using the JIT, eg, to optimize the common path once it has been detected (inlining, avoiding branch misprediction, optimizing for cache usage and avoiding register spilling etc), or to resolve dynamic types that could not be determined at compile time.
Every now and again, discussions of AOT vs JIT arise and people on both sides have good arguments as to why their compilation strategy of choice can generate the most efficient code. I want to explore mixing both strategies in a single language/runtime in an attempt to get the best of both worlds. I have searched online, but have not yet found anything similar (but if someone knows of a language or VM which mixes native ahead of time compilation with JIT compilation in the same executable, please let me know!)
So far, I have most of the type inference engine and abstract syntax tree done and some prototype code in assembly and C which will eventually become the code generator, runtime library and JIT. I am also still working out semantics and syntax of the language itself (which I need to finish before writing a parser, finishing the AST and writing the code generator and JIT..) A lot still needs to be done and I'm not finding as much time for it as I'd like (too many other projects :P), but I'll get there eventually. I'll probably blog about it when its closer to working.
"back-port static type annotations to the source code"
I love this idea. Profile-guided type resolution or something like that.
- stcredzero 16y agoExactly. I'm thinking more of approaching it from the dynamic language side, and making applications "harder" with more static typing as they mature and stabilize. I think that's the right time to benefit from the added documentation of type annotations. At the same time, programmers also benefit from the flexibility of the dynamic language during early prototyping and initial development stages.
- dkersten 16y agoThat makes sense, since you want to quickly prototype code (ignoring those annoying type annotations :D) and as the code matures, you want the extra details inserted to help achieve higher performance. Get it right, THEN get it fast. In my case, I try to have the compiler deduce the information to make it fast, where it can, but fall back to JIT when it can't, or where running the application is the best way of figuring out what needs to be optimized. The missing link between what I'm doing and what you are suggesting is that the runtime should be able to dump metadata for the compiler to use next time around. I'm going to have to think about this a bit, I think there may be some great applications of this idea, eg, turning assertion failures into unit test stubs. It would be great if the compiler and runtime worked together to help you program 1) more efficient programs and, probably more importantly, 2) correcter, less error prone programs. I love the concept of the compiler "hardening" code that was detected to produce errors in a previous run, especially if the JIT compiler can remove checks that are not needed.
- stcredzero 16y agoIMO, Doing this sort of approach within the micro-iterations of an individual programmer's edit-compile-test cycle is not quite the best place to do this. I envision the "hardness level" of a code base to be about the same throughout one iteration/release. This gives time for the production application to generate runtime profile data from actual use. In this case, the more data, the better. This data can then be massaged and verified by programmers using suitable tools and their in-depth knowledge of the application. (So runtime traces which contradict each other's type inferences can be looked at, etc.) Such tools can also apply a set of massaged type annotations to a branch of the code base, which can then be tested and merged back into the main branch of the application. I think this is a very natural pace of this level of change for most shops. This technique can also be adapted to apply to specific sections of code which need to be optimized, or can be applied incrementally across a code base over time.
- tlack 16y agoAn easy/fun testbed for this idea might be Erlang's new type specs: http://www.erlang.org/doc/reference_manual/typespec.html http://www.erlang.org/doc/reference_manual/typespec.html You could write a parse transform that wraps all exported funs in a module in some kind of type analyzing shim function, which would eventually output type specs as an .hrl you could -include(). Type specs will eventually be used by Dialyzer and other Erlang tools to issue warnings and optimize code, so the effort wouldn't be in vain. Seems like a quick way to figure out the feasibility of your excellent idea.