10 ms·
How to write a toy JVM
- MuffinFlavored 6y agoInclude some benchmarks? :) I've always wondered how production/robust JVM implementations make themselves "faster" after they warm up.
- chrisseaton 6y ago> I've always wondered how production/robust JVM implementations make themselves "faster" after they warm up. They compile the bytecode just-in-time to native machine code, using many of the same techniques a conventional native code compiler.
- nineteen999 6y agoMaybe my knowledge isn't up to date, but I always understood (since Hotspot anyway) that not all bytecode is necessarily JIT'd to native code. From the Hotspot Wikipedia page: "Both VMs compile only often-run methods, using a configurable invocation-count threshold to decide which methods to compile."[1] Also see very old discussions at StackOverflow[2][3]. Then of course there are compilers (eg. gcj) which compile to native up-front. [1] https://en.wikipedia.org/wiki/HotSpot#Features https://en.wikipedia.org/wiki/HotSpot#Features [2] https://stackoverflow.com/questions/7100365/why-doesnt-javas-jit-compiler-translate-everything-to-native-code https://stackoverflow.com/questions/7100365/why-doesnt-javas... [3] https://stackoverflow.com/questions/16568253/difference-between-jvm-and-hotspot https://stackoverflow.com/questions/16568253/difference-betw...
- chrisseaton 6y ago> I always understood (since Hotspot anyway) that not all bytecode is necessarily JIT'd to native code Who are you disagreeing with? I didn’t say that’s the only way they execute bytecode, did I? The question was how it’s executed ‘after they warm up’. > Then of course there are compilers (eg. gcj) which compile to native up-front. But I was replying to someone who asked specifically about how ‘production JVMs’ do it, not how discontinued compilers do it.
- nineteen999 6y ago> Who are you disagreeing with? I didn’t say that’s the only way they execute bytecode, did I? Nope, my bad. Thanks for clarifying.
- bitcharmer 6y agoOnly hot code paths get compiled. That's after 10000 executions on "server" JVM and 1000 executions on a "client" JVM by default. This is by design, and if you need everything compiled right away you can set the compilation threshold to 1. I don't see any value in compiling parts of code that only gets executed during bootstrap.
- nineteen999 6y ago> I don't see any value in compiling parts of code that only gets executed during bootstrap. Not disagreeing with you there, since stopping to compile code/optimize at runtime contributes to sluggish interactive performance.
- bitcharmer 6y agoCompilation is performed concurrently to the application in specialised compilation threads. Only OSR (on-stack-replacement) requires stopping execution.
- chrisseaton 6y ago> Not disagreeing with you there, since stopping to compile code/optimize at runtime contributes to sluggish interactive performance. No JVM I am aware of stops to compile - the compiler runs on a background thread while the application continues to run as normal.
- MaxBarraclough 6y agoNitpick: the HotSpot 'client' JVM's default JIT threshold is 1500, not 1000. https://www.oracle.com/java/technologies/javase/vmoptions-jsp.html https://www.oracle.com/java/technologies/javase/vmoptions-js...
- bitcharmer 6y agoYou're right, thank you for correcting me. For more popular archs (x86, arm, aarch64) it's 1500. It's only 1000 by default for ppc, s390 and sparc. https://chriswhocodes.com/hotspot_options_jdk14.html https://chriswhocodes.com/hotspot_options_jdk14.html
- bogomipz 6y agoThis was a great read! Do you plan to continue to develop this? I would love to see more posts if so. I actually think this would be a great book as well. Cheers.
- suyash 6y agoYes, uncover the magic, good job!
- exabrial 6y agoThe opening line confuses me... The JVM is one of the fastest, well established, well documented, widely deployed platform in the world. Hundreds of languages run on it, quite quickly.
- emmanueloga_ 6y agoOpening: "Whether we like it or not, but Java is one of the most widely used programming languages. However, since most of the applications in Java are either too boring or too complex - not every Java developer has enough curiosity to look under the hood and see how JVM works." I don't think the author contradicted what you just said. I guess you may be confused by the "whether we like it or not" part? I feel like the author is commenting on Java the language (vs the JVM).
- TedDoesntTalk 6y agoI don't think English is his first language. Go easy.
- exabrial 6y agoTone is hard to communicate over the internet. I wasn't attacking him, apologies, my comment was purely critiquing the statement itself.
- giancarlostoro 6y agoOutside of the specs for the JVM itself, has anybody studied from alternate resources to learn move about the JVM? Like any specific videos or illustrative resources? I love learning about the interiors of languages, but sometimes you need a second person to explain things or some visuals to really catch it.
- eropple 6y agoPretty much anything Aleksey Shipilev has ever done, tbh. Here's an example - https://shipilev.net/blog/2014/jmm-pragmatics/ https://shipilev.net/blog/2014/jmm-pragmatics/
- jcims 6y agoFrom your link to Shiplev’s talk comes this definition of ‘nasal demons’: http://www.catb.org/jargon/html/N/nasal-demons.html http://www.catb.org/jargon/html/N/nasal-demons.html In there is a link to the thread that originated the term from 1992: http://groups.google.com/groups?hl=en&selm=10195%40ksr.com http://groups.google.com/groups?hl=en&selm=10195%40ksr.com If you scroll to the bottom you’ll find possibly the most lost soul on the Internet reviving the most dead thread ever.
- MarkSweep 6y agoThis is not a comprehensive resource, but it provides 25 bite-sized essays about different aspects of the JVM: https://shipilev.net/jvm/anatomy-quarks/ https://shipilev.net/jvm/anatomy-quarks/
- pjmlp 6y agoSure plenty of stuff. Occasionally there are such talks at Java ONE (now Code ONE), Voxxed, NDC. JVM Languages Summit also has such talks. http://openjdk.java.net/projects/mlvm/jvmlangsummit/ http://openjdk.java.net/projects/mlvm/jvmlangsummit/ Talks from Cliff Click or Gil Tene. Then you can have a look at implementations like JikesRVM (one of the first ones implemented in Java), OpenJ9 (open source variant of IBM's J9). https://www.jikesrvm.org/ https://www.jikesrvm.org/ https://www.eclipse.org/openj9/ https://www.eclipse.org/openj9/ There is plenty of other stuff, but maybe this allows you to get going.
- praveen9920 6y agoI always liked writing VMs But we have to agree that JVM feels like steam engine running in the age of electric motors. With virtualisation available cheaply in every level (hardware, arch, OS and docker) virtualisation at runtime feels like overhead. JVM was originally created for purpose of 'write once, run anywhere', which I think can be addressed in alternative ways, look at golang
- closeparen 6y agoClojure famously takes exactly the opposite tack: https://clojure.org/about/rationale#_languages_and_platforms https://clojure.org/about/rationale#_languages_and_platforms
- monadic2 6y ago> With virtualisation available cheaply in every level (hardware, arch, OS and docker) virtualisation at runtime feels like overhead. These seem to be solving fundamentally different problems. Where do you see the overlap that might be able to be moved out of the jvm?
- saagarjha 6y agoI don’t see how Go presents anything new in the portability front.
- on_and_off 6y agoSo why CAFEBABE ? it is a random hex value looking like words or intentional to say that ? edit, found it : https://dzone.com/articles/the-magic-word-in-java-cafebabe https://dzone.com/articles/the-magic-word-in-java-cafebabe
- mettamage 6y agoI never thought of CAFE as a hex word. I always knew about DEADBEEF and so on. Now I feel that there should be a website that lists all kinds of fun hex words. Like CAFEFACE
- Donckele 6y agoMagic Numbers https://en.wikipedia.org/wiki/Magic_number_(programming) https://en.wikipedia.org/wiki/Magic_number_(programming) Hexspeak https://en.wikipedia.org/wiki/Hexspeak https://en.wikipedia.org/wiki/Hexspeak
- Symbiote 6y agoWe don't really need a website to do it: grep -i '^[0-9a-f]*$' /usr/share/dict/words
- raverbashing 6y agoI'd say you might want to grep for [0134567] instead (being charitable) and maybe use sed y command to find the l33t equivalences 0 - o 1 - I 3 - E (redundant) 4 - A (redundant) 5 - S 6 - G 7 - T
- deleted 6y ago[deleted]
- grishka 6y agoIIRC Facebook uses IPv6 addresses that have face:b00c in them
- duskwuff 6y agoApple uses a bunch of hexspeak for error codes in iOS, including 8BADFOOD ("ate bad food"), C00010FF ("cool off" - related to thermal events), and DEAD10CC ("deadlocc" - deadlock). There's also DEFEC8ED ("defecated"), which was used by OpenSolaris for core dumps.
- orangepanda 6y ago> There are 11 groups of instructions [missing] and most of them are trivial: > * Conversions (int to short, int to float, …). float to string should be the most trivial of all
- lihaoyi 6y agoOn the topic of toy JVMs, my Metascala project is a JVM implemented in ~4000 lines of Scala that is complete enough to interpret itself! - https://github.com/lihaoyi/Metascala https://github.com/lihaoyi/Metascala Interpreting code in Metascala is about 100x slower than just running it, and interpreting code in Metascala interpreted by Metascala is about 10,000x slower than just running it. Not going to win any performance benchmarks, but it's a cool demonstration of how a JVM works. All the runtime data structures, memory allocation and garbage collection, method dispatch logic, stack trace management, exceptions, inheritance, object layouts, etc. are all implemented in a relatively small amount of relatively simple code. For example, here is the implementation of the heap, which allocates the VM's objects inside a big byte array and has a simple copying semispace garbage collector to clean them up: - https://github.com/lihaoyi/Metascala/blob/master/src/main/scala/metascala/Heap.scala https://github.com/lihaoyi/Metascala/blob/master/src/main/sc...
- miguendes 6y agoThis is awesome! I have been working on a interpreter for a toy language and my next step would be study the JVM. I'll definitely check your project out
- rightbyte 6y agoI like the commented out print statements all over the place. It make the code seem alive somehow.
- z3t4 6y agoMost of the performance overhead is probably the print messages as those are likely sync.
- Sevaris 6y agoAs always, I'm impressed with the amount of work you do. Have you ever talked about how you manage to be so productive?
- elric 6y agoAnd here's a talk by Ben Evans about implementing a JVM in Rust. https://www.youtube.com/watch?v=7ECbwgkHdAE https://www.youtube.com/watch?v=7ECbwgkHdAE
- m12k 6y agoTo anyone writing an article like this: Please mention what language you are using before your first code listing, not four paragraphs after.
- jbreckmckye 6y agoI didn't feel I needed to know Go to read the example code. (Plus, didn't "if err != nil" give it away?)
- afandian 6y agoI bought the JVM Specification book some years ago. It was fun holiday reading (seriously) seeing how they bytecode was put together, how try-catch blocks really work etc. It's quite readable as general interest, if you're into that kind of thing. I don't think it's ever been much actual use to me in programming, but was nice-to-have background knowledge.
- bArray 6y agoYou mean like this book? https://docs.oracle.com/javase/specs/jvms/se8/jvms8.pdf https://docs.oracle.com/javase/specs/jvms/se8/jvms8.pdf I'm very tempted to pick a hard copy up!
- nathell 6y agoImplementing a VM is a unique experience. You take some bytecode that's initially a meaningless blob, sketch out an execution environment, and start implementing opcodes, one by one, and the blob actually starts doing real things. Back at uni, I've done this with the ZMachine [1], in (non-idiomatic, newbie) Haskell [2], using Zork 1 as the blob. Sixteen years after, I remember the elation when my interpreter first printed out the familiar message: You are standing in an open field west of a white house, with a boarded front door. [1]: https://en.wikipedia.org/wiki/Z-machine https://en.wikipedia.org/wiki/Z-machine [2]: https://github.com/nathell/haze https://github.com/nathell/haze
- AndrewDavis 6y agoIt truly is unique. I wrote a chip8[1] interpreter shortly after finishing my first year of university and was still a very novice programmer. My implementation was poor even by novice standards, i knew there was a lot of spaghetti but I didn't mind. Implementing each opcode was like beating a level in a video game - and it worked, not perfectly and with some unsolved bugs. Tetris worked flawlessly though. [1]https://en.wikipedia.org/wiki/Chip-8 https://en.wikipedia.org/wiki/Chip-8
- cardiffspaceman 6y agoI did this with the Z80 machine code variant that runs in the GameBoy. I defined my goal as, run the boot rom until it jumps to the game. The boot rom makes sound and moves a logo on the screen. Not the first person to do this and not the last. But interesting projects you can do after you get one going is, study on why Android doesn't run JVM bytecode, or come up with a better byte code for Java/Scala.
- bArray 6y agoI fell down the rabbit hole trying to answer "what is the smallest JVM that could be implemented". I've come to TinyVM [1] made for some Lego system which uses about 10kB of RAM. I was wondering about porting it to other micro-controllers... [1] http://tinyvm.sourceforge.net/ http://tinyvm.sourceforge.net/
- fmakunbound 6y ago> what’s missing The other two hundred instructions, the runtime, OOP type system, and a few other things. Also bytecode verifier