4 ms·
If you pass an object into a function, the code has to work with any subclass. This means the data has to be behind a pointer since the size is unknown and meth
by Tarean 6y ago
If you pass an object into a function, the code has to work with any subclass. This means the data has to be behind a pointer since the size is unknown and method calls have to go through a vtable. If you can see the full lifetime of an object you can specialize away until no pointers are left but in the general case this doesn't work, or makes performance worse because you unbox and rebox repeatedly whenever you call methods.
For final classes unpacking into registers might work, but at least in the jvm you can subclass final classes via reflection. If I understand correctly this avoids repacking when calling methods that expect the normal memory layout by actually allocating the the normal object layout - just on the heap.
- MrBuddyCasino 6y agoMakes sense, thanks for the explanation.