by choeger · 2y ago · 0 comments
I wonder if there's a way to efficiently implement it without resorting to monomorphization?
A function that's polymorphic can be transformed into a more primitive (say C or assembly) function that gets extra arguments that carry the "shape" of the type variable (think of sizes, pointers vs. values, etc.). Is there a similar strategy for these polymorphic records?
I see two issues:
1. The offset of any particular field in the record is unknown at compile time
2. The size of the record itself is unknown at compile time (but this should be trivial as an extra argument.)
For instance, if the "prototype" of the argument is {int foo, float bar}, and I supply {int foo, int baz, float bar}, the table will be {foo: base+0 bytes, bar: base+8 bytes}.
Depends on what you consider "efficient".
Monomorphization is necessary for the most efficient code. But you can have a vtable or a restricted type lookup.
That is not always true. Monomorphisation also leads to code size increase, because the function is compiled for each type. This may decrease cache efficiency.
But not always, and won't always increase your performance either.
[0] https://osa1.net/posts/2023-01-23-fast-polymorphic-record-ac...
void f({float x, float y} p);
Becomes
void f(void* p, size_t offsets[2]);