Skip to content

Repository files navigation

NuGet

FastFibCode

Ultra-fast Fibonacci encoding/decoding methods. The implementation is based on the Fast Fibonacci Encoding Algorithm and the The Fast Fibonacci Decompression Algorithm, with additional optimizations to achieve even better performance. It is 10–30 times faster than the conventional algorithm (depending on the value distribution).

Basic usage

Note

All values are internally incremented during encoding and decremented during decoding to allow the representation of zero. The maximum supported value is $2^{64}-2$.

usingstaticFastFibCode.Fibonacci;UInt128codeForULong=EncodeULong(0x0102030405060708);ulongcodeForUInt=EncodeUInt(0x01020304);uintcodeForShort=EncodeUShort(0x0102);ushortcodeForByte=EncodeByte(0x01);MyFavoriteBitWriterbw= ...;// assume lower bits are written firstbw.WriteBits(codeForULong,(int)UInt128.Log2(codeForULong)+1);bw.WriteBit(1);// "delimiter"bw.WriteBits(codeForUInt,BitOperations.Log2(codeForUInt)+1);bw.WriteBit(1);bw.WriteBits(codeForShort,BitOperations.Log2(codeForShort)+1);bw.WriteBit(1);bw.WriteBits(codeForByte,BitOperations.Log2(codeForByte)+1);bw.WriteBit(1);// =============================================================MyFavoriteBitReaderbr= ...;UInt128code=br.ReadUpToDoubleOnes();// with final '11'code^=UInt128.One<<(int)UInt128.Log2(codeForULong);// clear the delimiter '1' bitulongvalue=DecodeAsULong(code);// ...

Benchmark

A comparison with conventional encoding/decoding algorithms was performed for various value distributions: Folded Normal (absolute value of a normal distribution), Exponential distribution, and Uniform distribution. Each test processed an array of $10^6$ random values (or their Fibonacci codes) with the given distribution and standard deviation (shown as suffixes in the Distribution column). For the Uniform distribution, values were generated in the range $0 .. 5 \times 10^6$.

