@@ -36,10 +36,6 @@ internal sealed unsafe class DeflaterHuffman : IDisposable
3636
3737 private const int EofSymbol = 256 ;
3838
39- // The lengths of the bit length codes are sent in order of decreasing
40- // probability, to avoid transmitting the lengths for unused bit length codes.
41- private static readonly int [ ] BitLengthOrder = { 16 , 17 , 18 , 0 , 8 , 7 , 9 , 6 , 10 , 5 , 11 , 4 , 12 , 3 , 13 , 2 , 14 , 1 , 15 } ;
42-
4339 private static readonly short [ ] StaticLCodes ;
4440 private static readonly byte [ ] StaticLLength ;
4541 private static readonly short [ ] StaticDCodes ;
@@ -126,6 +122,11 @@ public DeflaterHuffman(MemoryAllocator memoryAllocator)
126122 this . pinnedLiteralBuffer = ( short * ) this . literalBufferHandle . Pointer ;
127123 }
128124
125+ /// <summary>
126+ /// Gets the lengths of the bit length codes are sent in order of decreasing probability, to avoid transmitting the lengths for unused bit length codes.
127+ /// </summary>
128+ private static ReadOnlySpan < byte > BitLengthOrder => new byte [ ] { 16 , 17 , 18 , 0 , 8 , 7 , 9 , 6 , 10 , 5 , 11 , 4 , 12 , 3 , 13 , 2 , 14 , 1 , 15 } ;
129+
129130 private static ReadOnlySpan < byte > Bit4Reverse => new byte [ ] { 0 , 8 , 4 , 12 , 2 , 10 , 6 , 14 , 1 , 9 , 5 , 13 , 3 , 11 , 7 , 15 } ;
130131
131132 /// <summary>
@@ -158,9 +159,14 @@ public void SendAllTrees(int blTreeCodes)
158159 this . Pending . WriteBits ( this . literalTree . NumCodes - 257 , 5 ) ;
159160 this . Pending . WriteBits ( this . distTree . NumCodes - 1 , 5 ) ;
160161 this . Pending . WriteBits ( blTreeCodes - 4 , 4 ) ;
162+
163+ ref byte bitLengthOrderRef = ref MemoryMarshal . GetReference ( BitLengthOrder ) ;
164+
161165 for ( int rank = 0 ; rank < blTreeCodes ; rank ++ )
162166 {
163- this . Pending . WriteBits ( this . blTree . Length [ BitLengthOrder [ rank ] ] , 3 ) ;
167+ ref byte bitsRef = ref Unsafe . Add ( ref bitLengthOrderRef , rank ) ;
168+
169+ this . Pending . WriteBits ( this . blTree . Length [ bitsRef ] , 3 ) ;
164170 }
165171
166172 this . literalTree . WriteTree ( this . Pending , this . blTree ) ;
@@ -249,10 +255,14 @@ public void FlushBlock(byte[] stored, int storedOffset, int storedLength, bool l
249255 // Build bitlen tree
250256 this . blTree . BuildTree ( ) ;
251257
258+ ref byte bitLengthOrderRef = ref MemoryMarshal . GetReference ( BitLengthOrder ) ;
252259 int blTreeCodes = 4 ;
260+
253261 for ( int i = 18 ; i > blTreeCodes ; i -- )
254262 {
255- if ( this . blTree . Length [ BitLengthOrder [ i ] ] > 0 )
263+ ref byte bits = ref Unsafe . Add ( ref bitLengthOrderRef , i ) ;
264+
265+ if ( this . blTree . Length [ bits ] > 0 )
256266 {
257267 blTreeCodes = i + 1 ;
258268 }
0 commit comments