Skip to content

Commit f92ffa6

Browse files
author
Dubovsky Jakub
committed
performance tuning
1 parent 480f9cd commit f92ffa6

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

core/src/main/scala/org/apache/spark/SparkContext.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package org.apache.spark
1919

20+
import java.util.Arrays
21+
2022
import scala.language.implicitConversions
2123

2224
import java.io._
@@ -1409,7 +1411,7 @@ object SparkContext extends Logging {
14091411
simpleWritableConverter[Boolean, BooleanWritable](_.get)
14101412

14111413
implicit def bytesWritableConverter(): WritableConverter[Array[Byte]] = {
1412-
simpleWritableConverter[Array[Byte], BytesWritable](bw => bw.getBytes.take(bw.getLength))
1414+
simpleWritableConverter[Array[Byte], BytesWritable](bw => Arrays.copyOfRange(bw.getBytes, 0, bw.getLength))
14131415
}
14141416

14151417
implicit def stringWritableConverter(): WritableConverter[String] =

core/src/test/scala/org/apache/spark/SparkContextSuite.scala

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ import org.apache.hadoop.io.BytesWritable
2424
class SparkContextSuite extends FunSuite {
2525
test("test of writing spark scala test") {
2626
val bytesWritable = new BytesWritable()
27-
bytesWritable.set((1 to 10).map(_.toByte).toArray, 0, 10)
28-
bytesWritable.set((1 to 5).map(_.toByte).toArray, 0, 5)
27+
val inputArray = (1 to 10).map(_.toByte).toArray
28+
bytesWritable.set(inputArray, 0, 10)
29+
bytesWritable.set(inputArray, 0, 5)
2930

3031
val converter = SparkContext.bytesWritableConverter()
3132
val byteArray = converter.convert(bytesWritable)
3233
assert(byteArray.length === 5)
34+
35+
bytesWritable.set(inputArray, 0, 0)
36+
val byteArray2 = converter.convert(bytesWritable)
37+
assert(byteArray2.length === 0)
3338
}
3439
}

0 commit comments

Comments
 (0)