
HolyJit: A New Hope - bpierre
https://blog.mozilla.org/javascript/2017/10/20/holyjit-a-new-hope/
======
DannyBee
For those wondering, it's a specializer, applied to an interpreter, to
specialize the interpreter into a jit.

This is a pretty well explored technique, it's rarely done these days because
historically, people could not get good enough performance.

(I am not trying to knock them, just put it in context)

For more context on how you'd do something like this, read this:

[https://en.wikipedia.org/wiki/Partial_evaluation#Futamura_pr...](https://en.wikipedia.org/wiki/Partial_evaluation#Futamura_projections)
and [http://blog.sigfpe.com/2009/05/three-projections-of-
doctor-f...](http://blog.sigfpe.com/2009/05/three-projections-of-doctor-
futamura.html)

~~~
stefano
Isn't this what pypy does? pypy does the specialization at runtime, maybe this
does it at (interpreter) compile time?

~~~
steveklabnik
Given how HolyJIT is implemented (with compiler plugins etc), I believe that
it does this work at compile time, yes. I haven't actually confirmed this
though.

~~~
nbpname
The goal is to optimize the code both at compile-time and at run-time. We need
to optimize it at compile time to prevent slow start-up, and we need to
optimize it at run-time to benefit from profile guided optimizations.

------
readittwice
No offense, but the blog post is seriously missing a better (or any?)
explanation of what's the new JIT all about. At first I even thought my
browser loaded only half the page...

~~~
bzbarsky
It does link to
[https://github.com/nbp/holyjit](https://github.com/nbp/holyjit) which
explains pretty much everything there is to explain so far...

~~~
jcadam
That's all well and good, but github is blocked where I work.

~~~
pirocks
If you don't mind me asking, why is github blocked?

~~~
jcadam
Government site. There doesn't have to be a reason.

~~~
Touche
I hope they are paying you very well.

~~~
microcolonel
Government site. They aren't paying James well.

Added: if you think I'm joking or being unfair, just look at the compensation
tables for just about any government outfit. They top out around a salary that
is considered _average_ for software folks in some places.

~~~
Touche
I know that that is typical, but there are exceptions, and if you work for the
right agency you _can_ be paid well, so I don't want to make any assumptions.

If he's _not_ paid well then I hope he realizes that by merely being aware of
HN and GitHub puts him in the top 10% of developers and he can do a lot better
than working at a place that restricts his ability to educate himself.

~~~
jcadam
Oh, I'm a contractor and I'm paid decently, but the working conditions are
terrible. I've been trying to find a job outside of defense/govt work for a
long time, but either the pay isn't as good (the one or two times I managed to
land an offer) or I'm not a "cultural fit." Not having a network outside of
the govt bubble makes it hard.

~~~
jasondclinton
Hi there, James. I'm a software engineer at Google. I've worked with many
former government and defense contractors here whom I would consider some of
the best engineers. Please send me your resume (or anyone else with a similar
situation!); my email is in my profile. We have offices all over the US (but
not Florida) so the bay area wouldn't be a requirement.

~~~
jcadam
Well, I finally got tired of this place and went and got an offer from a local
aerospace company, I suppose I ought to see how that goes :)

------
bnjbvr
For what it's worth, the name is obviously a pun and a small wink to the Graal
VM [1]. Not sure there's any intention to reference TempleOS's HolyC.

