Skip to content

Commit

Permalink
Merge branch 'appendStringOp'
Browse files Browse the repository at this point in the history
The StringBuilder.append method invoked was StringBuilder.append(Object) as
both String and Char are being returned. The implementation of
StringBuilder.append(Object) when applied to a Character is essentially:

* call Character.toString - which produces a one element char[] and then
  creates a new String with the char array, which in turn does a defensive copy
  of the buffer.
* the string created is then used to add to the StringBuilder

This PR changes JsonAST.appendEscapedString to use StringBuilder.append(Char)
which directly inserts the character into the buffer avoiding the operations
described above
  • Loading branch information
Shadowfiend committed Jul 3, 2015
2 parents 428af40 + d427fa4 commit 81db1ae
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions core/json/src/main/scala/net/liftweb/json/JsonAST.scala
Expand Up @@ -852,7 +852,7 @@ object JsonAST {
private def appendEscapedString(buf: StringBuilder, s: String) {
for (i <- 0 until s.length) {
val c = s.charAt(i)
buf.append(c match {
val strReplacement = c match {
case '"' => "\\\""
case '\\' => "\\\\"
case '\b' => "\\b"
Expand All @@ -861,8 +861,16 @@ object JsonAST {
case '\r' => "\\r"
case '\t' => "\\t"
case c if ((c >= '\u0000' && c < '\u0020')) => "\\u%04x".format(c: Int)
case c => c
})

case _ => ""
}

// Use Char version of append if we can, as it's cheaper.
if (strReplacement.isEmpty) {
buf.append(c)
} else {
buf.append(strReplacement)
}
}
}

Expand Down

0 comments on commit 81db1ae

Please sign in to comment.