Skip to content

Commit

Permalink
Merge pull request #24 from ScorexFoundation/perf-opts
Browse files Browse the repository at this point in the history
More performance optimisations
  • Loading branch information
aslesarenko authored Nov 16, 2020
2 parents ee9a2b9 + 0e58f9e commit 9adb6c6
Show file tree
Hide file tree
Showing 11 changed files with 46 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/main/scala/scorex/util/encode/Base16.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ object Base16 extends BytesEncoder {
}

def encode(input: Array[Byte]): String = {
if (input.length == 0) return "" // avoid allocation of empty array and new String instance
val buf = new Array[Char](input.length * 2)
var j = 0
while (j < input.length) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/scorex/util/encode/Base58.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ object Base58 extends BytesEncoder {
override def decode(input: String): Try[Array[Byte]] = Try {
val decoded = decodeToBigInteger(input)

val bytes: Array[Byte] = if (decoded == BigInt(0)) Array.empty else decoded.toByteArray
val bytes: Array[Byte] = if (decoded == BigInt(0)) Array.emptyByteArray else decoded.toByteArray
// We may have got one more byte than we wanted, if the high bit of the next-to-last byte was not zero.
// This is because BigIntegers are represented with twos-compliment notation,
// thus if the high bit of the last byte happens to be 1 another 8 zero bits will be added to
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/scorex/util/serialization/Reader.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package scorex.util.serialization


trait Reader {
abstract class Reader {

/**
* Type of encoded data
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/scorex/util/serialization/Serializer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package scorex.util.serialization

import scala.util.Try

trait Serializer[TFamily, T <: TFamily, R <: Reader, W <: Writer] {
abstract class Serializer[TFamily, T <: TFamily, R <: Reader, W <: Writer] {

def serialize(obj: T, w: W): Unit

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class VLQByteBufferReader(buf: ByteBuffer) extends VLQReader {
@inline override def getByte(): Byte = buf.get

@inline override def getBytes(size: Int): Array[Byte] = {
require(size <= remaining, s"Not enough bytes in the buffer: $size")
val res = new Array[Byte](size)
buf.get(res)
res
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/scorex/util/serialization/VLQReader.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ trait VLQReader extends Reader {
}

@inline override def getBits(size: Int): Array[Boolean] = {
if (size == 0) return Array[Boolean]()
val bitSet = util.BitSet.valueOf(getBytes((size + 7) / 8))
if (size == 0) return Array.emptyBooleanArray
val bitSet = util.BitSet.valueOf(getBytes((size + 7) >> 3))
val boolArray = new Array[Boolean](size)
var i = 0
while (i < size) {
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 @@ -131,7 +131,7 @@ trait VLQWriter extends Writer {
}
// pad the byte array to fix the "no bit was set" behaviour
// see https://stackoverflow.com/questions/11209600/how-do-i-convert-a-bitset-initialized-with-false-in-a-byte-containing-0-in-java
val bytes = util.Arrays.copyOf(bitSet.toByteArray, (xs.length + 7) / 8)
val bytes = util.Arrays.copyOf(bitSet.toByteArray, (xs.length + 7) >> 3)
putBytes(bytes)
this
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/scala/scorex/util/serialization/Writer.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package scorex.util.serialization

trait Writer {
abstract class Writer {

/**
* Type of encoded data
Expand Down
6 changes: 6 additions & 0 deletions src/test/scala/scorex/util/encode/Base16Specification.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,10 @@ package scorex.util.encode

class Base16Specification extends BytesEncoderSpecification {
override val encoder: BytesEncoder = Base16

property("test vectors") {
val bytes = Array[Byte](1, 2, 3)
Base16.encode(bytes) shouldBe "010203"
Base16.encode(Array.emptyByteArray) shouldBe ""
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,36 @@ trait VLQReaderWriterSpecification extends AnyPropSpec
checkFail(Int.MaxValue)
}

property("getBytes size check") {
val bytes = Array[Byte](1, 2, 3)

{ // successful case
val r = byteBufReader(bytes)
r.getBytes(3) shouldBe bytes
}

{ // successful case 2
val r = byteBufReader(bytes)
r.position = 2
r.getBytes(1) shouldBe bytes.slice(2, 3)
}

{ // failure case
val r = byteBufReader(bytes)
an[IllegalArgumentException] should be thrownBy {
r.getBytes(4)
}
}

{ // failure case 2
val r = byteBufReader(bytes)
r.position = 2
an[IllegalArgumentException] should be thrownBy {
r.getBytes(2)
}
}
}

property("getUInt range check assertion") {
def check(in: Long): Unit =
byteBufReader(byteArrayWriter().putULong(in).toBytes).getUInt() shouldBe in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import java.nio.ByteBuffer

import scorex.util.ByteArrayBuilder

class VQLByteBufferReaderWriterSpecification extends VLQReaderWriterSpecification {
class VLQByteBufferReaderWriterSpecification extends VLQReaderWriterSpecification {

override def byteBufReader(bytes: Array[Byte]): VLQReader = {
val buf = ByteBuffer.wrap(bytes)
Expand Down

0 comments on commit 9adb6c6

Please sign in to comment.