-
Notifications
You must be signed in to change notification settings - Fork 29k
Added a FastByteArrayOutputStream that exposes the underlying array to avoid unnecessary mem copy. #397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Added a FastByteArrayOutputStream that exposes the underlying array to avoid unnecessary mem copy. #397
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
core/src/main/scala/org/apache/spark/util/io/FastByteArrayOutputStream.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.util.io | ||
|
|
||
| import java.io.OutputStream | ||
| import java.nio.ByteBuffer | ||
|
|
||
| /** | ||
| * A simple, fast byte-array output stream that exposes the backing array, | ||
| * inspired by fastutil's FastByteArrayOutputStream. | ||
| * | ||
| * [[java.io.ByteArrayOutputStream]] is nice, but to get its content you | ||
| * must generate each time a new object. This doesn't happen here. | ||
| * | ||
| * This class will automatically enlarge the backing array, doubling its | ||
| * size whenever new space is needed. | ||
| */ | ||
| private[spark] class FastByteArrayOutputStream(initialCapacity: Int = 16) extends OutputStream { | ||
|
|
||
| private[this] var _array = new Array[Byte](initialCapacity) | ||
|
|
||
| /** The current writing position. */ | ||
| private[this] var _position: Int = 0 | ||
|
|
||
| /** The number of valid bytes in array. */ | ||
| def length: Int = _position | ||
|
|
||
| override def write(b: Int): Unit = { | ||
| if (_position >= _array.length ) { | ||
| _array = FastByteArrayOutputStream.growArray(_array, _position + 1, _position) | ||
| } | ||
| _array(_position) = b.toByte | ||
| _position += 1 | ||
| } | ||
|
|
||
| override def write(b: Array[Byte], off: Int, len: Int) { | ||
| if (off < 0) { | ||
| throw new ArrayIndexOutOfBoundsException(s"Offset ($off) is negative" ) | ||
| } | ||
| if (len < 0) { | ||
| throw new IllegalArgumentException(s"Length ($len) is negative" ) | ||
| } | ||
| if (off + len > b.length) { | ||
| throw new ArrayIndexOutOfBoundsException( | ||
| s"Last index (${off + len}) is greater than array length (${b.length})") | ||
| } | ||
| if ( _position + len > _array.length ) { | ||
| _array = FastByteArrayOutputStream.growArray(_array, _position + len, _position) | ||
| } | ||
| System.arraycopy(b, off, _array, _position, len) | ||
| _position += len | ||
| } | ||
|
|
||
| /** Return a ByteBuffer wrapping around the filled content of the underlying array. */ | ||
| def toByteBuffer: ByteBuffer = { | ||
| ByteBuffer.wrap(_array, 0, _position) | ||
| } | ||
|
|
||
| /** | ||
| * Return a tuple, where the first element is the underlying array, and the second element | ||
| * is the length of the filled content. | ||
| */ | ||
| def toArray: (Array[Byte], Int) = (_array, _position) | ||
|
|
||
| /** Ensures that the length of the backing array is equal to [[length]]. */ | ||
| def trim(): this.type = { | ||
| if (_position < _array.length) { | ||
| val newArr = new Array[Byte](_position) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This still entails a copy, right? I don't see that this improves the situation by itself. |
||
| System.arraycopy(_array, 0, newArr, 0, _position) | ||
| _array = newArr | ||
| } | ||
| this | ||
| } | ||
| } | ||
|
|
||
| private object FastByteArrayOutputStream { | ||
| /** | ||
| * Grows the given array to the maximum between the given length and the current length | ||
| * multiplied by two, provided that the given length is larger than the current length, | ||
| * preserving just a part of the array. | ||
| * | ||
| * @param arr input array | ||
| * @param len the new minimum length for this array | ||
| * @param preserve the number of elements of the array that must be preserved | ||
| * in case a new allocation is necessary | ||
| */ | ||
| private def growArray(arr: Array[Byte], len: Int, preserve: Int): Array[Byte] = { | ||
| if (len > arr.length) { | ||
| val maxArraySize = Integer.MAX_VALUE - 8 | ||
| val newLen = math.min( math.max(2L * arr.length, len), maxArraySize).toInt | ||
| val newArr = new Array[Byte](newLen) | ||
| System.arraycopy(arr, 0, newArr, 0, preserve) | ||
| newArr | ||
| } else { | ||
| arr | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
core/src/test/scala/org/apache/spark/util/io/FastByteArrayOutputStreamSuite.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.apache.spark.util.io | ||
|
|
||
| import org.scalatest.FunSuite | ||
|
|
||
|
|
||
| class FastByteArrayOutputStreamSuite extends FunSuite { | ||
|
|
||
| test("write single byte") { | ||
| val out = new FastByteArrayOutputStream(initialCapacity = 4) | ||
| out.write(0) | ||
| out.write(1) | ||
| assert(out.toArray._1(0) === 0) | ||
| assert(out.toArray._1(1) === 1) | ||
| assert(out.toArray._2 === 2) | ||
| assert(out.length === 2) | ||
|
|
||
| out.write(2) | ||
| out.write(3) | ||
| assert(out.toArray._1(2) === 2) | ||
| assert(out.toArray._1(3) === 3) | ||
| assert(out.length === 4) | ||
|
|
||
| out.write(4) | ||
| assert(out.toArray._1(4) === 4) | ||
| assert(out.toArray._2 === 5) | ||
| assert(out.length === 5) | ||
|
|
||
| for (i <- 5 to 100) { | ||
| out.write(i) | ||
| } | ||
|
|
||
| for (i <- 5 to 100) { | ||
| assert(out.toArray._1(i) === i) | ||
| } | ||
| } | ||
|
|
||
| test("write multiple bytes") { | ||
| val out = new FastByteArrayOutputStream(initialCapacity = 4) | ||
| out.write(Array[Byte](0.toByte, 1.toByte)) | ||
| assert(out.length === 2) | ||
| assert(out.toArray._1(0) === 0) | ||
| assert(out.toArray._1(1) === 1) | ||
|
|
||
| out.write(Array[Byte](2.toByte, 3.toByte, 4.toByte)) | ||
| assert(out.length === 5) | ||
| assert(out.toArray._1(2) === 2) | ||
| assert(out.toArray._1(3) === 3) | ||
| assert(out.toArray._1(4) === 4) | ||
|
|
||
| // Write more than double the size of the current array | ||
| out.write((1 to 100).map(_.toByte).toArray) | ||
| assert(out.length === 105) | ||
| assert(out.toArray._1(104) === 100) | ||
| } | ||
|
|
||
| test("test large writes") { | ||
| val out = new FastByteArrayOutputStream(initialCapacity = 4096) | ||
| out.write(Array.tabulate[Byte](4096 * 1000)(_.toByte)) | ||
| assert(out.length === 4096 * 1000) | ||
| assert(out.toArray._1(0) === 0) | ||
| assert(out.toArray._1(4096 * 1000 - 1) === (4096 * 1000 - 1).toByte) | ||
| assert(out.toArray._2 === 4096 * 1000) | ||
|
|
||
| out.write(Array.tabulate[Byte](4096 * 1000)(_.toByte)) | ||
| assert(out.length === 2 * 4096 * 1000) | ||
| assert(out.toArray._1(0) === 0) | ||
| assert(out.toArray._1(4096 * 1000) === 0) | ||
| assert(out.toArray._1(2 * 4096 * 1000 - 1) === (4096 * 1000 - 1).toByte) | ||
| assert(out.toArray._2 === 2 * 4096 * 1000) | ||
| } | ||
|
|
||
| test("trim") { | ||
| val out = new FastByteArrayOutputStream(initialCapacity = 4096) | ||
| out.write(1) | ||
| assert(out.trim().toArray._2 === 1) | ||
|
|
||
| val out1 = new FastByteArrayOutputStream(initialCapacity = 1) | ||
| out1.write(1) | ||
| assert(out1.trim().toArray._2 === 1) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We probably don't need this method, as it eliminates the whole purpose of this stream. You might as well use ByteArrayOutputStream if you need trim().