by 13of40 · 10y ago · 0 comments
> type by traversing the program, instantiating methods, and checking what gets assigned to them. So we can’t really reuse the cache because we can’t know the final
> types until we type the whole program! It’s a chicken and egg problem."
Maybe I'm oversimplifying it, but suppose you compiled a statement like...
a++
...into something like...
if a is an integer (which would presumably be something fast like "CMP [RAX],123; JNZ NotAnInteger") do "INC [RAX+8]", otherwise do more expensive stuff to look up the actual type and a function to use as a ++ operator?
As long as the programmer sticks to the same type, the compiled expression runs fast, and if they switch types midstream, it still runs, albeit with a performance hit.
http://chrisseaton.com/rubytruffle/deoptimizing/
This would have the effect that you describe (actually even faster than what you're proposing, by essentially trapping on all changes to the target to know ahead of time that deoptimisation has to occur), but it's not the world's most trivial feature to implement.