Skip to content

Commit 5655ff2

Browse files
committed
Optimize appendString: bulk array copy via String.getChars
Replace char-by-char appendUnsafeC loop with String.getChars bulk copy directly into CharBuilder's backing array. This eliminates per-character method call overhead and allows the JVM/native runtime to use optimized memory copy intrinsics. Upstream: He-Pin/sjsonnet@5e9686cd (appendString portion)
1 parent 652bdf7 commit 5655ff2

1 file changed

Lines changed: 6 additions & 5 deletions

File tree

sjsonnet/src/sjsonnet/BaseCharRenderer.scala

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,14 @@ class BaseCharRenderer[T <: upickle.core.CharOps.Output](
179179
}
180180
}
181181

182+
/** Bulk-copy String chars into CharBuilder's backing array.
183+
* Safe because ensureLength(len) guarantees capacity for currentLength + len.
184+
*/
182185
protected def appendString(s: String): Unit = {
183186
val len = s.length
184-
var i = 0
185187
elemBuilder.ensureLength(len)
186-
while (i < len) {
187-
elemBuilder.appendUnsafeC(s.charAt(i))
188-
i += 1
189-
}
188+
val pos = elemBuilder.getLength
189+
s.getChars(0, len, elemBuilder.arr, pos)
190+
elemBuilder.length = pos + len
190191
}
191192
}

0 commit comments

Comments
 (0)