Skip to content

Commit

Permalink
More efficient decoding of string values (#1280)
Browse files Browse the repository at this point in the history
  • Loading branch information
plokhotnyuk authored Feb 3, 2025
1 parent fec8e2c commit e56066d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
9 changes: 6 additions & 3 deletions zio-json/shared/src/main/scala/zio/json/internal/lexer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,8 @@ object Lexer {
def string(trace: List[JsonError], in: OneCharReader): CharSequence = {
var c = in.nextNonWhitespace()
if (c != '"') error("'\"'", c, trace)
val sb = new FastStringBuilder(64)
var cs = new Array[Char](64)
var i = 0
while ({
c = in.readChar()
c != '"'
Expand All @@ -222,9 +223,11 @@ object Lexer {
case _ => error(c, trace)
}
} else if (c < ' ') error("invalid control in string", trace)
sb.append(c)
if (i == cs.length) cs = java.util.Arrays.copyOf(cs, i << 1)
cs(i) = c
i += 1
}
sb.buffer
new String(cs, 0, i)
}

def char(trace: List[JsonError], in: OneCharReader): Char = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ final class FastStringWrite(initial: Int) extends Write {
def buffer: CharSequence = sb
}

// like StringBuilder but doesn't have any encoding or range checks
// FIXME: remove in the next major version
private[zio] final class FastStringBuilder(initial: Int) {
private[this] var chars: Array[Char] = new Array[Char](initial)
private[this] var i: Int = 0
Expand Down

0 comments on commit e56066d

Please sign in to comment.