@@ -24,8 +24,10 @@ class PackedEncoder(
2424 // Bitmask arrays for CLASSES only
2525 private var booleanIndices: IntArray = intArrayOf()
2626 private lateinit var booleanValues: BooleanArray
27+ private var booleanLookup: IntArray = intArrayOf() // fieldIndex → bitmask position, or -1
2728 private var nullableIndices: IntArray = intArrayOf()
2829 private lateinit var nullValues: BooleanArray
30+ private var nullableLookup: IntArray = intArrayOf() // fieldIndex → bitmask position, or -1
2931
3032 // Buffer for field data
3133 private val dataBuffer = ByteArrayOutputStream ()
@@ -69,18 +71,24 @@ class PackedEncoder(
6971 }
7072 booleanIndices = boolIdx.toIntArray()
7173 booleanValues = BooleanArray (booleanIndices.size)
74+ booleanLookup = IntArray (descriptor.elementsCount) { - 1 }
75+ booleanIndices.forEachIndexed { pos, fieldIdx -> booleanLookup[fieldIdx] = pos }
7276
7377 val nullableIdx = (0 until descriptor.elementsCount).filter {
7478 descriptor.getElementDescriptor(it).isNullable
7579 }
7680 nullableIndices = nullableIdx.toIntArray()
7781 nullValues = BooleanArray (nullableIndices.size)
82+ nullableLookup = IntArray (descriptor.elementsCount) { - 1 }
83+ nullableIndices.forEachIndexed { pos, fieldIdx -> nullableLookup[fieldIdx] = pos }
7884 } else {
7985 // Collections do not use bitmasks
8086 booleanIndices = intArrayOf()
8187 booleanValues = BooleanArray (0 )
88+ booleanLookup = intArrayOf()
8289 nullableIndices = intArrayOf()
8390 nullValues = BooleanArray (0 )
91+ nullableLookup = intArrayOf()
8492 }
8593
8694 dataBuffer.reset()
@@ -181,7 +189,7 @@ class PackedEncoder(
181189
182190 // 1. Collections: No header, just data
183191 if (isCollection) {
184- output.write( dataBuffer.toByteArray() )
192+ dataBuffer.writeTo(output )
185193 inStructure = false
186194 return
187195 }
@@ -201,28 +209,26 @@ class PackedEncoder(
201209 PackedUtils .writeVarInt(flagBytes.size, output)
202210 output.write(flagBytes)
203211 } else {
204- val flagsLong = PackedUtils .packFlagsToLong(* combined)
212+ val flagsLong = PackedUtils .packFlagsToLong(combined)
205213 PackedUtils .writeVarLong(flagsLong, output)
206214 }
207215 }
208216
209- output.write( dataBuffer.toByteArray() )
217+ dataBuffer.writeTo(output )
210218
211219 inStructure = false
212220 currentIndex = - 1
213221 booleanIndices = intArrayOf()
222+ booleanLookup = intArrayOf()
214223 nullableIndices = intArrayOf()
224+ nullableLookup = intArrayOf()
215225 }
216226
217- private fun booleanPos (index : Int ): Int {
218- for (i in booleanIndices.indices) if (booleanIndices[i] == index) return i
219- return - 1
220- }
227+ private fun booleanPos (index : Int ): Int =
228+ if (index < booleanLookup.size) booleanLookup[index] else - 1
221229
222- private fun nullablePos (index : Int ): Int {
223- for (i in nullableIndices.indices) if (nullableIndices[i] == index) return i
224- return - 1
225- }
230+ private fun nullablePos (index : Int ): Int =
231+ if (index < nullableLookup.size) nullableLookup[index] else - 1
226232
227233 override fun encodeBooleanElement (
228234 descriptor : SerialDescriptor ,
@@ -239,28 +245,10 @@ class PackedEncoder(
239245 index : Int ,
240246 value : Int
241247 ) {
242- val anns = descriptor.getElementAnnotations(index)
243- val hasFixedInt = anns.hasFixedInt()
244- val hasVarInt = anns.hasVarInt()
245- val hasVarUInt = anns.hasVarUInt()
246-
247- val isVar = when {
248- hasFixedInt -> false
249- hasVarInt || hasVarUInt -> true
250- else -> config.defaultVarInt || config.defaultZigZag
251- }
252-
253- val zigZag = when {
254- hasVarInt -> true
255- hasVarUInt || hasFixedInt -> false
256- else -> config.defaultZigZag
257- }
258-
259- if (isVar) {
260- val v = if (zigZag) PackedUtils .zigZagEncodeInt(value) else value
261- PackedUtils .writeVarInt(v, dataBuffer)
262- } else {
263- PackedUtils .writeInt(value, dataBuffer)
248+ when (resolveIntEncoding(descriptor.getElementAnnotations(index), config)) {
249+ IntEncoding .ZIGZAG -> PackedUtils .writeVarInt(PackedUtils .zigZagEncodeInt(value), dataBuffer)
250+ IntEncoding .VARINT -> PackedUtils .writeVarInt(value, dataBuffer)
251+ IntEncoding .FIXED -> PackedUtils .writeInt(value, dataBuffer)
264252 }
265253 }
266254
@@ -269,28 +257,10 @@ class PackedEncoder(
269257 index : Int ,
270258 value : Long
271259 ) {
272- val anns = descriptor.getElementAnnotations(index)
273- val hasFixedInt = anns.hasFixedInt()
274- val hasVarInt = anns.hasVarInt()
275- val hasVarUInt = anns.hasVarUInt()
276-
277- val isVar = when {
278- hasFixedInt -> false
279- hasVarInt || hasVarUInt -> true
280- else -> config.defaultVarInt || config.defaultZigZag
281- }
282-
283- val zigZag = when {
284- hasVarInt -> true
285- hasVarUInt || hasFixedInt -> false
286- else -> config.defaultZigZag
287- }
288-
289- if (isVar) {
290- val v = if (zigZag) PackedUtils .zigZagEncodeLong(value) else value
291- PackedUtils .writeVarLong(v, dataBuffer)
292- } else {
293- PackedUtils .writeLong(value, dataBuffer)
260+ when (resolveIntEncoding(descriptor.getElementAnnotations(index), config)) {
261+ IntEncoding .ZIGZAG -> PackedUtils .writeVarLong(PackedUtils .zigZagEncodeLong(value), dataBuffer)
262+ IntEncoding .VARINT -> PackedUtils .writeVarLong(value, dataBuffer)
263+ IntEncoding .FIXED -> PackedUtils .writeLong(value, dataBuffer)
294264 }
295265 }
296266
0 commit comments