1- // Copyright (C) 2003-2012 Xtensive LLC.
1+ // Copyright (C) 2003-2012 Xtensive LLC.
22// All rights reserved.
33// For conditions of distribution and use, see license.
44// Created by: Denis Krjuchkov
@@ -16,27 +16,20 @@ internal static class TupleLayout
1616 private const int Val032Rank = 5 ;
1717 private const int Val016Rank = 4 ;
1818 private const int Val008Rank = 3 ;
19- private const int Val001Rank = 0 ;
2019
2120 private const int Val064BitCount = 1 << Val064Rank ;
2221 private const int Val032BitCount = 1 << Val032Rank ;
2322 private const int Modulo064RemainderMask = Val064BitCount - 1 ;
2423 private const int Modulo032RemainderMask = Val032BitCount - 1 ;
2524
26- private ref struct ValPointer
27- {
28- public int Index ;
29- public int Offset ;
30- }
31-
3225 private ref struct ValPointers
3326 {
34- public ValPointer Val001Pointer ;
35- public ValPointer Val008Pointer ;
36- public ValPointer Val016Pointer ;
37- public ValPointer Val032Pointer ;
38- public ValPointer Val064Pointer ;
39- public ValPointer Val128Pointer ;
27+ public int Val001Pointer ;
28+ public int Val008Pointer ;
29+ public int Val016Pointer ;
30+ public int Val032Pointer ;
31+ public int Val064Pointer ;
32+ public int Val128Pointer ;
4033 }
4134
4235 private ref struct ValCounters
@@ -197,7 +190,7 @@ public static void ConfigureLen2(Type[] fieldTypes, ref PackedFieldDescriptor de
197190 descriptor1 . SetValueBitOffset ( Val064BitCount ) ;
198191 val1BitCount = descriptor1 . Accessor . ValueBitCount ;
199192 }
200- valuesLength = 1 + ( ( val1BitCount + ( Val064BitCount - 1 ) ) >> Val064Rank ) ;
193+ valuesLength = ( val1BitCount + ( ( Val064BitCount * 2 ) - 1 ) ) >> Val064Rank ;
201194 return ;
202195 }
203196 }
@@ -219,53 +212,56 @@ public static void ConfigureLen2(Type[] fieldTypes, ref PackedFieldDescriptor de
219212 public static void Configure ( Type [ ] fieldTypes , PackedFieldDescriptor [ ] fieldDescriptors , out int valuesLength ,
220213 out int objectsLength )
221214 {
222- var valCounters = new ValCounters ( ) ;
223215 var fieldCount = fieldTypes . Length ;
216+ const int statesPerLong = Val064BitCount / 2 ;
217+ var stateBitCount = ( ( fieldCount + ( statesPerLong - 1 ) ) >> Val032Rank ) << Val064Rank ;
218+
219+ var valCounters = new ValCounters ( ) ;
224220 for ( var fieldIndex = 0 ; fieldIndex < fieldCount ; fieldIndex ++ ) {
225221 ConfigureFieldPhase1 ( ref fieldDescriptors [ fieldIndex ] , ref valCounters , fieldTypes , fieldIndex ) ;
226222 }
227223
228- const int stateBitCount = 2 ;
229- const int statesPerLong = Val064BitCount / stateBitCount ;
230-
231- var vPointers = new ValPointers {
232- Val128Pointer = new ValPointer { Index = ( fieldCount + ( statesPerLong - 1 ) ) >> Val032Rank }
233- } ;
234- InitValPointer ( ref vPointers . Val064Pointer , ref vPointers . Val128Pointer , valCounters . Val128Counter , Val128Rank ) ;
235- InitValPointer ( ref vPointers . Val032Pointer , ref vPointers . Val064Pointer , valCounters . Val064Counter , Val064Rank ) ;
236- InitValPointer ( ref vPointers . Val016Pointer , ref vPointers . Val032Pointer , valCounters . Val032Counter , Val032Rank ) ;
237- InitValPointer ( ref vPointers . Val008Pointer , ref vPointers . Val016Pointer , valCounters . Val016Counter , Val016Rank ) ;
238- InitValPointer ( ref vPointers . Val001Pointer , ref vPointers . Val008Pointer , valCounters . Val008Counter , Val008Rank ) ;
239-
240- var valuesEndPointer = new ValPointer ( ) ;
241- InitValPointer ( ref valuesEndPointer , ref vPointers . Val001Pointer , valCounters . Val001Counter , Val001Rank ) ;
224+ var vPointers = new ValPointers ( ) ;
225+ var totalBitCount = InitValPointers ( stateBitCount , ref valCounters , ref vPointers ) ;
242226
243227 for ( var fieldIndex = 0 ; fieldIndex < fieldCount ; fieldIndex ++ ) {
244228 ConfigureFieldPhase2 ( ref fieldDescriptors [ fieldIndex ] , ref vPointers ) ;
245229 }
246230
247- valuesLength = valuesEndPointer . Index + ( ( valuesEndPointer . Offset + ( Val064BitCount - 1 ) ) >> Val064Rank ) ;
231+ valuesLength = ( totalBitCount + ( Val064BitCount - 1 ) ) >> Val064Rank ;
248232 objectsLength = valCounters . ObjectCounter ;
249233 }
250234
251235 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
252- private static void InitValPointer ( ref ValPointer pointer , ref ValPointer prevPointer , int prevValueCount ,
253- int prevRank )
236+ private static int InitValPointers ( int stateBitCount , ref ValCounters valCounters , ref ValPointers vPointers )
254237 {
255- var prevBitCountWithOffset = ( prevValueCount << prevRank ) + prevPointer . Offset ;
238+ var totalBitCount = stateBitCount ;
239+ vPointers . Val128Pointer = totalBitCount ;
240+
241+ totalBitCount += valCounters . Val128Counter << Val128Rank ;
242+ vPointers . Val064Pointer = totalBitCount ;
243+
244+ totalBitCount += valCounters . Val064Counter << Val064Rank ;
245+ vPointers . Val032Pointer = totalBitCount ;
246+
247+ totalBitCount += valCounters . Val032Counter << Val032Rank ;
248+ vPointers . Val016Pointer = totalBitCount ;
249+
250+ totalBitCount += valCounters . Val016Counter << Val016Rank ;
251+ vPointers . Val008Pointer = totalBitCount ;
252+
253+ totalBitCount += valCounters . Val008Counter << Val008Rank ;
254+ vPointers . Val001Pointer = totalBitCount ;
256255
257- pointer . Index = prevPointer . Index + ( prevBitCountWithOffset >> Val064Rank ) ;
258- pointer . Offset = prevBitCountWithOffset & Modulo064RemainderMask ;
256+ totalBitCount += valCounters . Val001Counter ;
257+ return totalBitCount ;
259258 }
260259
261260 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
262- private static void UpdateDescriptorPosition ( ref PackedFieldDescriptor descriptor , ref ValPointer valPointer )
261+ private static void UpdateDescriptorPosition ( ref PackedFieldDescriptor descriptor , ref int valPointer )
263262 {
264- var totalBitCount = ( valPointer . Index << Val064Rank ) + valPointer . Offset ;
265- descriptor . SetValueBitOffset ( totalBitCount ) ;
266- totalBitCount += descriptor . Accessor . ValueBitCount ;
267- valPointer . Index = totalBitCount >> Val064Rank ;
268- valPointer . Offset = totalBitCount & Modulo064RemainderMask ;
263+ descriptor . SetValueBitOffset ( valPointer ) ;
264+ valPointer += descriptor . Accessor . ValueBitCount ;
269265 }
270266
271267 [ MethodImpl ( MethodImplOptions . AggressiveInlining ) ]
0 commit comments