Hacker News new | ask | show | jobs
by setzer22 2141 days ago
Genuine curiosity: How can it check the types at the call side quickly? Let's say I have a function that averages an array of floats. Does the JIT have to check each of the array's inputs to have the right type? That looks like it could become a serious overhead.
1 comments

Usually how it works is it does that a couple of times, while gathering some kind of stats.

If those stats prove that it is always an array of floats, then machine code gets generated for an array of floats, with a trap handler for when the code fails.

The trap handler, if called, will throw away the code and restart the analysis.

So if you are nice for the compiler and keep your types regular, the machine code will not be thrown away and there are no speed bumps.

(Answering a bit late, sorry I missed your reply!)

I still don't get it... How do you know when to activate the trap handler if you're not checking every time? It's not enough to wait for an error condition (e.g. an invalid pointer). If it's an array of integers, and I sneak in a string in there, what's stopping the JITed version from treating that pointer as an integer?