invertedtomato / packing

Library for encoding integers in the minimal number of bits. Includes VLQ and Elias Omega encoding.
MIT License
76 stars 6 forks source link

Inline lookup #6

Closed dzmitry-lahoda closed 5 years ago

dzmitry-lahoda commented 5 years ago

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

invertedtomato commented 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?

dzmitry-lahoda commented 5 years ago

I want my game (or any app - e.g. edge device) to start fast.

So I create script to generate fib numbers

  1. https://github.com/dzmitry-lahoda/NetStack/blob/master/Assets/Scripts/src/NetStack.Serialization/generator.csx
  2. run it via dotnet script manually or via https://github.com/daveaglick/Scripty on build (reade C++ constexpr).

So we have faster app start.

  1. 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.

invertedtomato commented 5 years ago

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.

dzmitry-lahoda commented 5 years ago

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)