5 ms·
> Now onto the new instruction: Adding a special VM instruction for such operations as inheriting from a class is a strikingly bad design decision. You want t
by kazinator 7y ago
> Now onto the new instruction:
Adding a special VM instruction for such operations as inheriting from a class is a strikingly bad design decision.
You want to minimize the proliferation of VM instructions as much as possible.
A rule of thumb is: is it unreasonable to compile into a function call? Or else, is it likely to be heavily used in inner loops, requiring the fastest possible dispatch? If no, don't add an instruction for it. Compile it into an ordinary call of a run-time support function.
You're not going to inherit from a class hundreds of millions of times in some hot loop where the application spends 80% of its time; and if someone contrives such a thing, they don't necessarily deserve the language level support for cutting their run-time down.
- monocasa 7y agoIt depends on the language, but in the Smalltalk vm I'm writing, super class methods get called a lot it seems. More than enough to have their own bytecode. > You want to minimize the proliferation of VM instructions as much as possible. I guess that seems like half the trade off to me, or else VMs would all be OISCs. What you really want to do is to approximate Huffman encoding in your ISA, balancing that with ease of parsing (so bytes generally rather than bits).
- kazinator 7y agoBut this OP_INHERIT instruction isn't for calling methods; it's just for setting up the inheritance relationship.
- _old_dude_ 7y agoThe opcode OP_INHERIT takes two classes as parameter on the stack so this is equivalent to do a function/method calls with the class and the super class and let the function do the magic of filling the vtable, etc.
- munificent 7y agoThis is a good point. One of the things I have to balance with the book is teaching the optimal way to do something without dragging through reader through a large amount of verbose, grungy code. So sometimes (but not too often) I take a simpler approach even if it's not the best one. With this VM, we have plenty of opcode space left, so it's natural to just add another instruction for inheritance, even if it does mean that the bytecode dispatch loop doesn't fit in the instruction cache quite as well. I'll think about this some more. I'm very hesitant to make sweeping changes to the chapter (I want nothing more in life than for the book to be done), but this would be a good place to teach readers this fundamental technique of compiling language constructs to runtime function calls.
- wool_gather 7y agoProbably sufficient to add it as a chapter footnote/challenge like you did with flexible array members for the string implementation. Maybe with a pointer to an example if you have time to find one.
- hhas01 7y agoThis. Compromises are fine: real-world solutions make them all the time. What’s valuable to readers is displaying the reasoning behind them.
- vmsp 7y agoHi Bob! I just want to tell you that I've just started reading your book and I'm enjoying it very much. Maybe you could add a design note in the chapter with the mentioned content? Keep it up!
- munificent 7y ago> Maybe you could add a design note in the chapter with the mentioned content? Great idea! Made a note of it: https://github.com/munificent/craftinginterpreters/issues/621 https://github.com/munificent/craftinginterpreters/issues/62...
- 7y ago
- tus88 7y ago> Adding a special VM instruction for such operations as inheriting from a class is a strikingly bad design decision I am struggling how this can be seen as a runtime instruction anyway - surely inheritance is a compile time semantic?
- ghusbands 7y agoFor statically-typed languages, yes, but many dynamically-typed languages allow class definition and inheritance at run-time. Read the classes and instances chapter to see that this interpreter indeed does.
- matheusmoreira 7y agoCompletely agree. Instruction sets are very inflexible. If a language has a new keyword or instruction, developers will spend a lot of time avoiding it at all costs and hiding it behind methods. The new keyword requires a concrete class to instantiate. Abstractions such as interfaces cannot be used with new. The resulting instruction will directly reference that concrete class which makes it part of the software's binary interface. Its behavior can't be overridden: a constructor will always return an instance of the constructed class, never a subtype. The factory design pattern was invented to solve these problems. Replacing new with normal method calls pretty much takes care of everything. The author himself has written about this on his blog: http://journal.stuffwithstuff.com/2010/09/18/futureproofing-uniform-access-and-masquerades/ http://journal.stuffwithstuff.com/2010/09/18/futureproofing-... http://journal.stuffwithstuff.com/2010/12/14/the-trouble-with-constructors/ http://journal.stuffwithstuff.com/2010/12/14/the-trouble-wit...
- rs23296008n1 7y agoSharp edged words. High performance code only exists when you've developed experience from building low performance code. I think a few explanatory notes and discussion of suitable alternative approaches is warranted but not a complete rewrite because its somehow wrong. Each VM has its own quirks which produce their own opportunities. Why would this one be any different? Especially as a teaching instrument where it has to be simpler / clearer than a production codebase?