Skip to content

Commit

Permalink
More efficient reading of the next character (#1333)
Browse files Browse the repository at this point in the history
  • Loading branch information
plokhotnyuk authored Feb 22, 2025
1 parent 0783dfc commit cb5d1aa
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions zio-json/shared/src/main/scala/zio/json/internal/readers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,30 +95,37 @@ final class FastCharSequence(s: Array[Char]) extends CharSequence {
// java.io.StringReader uses a lock, which reduces perf by x2, this also allows
// fast retraction and access to raw char arrays (which are faster than Strings)
private[zio] final class FastStringReader(s: CharSequence) extends RetractReader with PlaybackReader {
private[this] var i: Int = 0
private[this] val len: Int = s.length
private[this] var i: Int = 0

def offset(): Int = i

def close(): Unit = ()

override def read(): Int = {
i += 1
if (i > len) -1
else s.charAt(i - 1).toInt // -1 is faster than assigning a temp value
if (i < s.length) {
val c = s.charAt(i)
i += 1
return c.toInt
}
-1
}

override def readChar(): Char = {
i += 1
if (i > len) throw new UnexpectedEnd
s.charAt(i - 1)
if (i < s.length) {
val c = s.charAt(i)
i += 1
return c
}
throw new UnexpectedEnd
}

override def nextNonWhitespace(): Char = {
while ({
while (i < s.length) {
val c = s.charAt(i)
i += 1
if (i > len) throw new UnexpectedEnd
isWhitespace(s.charAt(i - 1))
}) ()
s.charAt(i - 1)
if (c != ' ' && c != '\n' && (c | 0x4) != '\r') return c
}
throw new UnexpectedEnd
}

def retract(): Unit = i -= 1
Expand Down

0 comments on commit cb5d1aa

Please sign in to comment.