Closed dzmitry-lahoda closed 5 years ago
Sorry, I don't understand the relevance of the link. Is there a pre-computed table of fibs in a standard library somewhere?
I want my game (or any app - e.g. edge device) to start fast.
So I create script to generate fib numbers
dotnet script
manually or via https://github.com/daveaglick/Scripty on build (reade C++ constexpr).So we have faster app start.
Than I use next optimization to avoid array alloc on start (const expression):
// C# no-alloc optimization that directly wraps the data section of the dll (similar to string constants)
// https://github.com/dotnet/roslyn/pull/24621
private static ReadOnlySpan<byte> s_TrailingZeroCountDeBruijn => new byte[32]
{
};
Array may even have optimal layout in this case for lookup-fetch because of better layout. Interesting point to test.
But it look like the patter to have lookup tables in C# now.
Ah right. I've pre-computed the table and popped it in code. The no-alloc approach is interesting, but gets a little tricky with endianness and may kill any performance advantage.
stackalloc would work for noalloc either, for endianness there would be condition checked and 2 routes followed (seems i have seen such code in corefx)
https://github.com/invertedtomato/integer-compression/blob/8b0b80f1e74340b90cc9726c1bf944dbca93b24d/Library/Compression/Integers/Wave3/FibonacciCodec.cs#L26
These numbers are eternal.
https://github.com/dotnet/corefx/blob/release/3.0/src/Common/src/CoreLib/System/Numerics/BitOperations.cs#L26