3 ms·
What's the technique to unbox closures and ADTs?
by jules 4y ago
What's the technique to unbox closures and ADTs?
- titzer 4y agoYou gotta use multiple (machine) words to represent them. The ADT can't be recursive and the closure environment needs to be small enough. E.g. for delegates, the closure environment is just the receiver object, so using a fat pointer of two words (one code pointer, one object pointer) is sufficient.
- jules 4y agoAh I see, so you represent all closures as two machine words, in a representation that the GC can understand?
- titzer 4y agoYes, all closures are represented as a reference word (traced by the GC) and a non-reference word[1], which is a pointer to the actual function entry point (i.e. code). For delegates (i.e. methods bound to an object), the reference is just the object, and the function pointer is the actual code (looked up through the v-table once). So that actually means a call to a first-class function is cheaper than a virtual call--it is literally an indirect call to the code pointer, passing the receiver object as the first argument. [1] The two words even become separate, independent scalar values in the compiler IR. Thus the runtime support is minimal, in that it doesn't know anything about multiple-word values: the GC just needs to know whether a word is a reference or not.