static-generics: Zero cost generic statics for Rust
Hi all! I have been using cynecx/generic-statics for a while in my allocator code to compress vtable pointers efficiently per T into 16 bits for a while, and the thing worked flawlessly, but had one downside: it was not truly zero-cost as it had to perform a call on machine level to actually fetch the generic statics, and it turned out to be a real hotspot (based on profiling and seeing that uncompressed vtables aka full pointers were faster). So I came up with a crate which is based on api of generic-statics, but implements the whole thing differently: static-generics.
It gives you truly zero cost generic statics (it uses only 1 instruction on x64) and the access is inlined. The crate also extended platform support:
- x86_64
- aarch64
- x86_32
- arm (Thumb as well)
- riscv32/riscv64
- loongarc32/loongarch64
- powerpc/powerpc64
- s390x
How?
It works by using .comm directive of inline assembly which allocates zeroed section in object files and also the monomorphization of said section is guaranteed by unique function which uses T generic and TypeId::of::<T>() so LLVM does not dedup the function bodies and thus break the code.
Please, anyone who loves dark arts of unsafe code and asm, looking for a feedback!