Skip to content

Commit

Permalink
allocation optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
aslesarenko committed Sep 24, 2020
1 parent 3dc334f commit d48bb1a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
9 changes: 5 additions & 4 deletions src/main/scala/scorex/util/encode/Base16.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ object Base16 extends BytesEncoder {
}

def decode(input: String): Try[Array[Byte]] = {
var (isError, errorMsg) = if (input.length % 2 == 0) {
(false, "")
} else {
(true, s"invalid length ${input.length} of Hex data")
var isError = false
var errorMsg = ""
if (input.length % 2 != 0) {
isError = true
errorMsg = s"invalid length ${input.length} of Hex data"
}

val out = Array.ofDim[Byte](input.length / 2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ class VLQByteBufferWriter(b: ByteArrayBuilder) extends Writer with VLQWriter {
this
}

@inline override def putBytes(xs: Array[Byte], offset: Int, length: Int): this.type = {
b.append(xs, offset, length)
this
}

@inline override def length(): Int = b.length()

@inline override def result(): ByteArrayBuilder = b
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/scorex/util/serialization/VLQWriter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ trait VLQWriter extends Writer {
if ((value & ~0x7FL) == 0) {
buffer(position) = value.asInstanceOf[Byte]
position += 1
putBytes(util.Arrays.copyOf(buffer, position))
putBytes(buffer, 0, position)
return this
} else {
buffer(position) = ((value.asInstanceOf[Int] & 0x7F) | 0x80).toByte
Expand Down
8 changes: 8 additions & 0 deletions src/main/scala/scorex/util/serialization/Writer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ trait Writer {
*/
def putBytes(xs: Array[Byte]): this.type

/**
* Encode a slice of array of bytes.
* @param xs an array to take bytes from
* @param offset first byte of the slice
* @param length of the slice (number of bytes)
*/
def putBytes(xs: Array[Byte], offset: Int, length: Int): this.type

/**
* Encode an array of boolean values as a bit array
*
Expand Down

0 comments on commit d48bb1a

Please sign in to comment.