Skip to content

Commit 8834d96

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 8834d96

2 files changed

Lines changed: 15 additions & 13 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
}

sjsonnet/test/src/sjsonnet/StdObjectRemoveKeyTests.scala

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ object StdObjectRemoveKeyTests extends TestSuite {
1919

2020
test("re-adding removed key via object inheritance") {
2121
eval("""std.objectRemoveKey({foo: "bar"}, "foo") + {foo: "bar2"}""") ==>
22-
ujson.Obj("foo" -> "bar2")
22+
ujson.Obj("foo" -> "bar2")
2323

2424
eval("""std.objectRemoveKey({a: 1, b: 2}, "a") + {a: 3}""") ==>
25-
ujson.Obj("b" -> 2, "a" -> 3)
25+
ujson.Obj("b" -> 2, "a" -> 3)
2626

2727
eval("""std.objectRemoveKey({a: 1, b: 2, c: 3}, "b") + {b: 99, d: 4}""") ==>
28-
ujson.Obj("a" -> 1, "c" -> 3, "b" -> 99, "d" -> 4)
28+
ujson.Obj("a" -> 1, "c" -> 3, "b" -> 99, "d" -> 4)
2929
}
3030

3131
test("double removal then re-add") {
3232
eval(
33-
"""std.objectRemoveKey(std.objectRemoveKey({a: 1, b: 2, c: 3}, "a"), "b") + {a: 10}"""
33+
"""std.objeceKey(std.objectRemoveKey({a: 1, b: 2, c: 3}, "a"), "b") + {a: 10}"""
3434
) ==> ujson.Obj("c" -> 3, "a" -> 10)
3535
}
3636

@@ -46,18 +46,18 @@ object StdObjectRemoveKeyTests extends TestSuite {
4646

4747
test("external inheritance preserved after removal") {
4848
eval("{a: 1} + std.objectRemoveKey({b: super.a}, 'a')") ==>
49-
ujson.Obj("a" -> 1, "b" -> 1)
49+
ujson.Obj("a" -> 1, "b" -> 1)
5050
}
5151

5252
test("LHS key preserved when RHS removes it") {
5353
eval("""{a: 1} + std.objectRemoveKey({a: 2}, "a")""") ==>
54-
ujson.Obj("a" -> 1)
54+
ujson.Obj("a" -> 1)
5555

5656
eval("""{a: 1} + std.objectRemoveKey({a: 2, b: 3}, "a")""") ==>
57-
ujson.Obj("a" -> 1, "b" -> 3)
57+
ujson.Obj("a" -> 1, "b" -> 3)
5858

5959
eval("""{a: 10} + std.objectRemoveKey({a: 1} + {b: super.a}, "a")""") ==>
60-
ujson.Obj("a" -> 10, "b" -> 1)
60+
ujson.Obj("a" -> 10, "b" -> 1)
6161
}
6262

6363
test("containsKey and objectFields reflect re-added key") {

0 commit comments

Comments
 (0)