From 4643f2004efe5bc6c20647dc99811869c92ba09a Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Sun, 8 Dec 2019 23:26:19 +0000 Subject: [PATCH] the draf of choosing the platform post has been finished --- _drafts/choosing_a_platform.md | 82 +++++++++++++++++++++++++++++++--- 1 file changed, 76 insertions(+), 6 deletions(-) diff --git a/_drafts/choosing_a_platform.md b/_drafts/choosing_a_platform.md index 6fe20ef..f1e84ea 100644 --- a/_drafts/choosing_a_platform.md +++ b/_drafts/choosing_a_platform.md @@ -32,11 +32,12 @@ constantly iterate to come up with the right implementation. The evolution of programming languages such as [Scheme](https://en.wikipedia.org/wiki/Scheme_%28programming_language%29) is a good example of it (for more information take a look at [R6RS](http://www.r6rs.org/)). -While I think creating a programming language and a VM from scratch is really fun, -but it can be really frustrating as well. I don't want to get annoyed with myself -during the process and abandon my goal. I should ride on the shoulders of the giants -to gain benefit from their great work. I should choose a platform that helps me to -move faster and iterate through different ideas quicker. +Building a VM is hard and Building a fast VM is even harder. While I think creating +a programming language and a VM from scratch is really fun, but it can be really +frustrating as well. I don't want to get annoyed with myself during the process and +abandon my goal. I should ride on the shoulders of the giants to gain benefit from +their great work. I should choose a platform that helps me to move faster and iterate +through different ideas quicker. From technical perspective, Starting from scratch means that I have to write a program that includes at least a parser and a compiler. Building a compiler @@ -112,5 +113,74 @@ that I have to pass. ## JVM As much as I dislike Java (Mostly because of the syntax and the fact that it is an object -oriented language), I like JVM a lot. +oriented language), I like JVM a lot. The JVM is battle tested, well design (Well, sort of. +But it's certainly evolving.) and fast VM. It should be the most popular VM in the world +(I'm just guessing). It is one of the world's most heavily-optimized pieces of software. +Plenty of researches have been made to make it better and better. + +The JVM has a mature ecosystem and a massive community of developers that resulted in an +unbelievable number of libraries (not the largest though, NPM is the largest artifact +repository. But it has huge amount of useless BS as well). By targeting the the JVM, +users will have an easy time adopting the new languages because of the rich tools set +provided by the Java ecosystem and all the languages that targeted JVM as well. For example, +it will be possible to use libraries written in Scala or Clojure as well. + +Long story short, I think the JVM is the right platform for me. The fact that many languages +have chosen it as their base platform shows that how useful it can be. But there is a problem. +Targeting a higher level virtual machines like the JVM means that I'll have an easier job to +create a compiler. But I still have to write one. A compiler that takes the code and produces +JVM bytecode. As I mentioned earlier writing a compiler is an enormous task and the chance +of doing it wrong with some one like me who never has built a compiler before is very high. + ## One VM to rule them all +Luckily there is a solution. I can write an interpreter in a VM that is designed to optimize +my interpreter with all that wonderful JIT compilation magic. Oracle has released a new VM +that hopes to make writing language interpreters both easy and fast. It can also leverage +the huge ecosystem of the JVM. It is an enhanced JVM that contains a new JIT compiler which +can speed up interpreters to near Java speed. The new JIT compiler is called Graal. To use +the Graal’s JIT magic we can use the Truffle library to create the interpreter. We will +annotate the interpreter and give Graal some hints on invariants and type information. +According to Graal's documents, By doing this integration effort we get significant speedups +in out interpreter without having to resort to writing a bytecode compiler. + +[GraalVM](https://www.graalvm.org/) is a Java VM and JDK based on HotSpot/OpenJDK, +implemented in Java. It supports different execution modes, like ahead-of-time compilation +of Java applications for fast startup and low memory footprint. + +> GraalVM is a universal virtual machine for running applications written in JavaScript, +> Python, Ruby, R, JVM-based languages like Java, Scala, Groovy, Kotlin, Clojure, and +> LLVM-based languages such as C and C++. +> +> GraalVM removes the isolation between programming languages and enables +> interoperability in a shared runtime. It can run either standalone or in +> the context of OpenJDK, Node.js or Oracle Database. + +I copied the above paragraph from GraalVM's official website. It is truly a VM to rule +them all. + +[Truffle](https://github.com/oracle/graal/tree/master/truffle) library is one the key +players in GraalVM. The initial results of Truffle are super exciting. Implementations +of Ruby in Truffle has a performances on the same order of magnitude as the much bigger +projects of JRuby. Just checkout [Truffle Ruby](https://chrisseaton.com/truffleruby/)'s +website to get amazed by it. +There is a [Javascript implementation](https://github.com/graalvm/graaljs) +as well which showed great progress as well. +Lots of research has been dedicated to this topic and the result is mind blowing. +The interesting thing is that these Truffle implementations were done with fewer people +in a shorter period of time. This means you can create your own language on the JVM that +takes advantage of all it’s existing libraries, native threading, JIT compiler without +having to write your own compiler, and you get speeds that took other languages years +to achieve. + +Using GraalVM as the platform for my new language will help me to be much faster because +All I need to do is to build an [AST](https://en.wikipedia.org/wiki/Abstract_syntax_tree) +interpreter and Graal will handle the rest. It means that I can start by building what is +important and use a very well engineered toolkit in my advantage to get to my goal quicker +and then later on replace any part that I like with my own implementation. How cool is that??? + +But as an engineer and a wannabe scientist I'd like to see the proof with my own eye. Not +because I don't trust academic work, Just because it feels good to experience the proof. + +So to begin with I'm going to create a dead simple Serene interpreter in Java and OpenJDK +and then build the same interpreter using Java on GraalVM using Truffle library and +compare the result and prove myself that choosing GraalVM is the right choice.