Skip to content

Commit 62935e0

Browse files
committed
Use a reusable ByteBuffer
1 parent 17577fd commit 62935e0

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

src/main/java/com/amazon/ion/bytecode/bin10/ByteArrayBytecodeGenerator10.kt

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import com.amazon.ion.impl.bin.utf8.Utf8StringDecoderPool
2323
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
2424
import java.lang.IllegalStateException
2525
import java.math.BigInteger
26+
import java.nio.Buffer
2627
import java.nio.ByteBuffer
2728
import java.util.Arrays
2829
import kotlin.math.min
@@ -43,6 +44,7 @@ internal class ByteArrayBytecodeGenerator10(
4344
) : BytecodeGenerator {
4445

4546
private val decoder = Utf8StringDecoderPool.getInstance().getOrCreate()
47+
private val scratchBuffer = ByteBuffer.wrap(source)
4648
private var scratchArray = ByteArray(32)
4749
private val symbolTableHelper = SymbolTableHelper
4850

@@ -79,9 +81,13 @@ internal class ByteArrayBytecodeGenerator10(
7981
override fun readTimestampReference(position: Int, length: Int) = readTimestampReference(source, position, length)
8082

8183
override fun readTextReference(position: Int, length: Int): String {
82-
// TODO(perf): See if there's a way to do this without allocating new ByteBuffers, that is compatible with JDK 8.
83-
val buffer = ByteBuffer.wrap(source, position, length)
84-
return decoder.decode(buffer, length)
84+
val b = scratchBuffer
85+
// We have to cast to `Buffer` here because JDK 17 added an override that returns `ByteBuffer`.
86+
// The compiler seems to prefer that version, rather than the base method (which returns `Buffer`), and so
87+
// running the tests with JDK 8 fails without this cast.
88+
(b as Buffer).limit(position + length)
89+
(b as Buffer).position(position)
90+
return decoder.decode(b, length)
8591
}
8692

8793
override fun readBytesReference(position: Int, length: Int): ByteSlice = ByteSlice(source, position, position + length)

0 commit comments

Comments
 (0)