The conventional algorithms used for comparison
ulongEncodeUInt(uintvalue,boolincrement){ulongv=value;if(increment)v++;ulongresult=0;varlog2=BitOperations.Log2(v);varmaxIndex=log2+(log2>>1)+1;maxIndex=Math.Min(maxIndex,45);for(inti=maxIndex;i>=0;i--){if(v==0)returnresult;varf=(uint)F[i];if(v>=f){result|=1UL<<i;v-=f;i--;}}returnresult;}uintDecodeAsUInt(ulongcode,booldecrement){uintresult=0;varmaxIndex=BitOperations.Log2(code)+1;maxIndex=Math.Min(maxIndex,45);for(inti=0;i<=maxIndex;i++){varf=(uint)F[i];if((code&(1UL<<i))!=0){result+=f;i++;}}if(decrement)result--;returnresult;}
Encoding benchmark results
Intel Core i7-6700HQ CPU 2.60GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
.NET SDK 9.0.302
[Host] : .NET 9.0.7 (9.0.725.31616), X64 RyuJIT AVX2
Job-ZJFXOJ : .NET 9.0.7 (9.0.725.31616), X64 RyuJIT AVX2
MethodDistributionMeanErrorStdDevMedianRatioRatioSD
EncodeConventionalFoldedNormal_10039.102 ms0.7629 ms0.8163 ms38.901 ms1.000.03
EncodeFastFoldedNormal_1001.542 ms0.0557 ms0.1607 ms1.489 ms0.040.00
EncodeConventionalFoldedNormal_1K54.080 ms1.0371 ms1.2346 ms53.907 ms1.000.03
EncodeFastFoldedNormal_1K2.906 ms0.0506 ms0.0846 ms2.902 ms0.050.00
EncodeConventionalFoldedNormal_10K69.528 ms1.2526 ms1.1716 ms69.309 ms1.000.02
EncodeFastFoldedNormal_10K4.413 ms0.0856 ms0.1083 ms4.424 ms0.060.00
EncodeConventionalFoldedNormal_100K84.035 ms1.5328 ms1.5055 ms84.026 ms1.000.02
EncodeFastFoldedNormal_100K3.906 ms0.0746 ms0.0944 ms3.898 ms0.050.00
EncodeConventionalFoldedNormal_1M98.920 ms1.8442 ms1.7251 ms98.421 ms1.000.02
EncodeFastFoldedNormal_1M3.941 ms0.0776 ms0.0924 ms3.958 ms0.040.00
EncodeConventionalFoldedNormal_10M114.072 ms1.8719 ms1.7510 ms114.155 ms1.000.02
EncodeFastFoldedNormal_10M8.203 ms0.1287 ms0.1264 ms8.207 ms0.070.00
EncodeConventionalExponential_10035.178 ms0.6949 ms1.1611 ms34.747 ms1.000.05
EncodeFastExponential_1001.537 ms0.0385 ms0.1092 ms1.509 ms0.040.00
EncodeConventionalExponential_1K51.469 ms0.9935 ms1.0203 ms51.420 ms1.000.03
EncodeFastExponential_1K2.369 ms0.0456 ms0.0856 ms2.371 ms0.050.00
EncodeConventionalExponential_10K65.755 ms1.1349 ms1.0616 ms65.705 ms1.000.02
EncodeFastExponential_10K5.003 ms0.0986 ms0.1211 ms4.995 ms0.080.00
EncodeConventionalExponential_100K80.600 ms1.2722 ms1.1900 ms80.906 ms1.000.02
EncodeFastExponential_100K4.007 ms0.0782 ms0.0931 ms4.004 ms0.050.00
EncodeConventionalExponential_1M95.661 ms1.5023 ms1.3317 ms95.957 ms1.000.02
EncodeFastExponential_1M3.930 ms0.0757 ms0.1086 ms3.924 ms0.040.00
EncodeConventionalExponential_10M110.287 ms1.8801 ms1.7587 ms110.172 ms1.000.02
EncodeFastExponential_10M8.518 ms0.1262 ms0.1596 ms8.490 ms0.080.00
EncodeConventionalUniform_5M103.441 ms1.3436 ms1.2568 ms103.713 ms1.000.02
EncodeFastUniform_5M3.917 ms0.0766 ms0.1321 ms3.908 ms0.040.00
Decoding benchmark results
Intel Core i7-6700HQ CPU 2.60GHz (Skylake), 1 CPU, 8 logical and 4 physical cores
.NET SDK 9.0.302
[Host] : .NET 9.0.7 (9.0.725.31616), X64 RyuJIT AVX2
Job-ZJFXOJ : .NET 9.0.7 (9.0.725.31616), X64 RyuJIT AVX2
MethodDistributionMeanErrorStdDevRatioRatioSD
DecodeConventionalFoldedNormal_10035.279 ms0.3204 ms0.2676 ms1.000.01
DecodeFastFoldedNormal_1001.813 ms0.0450 ms0.1319 ms0.050.00
DecodeConventionalFoldedNormal_1K48.935 ms0.9005 ms0.7520 ms1.000.02
DecodeFastFoldedNormal_1K3.008 ms0.0599 ms0.0531 ms0.060.00
DecodeConventionalFoldedNormal_10K61.887 ms1.1508 ms1.1302 ms1.000.03
DecodeFastFoldedNormal_10K3.595 ms0.0717 ms0.1095 ms0.060.00
DecodeConventionalFoldedNormal_100K74.222 ms1.4658 ms1.4396 ms1.000.03
DecodeFastFoldedNormal_100K2.686 ms0.0528 ms0.1191 ms0.040.00
DecodeConventionalFoldedNormal_1M86.271 ms1.3631 ms1.2750 ms1.000.02
DecodeFastFoldedNormal_1M2.574 ms0.0510 ms0.0893 ms0.030.00
DecodeConventionalFoldedNormal_10M99.897 ms1.5131 ms1.3413 ms1.000.02
DecodeFastFoldedNormal_10M5.395 ms0.1069 ms0.1664 ms0.050.00
DecodeConventionalExponential_10031.101 ms0.6039 ms0.7637 ms1.000.03
DecodeFastExponential_1001.773 ms0.0384 ms0.1127 ms0.060.00
DecodeConventionalExponential_1K46.007 ms0.9017 ms0.9260 ms1.000.03
DecodeFastExponential_1K2.551 ms0.0508 ms0.1135 ms0.060.00
DecodeConventionalExponential_10K59.651 ms0.9695 ms0.8595 ms1.000.02
DecodeFastExponential_10K4.371 ms0.0815 ms0.0800 ms0.070.00
DecodeConventionalExponential_100K72.305 ms0.9786 ms0.7640 ms1.000.01
DecodeFastExponential_100K2.782 ms0.0553 ms0.0925 ms0.040.00
DecodeConventionalExponential_1M85.477 ms1.5396 ms1.4402 ms1.000.02
DecodeFastExponential_1M2.621 ms0.0514 ms0.0815 ms0.030.00
DecodeConventionalExponential_10M96.975 ms1.9232 ms1.7990 ms1.000.03
DecodeFastExponential_10M6.630 ms0.1161 ms0.1192 ms0.070.00
DecodeConventionalUniform_5M91.944 ms1.7965 ms2.2063 ms1.000.03
DecodeFastUniform_5M2.635 ms0.0519 ms0.0881 ms0.030.00

About

Ultra-fast Fibonacci encoding/decoding

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Contributors

Languages