Skip to content

Commit e11511d

Browse files
authored
perf: add reference equality fast path to compareStringsByCodepoint (#770)
When two strings are the same reference (e.g. interned strings from the Parser pool, or self-comparison in array equality checks), return 0 immediately without doing the O(n) codepoint-by-codepoint comparison.
1 parent 661b8b2 commit e11511d

1 file changed

Lines changed: 2 additions & 0 deletions

File tree

sjsonnet/src/sjsonnet/Util.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ object Util {
135135
* compared correctly according to their Unicode codepoint values.
136136
*/
137137
def compareStringsByCodepoint(s1: String, s2: String): Int = {
138+
// Fast path: same reference (e.g. interned strings, or self-comparison)
139+
if (s1 eq s2) return 0
138140
val n1 = s1.length
139141
val n2 = s2.length
140142
var i1 = 0

0 commit comments

Comments
 (0)