Skip to content

Commit 1be6a92

Browse files
more efficient string processing
Co-authored-by: Caleb Aikens <[email protected]>
1 parent 1ad53c2 commit 1be6a92

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/PyBytesProxyHandler.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ static bool array_valueOf(JSContext *cx, unsigned argc, JS::Value *vp) {
3535
JS::AutoCheckCannotGC autoNoGC(cx);
3636
uint8_t *data = JS::GetArrayBufferData(rootedArrayBuffer, &isSharedMemory, autoNoGC);
3737

38-
std::string valueOfString;
38+
const size_t STRING_LENGTH = byteLength*2 - 1;
39+
JS::Latin1Char* buffer = (JS::Latin1Char *)malloc(sizeof(JS::Latin1Char) * STRING_LENGTH);
3940

40-
for (Py_ssize_t index = 0; index < byteLength; index++) {
41-
if (index > 0) {
42-
valueOfString += ",";
43-
}
44-
45-
valueOfString += std::to_string(data[index]);
41+
buffer[0] = data[0];
42+
for (Py_ssize_t index = 1; index < byteLength; index++) {
43+
buffer[index*2 - 1] = ',';
44+
buffer[index*2] = data[index];
4645
}
4746

48-
args.rval().setString(JS_NewStringCopyZ(cx, valueOfString.c_str()));
47+
JS::UniqueLatin1Chars str(buffer);
48+
args.rval().setString(JS_NewLatin1String(cx, std::move(str), STRING_LENGTH));
4949
return true;
5050
}
5151

0 commit comments

Comments
 (0)