Skip to content
This repository was archived by the owner on Oct 15, 2020. It is now read-only.

Commit 43125ad

Browse files
committed
chakrashim: Avoid calling JsCopyString twice to find length
To find Utf8Length, we call `JsCopyString()` to find the length of utf8 string and then another call to actually perform the copy into the buffer. 2nd call is not needed for just finding length. Gives approx. 2% win in acme-air on my machine.
1 parent 92eb561 commit 43125ad

File tree

3 files changed

+22
-12
lines changed

3 files changed

+22
-12
lines changed

deps/chakrashim/src/jsrtutils.cc

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -976,26 +976,34 @@ StringUtf8::~StringUtf8() {
976976
}
977977
}
978978

979+
JsErrorCode StringUtf8::LengthFrom(JsValueRef strRef) {
980+
CHAKRA_ASSERT(_length == 0);
981+
982+
size_t len = 0;
983+
IfJsErrorRet(JsCopyString(strRef, nullptr, 0, &len));
984+
985+
_length = len;
986+
return JsNoError;
987+
}
988+
979989
JsErrorCode StringUtf8::From(JsValueRef strRef) {
980990
CHAKRA_ASSERT(!_str);
981991

982-
size_t len = 0;
983-
IfJsErrorRet(JsCopyString(strRef, nullptr, 0, &len));
992+
IfJsErrorRet(LengthFrom(strRef));
993+
size_t len = length();
984994

985995
char* buffer = reinterpret_cast<char*>(malloc(len+1));
986996
CHAKRA_VERIFY(buffer != nullptr);
987997

988998
size_t written = 0;
989-
JsErrorCode errorCode = JsCopyString(strRef, buffer, len, &written);
999+
IfJsErrorRet(JsCopyString(strRef, buffer, len, &written));
9901000

991-
if (errorCode == JsNoError) {
992-
CHAKRA_ASSERT(len == written);
993-
buffer[len] = '\0';
994-
_str = buffer;
995-
_length = static_cast<int>(len);
996-
}
1001+
CHAKRA_ASSERT(len == written);
1002+
buffer[len] = '\0';
1003+
_str = buffer;
1004+
_length = static_cast<int>(len);
9971005

998-
return errorCode;
1006+
return JsNoError;
9991007
}
10001008

10011009
} // namespace jsrt

deps/chakrashim/src/jsrtutils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ class StringUtf8 {
104104
operator const char *() const { return _str; }
105105
int length() const { return static_cast<int>(_length); }
106106
JsErrorCode From(JsValueRef strRef);
107+
// This just initializes length field. _str will remain uninitialized.
108+
// Use `From()` to initialize _str and _length
109+
JsErrorCode LengthFrom(JsValueRef strRef);
107110

108111
private:
109112
// Disallow copying and assigning

deps/chakrashim/src/v8string.cc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,11 +79,10 @@ int String::Length() const {
7979

8080
int String::Utf8Length() const {
8181
jsrt::StringUtf8 str;
82-
if (str.From((JsValueRef)this) != JsNoError) {
82+
if (str.LengthFrom((JsValueRef)this) != JsNoError) {
8383
// error
8484
return 0;
8585
}
86-
8786
return str.length();
8887
}
8988

0 commit comments

Comments
 (0)