From 2c30d6174032d63d52f2c3b6c8248022de2ca5d9 Mon Sep 17 00:00:00 2001 From: wuggy Date: Wed, 22 Apr 2026 03:35:52 -0700 Subject: [PATCH] Optimize substring handling in js::SubstringKernel by caching leftChild length --- js/src/jsstr.cpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/js/src/jsstr.cpp b/js/src/jsstr.cpp index a5829f9b9e..88667701ee 100644 --- a/js/src/jsstr.cpp +++ b/js/src/jsstr.cpp @@ -564,14 +564,15 @@ js::SubstringKernel(JSContext* cx, HandleString str, int32_t beginInt, int32_t l */ if (str->isRope()) { JSRope* rope = &str->asRope(); + size_t leftLength = rope->leftChild()->length(); /* Substring is totally in leftChild of rope. */ - if (begin + len <= rope->leftChild()->length()) + if (begin + len <= leftLength) return NewDependentString(cx, rope->leftChild(), begin, len); /* Substring is totally in rightChild of rope. */ - if (begin >= rope->leftChild()->length()) { - begin -= rope->leftChild()->length(); + if (begin >= leftLength) { + begin -= leftLength; return NewDependentString(cx, rope->rightChild(), begin, len); } @@ -579,11 +580,11 @@ js::SubstringKernel(JSContext* cx, HandleString str, int32_t beginInt, int32_t l * Requested substring is partly in the left and partly in right child. * Create a rope of substrings for both childs. */ - MOZ_ASSERT(begin < rope->leftChild()->length() && - begin + len > rope->leftChild()->length()); + MOZ_ASSERT(begin < leftLength && + begin + len > leftLength); - size_t lhsLength = rope->leftChild()->length() - begin; - size_t rhsLength = begin + len - rope->leftChild()->length(); + size_t lhsLength = leftLength - begin; + size_t rhsLength = begin + len - leftLength; Rooted ropeRoot(cx, rope); RootedString lhs(cx, NewDependentString(cx, ropeRoot->leftChild(), begin, lhsLength));