Skip to content

Commit 0e64e16

Browse files
[perf] replace Range.foreach with while loop (#273)
1 parent ac2f3a4 commit 0e64e16

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

modules/flink-common-api/src/main/scala/org/apache/flinkx/api/serializer/CaseClassSerializer.scala

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,13 @@ class CaseClassSerializer[T <: Product](
7171

7272
def createInstance: T =
7373
try {
74-
val fields = (0 until arity).map(i => fieldSerializers(i).createInstance())
75-
createInstance(fields.toArray)
74+
val fields = new Array[AnyRef](arity)
75+
var i = 0
76+
while (i < arity) {
77+
fields(i) = fieldSerializers(i).createInstance()
78+
i += 1
79+
}
80+
createInstance(fields)
7681
} catch {
7782
case t: Throwable =>
7883
log.warn(s"Failed to create an instance returning null", t)
@@ -89,8 +94,13 @@ class CaseClassSerializer[T <: Product](
8994
if (from == null || isImmutableType) {
9095
from
9196
} else {
92-
val fields = (0 until arity).map(i => fieldSerializers(i).copy(from.productElement(i).asInstanceOf[AnyRef]))
93-
createInstance(fields.toArray)
97+
val fields = new Array[AnyRef](arity)
98+
var i = 0
99+
while (i < arity) {
100+
fields(i) = fieldSerializers(i).copy(from.productElement(i).asInstanceOf[AnyRef])
101+
i += 1
102+
}
103+
createInstance(fields)
94104
}
95105

96106
override val getLength: Int = if (super.getLength == -1) -1 else super.getLength + 4 // +4 bytes for the arity field
@@ -101,14 +111,16 @@ class CaseClassSerializer[T <: Product](
101111
target.writeInt(sourceArity)
102112
if (value == null) target.write(nullPadding)
103113

104-
(0 until sourceArity).foreach { i =>
114+
var i = 0
115+
while (i < sourceArity) {
105116
val serializer = fieldSerializers(i).asInstanceOf[TypeSerializer[Any]]
106117
val o = value.productElement(i)
107118
try serializer.serialize(o, target)
108119
catch {
109120
case e: NullPointerException =>
110121
throw new NullFieldException(i, e)
111122
}
123+
i += 1
112124
}
113125
}
114126

0 commit comments

Comments
 (0)