Skip to content

Commit 0a187be

Browse files
He-PinHe-Pin
authored andcommitted
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 0a187be

1 file changed

Lines changed: 7 additions & 5 deletions

File tree

sjsonnet/src/sjsonnet/BaseCharRenderer.scala

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

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

0 commit comments

Comments
 (0)