[1] [https://github.com/graalvm/](https://github.com/graalvm/)

~~~
m12k
It could also be a play on the phrase "Holy shit!"

~~~
nbpname
Both the reference to the GraalVM and a pun are intentional.

~~~
mtgx
Maybe make its slogan: _" HolyJit, it's fast!"_ \-- with the double meaning
and all.

------
kodablah
On a similar note, I have been watching this[0] project which provides an IR
to target and optional optimizations. But I like the idea that the only
difference between a JIT and an AOT compiler is optimization choice (assuming
we don't include tracing as part of the JIT features).

Also, this[1] blog series is a must-read for interested beginners.

0 -
[https://github.com/stoklund/cretonne/](https://github.com/stoklund/cretonne/)
1 - [https://eli.thegreenplace.net/2017/adventures-in-jit-
compila...](https://eli.thegreenplace.net/2017/adventures-in-jit-compilation-
part-1-an-interpreter/)

~~~
nbpname
It sounds more than likely that HolyJit will use Cretonne as a backend.

For the moment it uses dynasm, just to get the prototype working, but I expect
to change that in the upcoming months.

(I am the author of HolyJit)

------
jarym
"This means more time to implement JavaScript features" \- yes, because
JavaScript isn't getting enough features fast enough :-p

Jokes aside, I love this kind of work from the Mozilla team. This and the bits
going into Quantum are really amazing pieces of software engineering in my
opinion.

~~~
SwellJoe
I'm often overwhelmed by the size of the JavaScript language; it's a _big_
language (and I'm coming from mostly working in Perl which is also a very big
language...but it is dwarfed by modern JS in terms of TIMTOWTDI and in terms
of core language features). But, every time I learn about a new feature, I
can't fault them for adding it. It's usually a clear improvement in terms of
readability and capability. And, because it's such a widely used language
there tends to be a lot of input before new features are added. They are
rarely half-baked once they reach the standard.

So, I agree it's overwhelming; books about JavaScript from two years ago are
already out of date on a lot of fronts. But, it's also resulted in a really
powerful and concise language.

~~~
dannyw
Well, except WebAudio. That standard omits some essential low level audio
primitives while featuring an incomplete scatter of high level APIs.

~~~
SwellJoe
Agreed. I've only given it a cursory glance, but I've read several people
bemoaning how awful it is. And, I think it's an example of what happens when
APIs are built by folks who aren't actually building things with them. Audio
always seems to get screwed up by engineers, sometimes for years. Linux had
absolutely shitty audio up until...like yesterday.

It's a feedback loop, I think. Almost nobody uses the web for serious audio
because the web sucks for serious audio, and thus almost nobody who uses the
web for serious audio is working on the standards for web audio. I admire
anyone who can make the web platform work at all for anything audio related.

~~~
mwcampbell
> Linux had absolutely shitty audio up until...like yesterday.

Is that just hyperbole, or has there been a recent (within the past 6 months)
development that has made Linux audio better?

~~~
SwellJoe
Hyperbole. It really was fixed a couple years ago, maybe as much as three or
four, if you were running a cutting-edge distribution like Fedora. But, it was
entirely fair to say Linux absolutely sucked at audio for decades.

------
bananicorn
I'm not sure as to what is fundamentally different about this JIT-compiler
(except for the awesome name of course)

Is it basically just a Rust rewrite which also tries to reduce the complexity
of their current just-in-time compiler?

Edit: By calling it "just" a Rust rewrite, I'm not implying that's a simple
undertaking, even moreso considering the complexity of modern JS engines.

~~~
jchw
Basically, instead of manually writing assembly fragments, they want to reuse
annotated interpreter code, thus ensuring both correctness and safety for JIT-
generated code, while reducing the redundancy. The rustc compiler is used to
generate assembly fragments for the JIT, directly out of the interpreter code.
It seems like a worthwhile endeavor.

~~~
leeoniya
doesnt rustc rely on LLVM to generate its assembly. i watch the webrender repo
and there have been several issues about poor codegen that LLVM could not
properly handle. some were also related to absent optimizations at the MIR
level.

also would that mean that the asm snippets could change as llvm changes and
possibly cause security bugs if not carefully hand-audited/tweaked anyhow?

~~~
steveklabnik
Rustc does, but it doesn't appear to me that holyjit does, that is
[https://github.com/nbp/holyjit/blob/master/lib/src/compile.r...](https://github.com/nbp/holyjit/blob/master/lib/src/compile.rs)
and
[https://github.com/nbp/holyjit/blob/master/lib/src/lib.rs#L1...](https://github.com/nbp/holyjit/blob/master/lib/src/lib.rs#L16-L19)

Basically, it seems (and I haven't fully digested the code yet) that it uses
[https://crates.io/crates/dynasmrt](https://crates.io/crates/dynasmrt) for
codegen.

~~~
tbodt
Looks like dynasmrt was written for use in holyjit.

~~~
steveklabnik
Quite possibly! It's the first I'm hearing of it. It's _very_ interesting...

------
infogulch
This is amazing! Write a regular old interpreter in a fully featured language,
then just wrap it in a macro and _poof_ you magically have a jit that will
compile and execute your interpreted code on the fly.

Seriously. Look at the example for brainfuck [0], it's less than 70 lines of
completely normal interpreter loop, including a one-line macro on the top.
What the heck.

[0]:
[https://github.com/nbp/holyjit/blob/master/examples/brainfuc...](https://github.com/nbp/holyjit/blob/master/examples/brainfuck.rs)

~~~
kannanvijayan
The BF example was pretty cool and was sweet to see in action. That said, the
language is extremely simple, and the real meat of the problem with most
dynamic languages is not in the compilation per se, but the runtime type model
and specializing code on the basis of type-input fed from that model.

I think a simple next step is to take a small toy language (there's a small
rust scheme implementation written by another team member that might serve as
a good candidate, if extended with js-style prototype-based objects), and
prove this out for an actual type-driven specialization.

But yeah, the possibilities are certainly exciting.

------
MrZipf
36000 lines of handwritten assembler in v8? Not since the transition to
ignition and turbofan...

$ git clone [https://github.com/v8/v8.git](https://github.com/v8/v8.git) $ cd
v8 $ find . -name '*.S'

There's still some macro assembler in the built-in's but it's emitted rather
than being assembler.

~~~
nbpname
For both SpiderMonkey and v8, this is counting the number of calls to the
MacroAssembler. SpiderMonkey commonly use the prefix "masm", while v8 uses the
macro "__ " to alias the MacroAssembler.

The MacroAssembler, is basically what is used to produce assembly code in both
JavaScript engines.

------
nathcd
This seems really cool! I know it's been mentioned before that there are no
plans to rewrite SpiderMonkey in Rust, but this sort of feels like a tiny
first step in that direction?

Almost reminds me of early on in Servo's history:
[https://news.ycombinator.com/item?id=6268521](https://news.ycombinator.com/item?id=6268521)

(Although I may just be projecting what I want to hear :) it's exciting
hearing about new Rust projects, especially new stuff going into Firefox.)

~~~
kibwen
Yes, I think it's safe to compare this to the early days of Servo for now, in
that one should not necessarily expect to see this in Firefox for the next
decade, though the research and expertise gained from creating it might work
its way in before then. Still exciting nonetheless!

------
jkabrg
How is this different from Pypy, other than the fact that ones uses RPython
and the other uses Rust?

~~~
masklinn
HolyJIT seems to be similar to RPython rather than Pypy: RPython is both the
restricted language and the tooling around it, Pypy is a Python interpreter
implemented using RPython.

------
immutable_ai
Is this basically the same transition as V8 did with crankshaft to
ignition+turbofan?

~~~
chrisseaton
I think that was the exact opposite - they generated an interpreter from their
JIT. This generates a JIT from their interpreter, like PyPy and Truffle do.

~~~
_old_dude_
It seems to be to generate JIT snippets from the Rust compiler, not an
interpreter.

~~~
Sean1708
As I understand it you write an interpreter in Rust then you generate a JIT
compiler from that interpreter.

------
nimish
Smells like Graal/Truffle

------
unkown-unknowns
When I first saw the name "HolyJit" On Reddit I thought it was going to be
something new from the TempleOS guy.

------
XR0CSWV3h3kZWg
By my understanding

HolyJit:Rust::Rpython (toolkit):Rpython (language)

Rpython being the language and toolkit that is used to create pypy.

------
benjismith
Gotta say... this is pretty badass.

------
bradknowles
What is a jit?

Why would you want one?

Can it help you write in a jot?

Can it help you write in a dot?

Why do I feel like I’m trapped in The Land of Doctor Seuss?

------
beached_whale
I was expecting TempleOS getting a new jit

~~~
KGIII
I confess, I actually chuckled out loud.

I haven't seen any of their posts for a while. I hope they are okay.

~~~
beached_whale
I find it amazing what he has done. I did say new jit as HolyC is compiled on
the fly and has some amazing features that no other system has(embedded
images). Yeah there are some esoteric features, but that is the benefit of
DIY.

~~~
KGIII
You have inspired me. I'll torrent his most recent ISO and spin it up in a VM
over the weekend. I will give it an honest look and be objective.

I should set a blog up before that.

~~~
beached_whale
You will need to ignore anything non-technical. He suffers from mental illness
and says offensive things.

~~~
KGIII
I was online in the 80s. I have an account on voat. I visit Slashdot. I should
be ok. ;-)

~~~
beached_whale
Ah Slashdot. I was there when it was cool :) I do have a 5 digit id, so there
is that.

How is voat, like is the programming/tech comments worthy?

------
explodingcamera
Kinda reminds me of holyC ;)

------
lettergram
First thought was: "Is this another Terry Davis language/framework?"[1]

No, no it is not.

I would suggest changing the name from HolyJit to anything else.

[1]
[https://en.m.wikipedia.org/wiki/TempleOS](https://en.m.wikipedia.org/wiki/TempleOS)

~~~
thesmallestcat
But it sounds like "holy shit" LOL. You know what else is funny? Farts!

~~~
jchw
Well you know, as Louis C.K. once said, you don't have to be smart to laugh at
farts, but you have to be stupid not to.

------
agounaris
I admire how mozilla is so passionate about what they develop but still, in
2017, firefox needs restart every couple of hours for performance reasons. I
kinda of feel like they have the proof of concept working but its never good
enough for the wider audience.

~~~
lucb1e
In class there are at least 30 out of 40 people using Firefox on Ubuntu with
no such issues. On my laptop I use Firefox on Debian with no such issues. Also
previously on Windows XP, 7 or 8, both in virtual machines and on hardware, I
never had such issues, but that's a while ago so not really applicable
anymore.

You might want to submit a bug with your specific hardware to see if anyone
can identify the cause, because that's not normal.

~~~
baby
Do they use tree style tabs though?

~~~
icebraining
I do. Works fine.

------
baby
In the mean time, the performance for Firefox' Tree Style Tabs plugin have
gone downhill. I'd be interested to know more about new browsers that put tabs
on the side by default.

------
TomMarius
For a moment I hoped this had something to do with HolyC

------
skocznymroczny
Can we just drop the JS VM and embed some nicer VM (JVM, DartVM) instead?

~~~
gilrain
Yes, once you convince everyone to drop the thing we have that works and
implement the thing we don't have that doesn't work. In short: you complain as
though it were an easy change to make.

~~~
pjmlp
He/she is right though, just wait when WebAssembly gets more mature.

I bet all those plugins will be back.

~~~
coldtea
WebAssembly has no good story for the DOM.

~~~
moocowtruck
i feel like that's on purpose...we can engineer all these crazy things, but
getting good DOM access? naw not possible

~~~
bbatha
wasm is an MVP, they fully intend to add DOM support:
[http://webassembly.org/docs/future-
features](http://webassembly.org/docs/future-features)

Its just that implementing the DOM requires a lot of other (complex) things
1\. Stable JS object ABI 2\. Integration with the GC garbage collector 3\.
Better integration with modules

Personally, I'm hoping that we actually get a lower-level subset of the DOM
apis that doesn't rely on as many OO features so we can bind to it easier, and
avoid more DOM manipulation overhead (though I have no idea what this would
look like).

~~~
tmzt
Personally I'd like to see an experimental mode in Quantum/Servo with an RPC
interface to render directly to the display list, possibly with an
intermediary like a vector of VDOM operations.

------
pcunite
It's fun to mock God ... until he judges you for it.

Daniel 5

------
koolba
Why does the Cargo.toml[1] file in the source repo for HolyJit have the word
"brainfuck"[2] in it?

Does that have some significance to Rust or Mozilla? Or is this a case of copy
pasta?

[1]:
[https://github.com/nbp/holyjit/blob/master/Cargo.toml](https://github.com/nbp/holyjit/blob/master/Cargo.toml)

[2]: Permanent link to line:
[https://github.com/nbp/holyjit/blob/1f20eb41de2dae14179815c7...](https://github.com/nbp/holyjit/blob/1f20eb41de2dae14179815c7d5ebb8780051e13c/Cargo.toml#L17)

~~~
ewillbefull
[https://en.wikipedia.org/wiki/Brainfuck](https://en.wikipedia.org/wiki/Brainfuck)

The HolyJit repo has a brainfuck jit example in the repository:
[https://github.com/nbp/holyjit/blob/master/examples/brainfuc...](https://github.com/nbp/holyjit/blob/master/examples/brainfuck.rs)

