Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

String optimization of using utf16 SourceText inside pm.eval #446

Open
Xmader opened this issue Oct 4, 2024 · 0 comments
Open

String optimization of using utf16 SourceText inside pm.eval #446

Xmader opened this issue Oct 4, 2024 · 0 comments
Assignees

Comments

@Xmader
Copy link
Member

Xmader commented Oct 4, 2024

index b0cfb09..9c25651 100644
--- a/src/modules/pythonmonkey/pythonmonkey.cc
+++ b/src/modules/pythonmonkey/pythonmonkey.cc
@@ -460,14 +460,26 @@ static PyObject *eval(PyObject *self, PyObject *args) {
   JS::RootedScript script(GLOBAL_CX);
   JS::Rooted<JS::Value> rval(GLOBAL_CX);
   if (code) {
-    JS::SourceText<mozilla::Utf8Unit> source;
-    Py_ssize_t codeLength;
-    const char *codeChars = PyUnicode_AsUTF8AndSize(code, &codeLength);
-    if (!source.init(GLOBAL_CX, codeChars, codeLength, JS::SourceOwnership::Borrowed)) {
-      setSpiderMonkeyException(GLOBAL_CX);
-      return NULL;
+    if (PyUnicode_KIND(code) == PyUnicode_2BYTE_KIND) { // code is in UCS2 encoding, a subset of UTF16
+      JS::SourceText<char16_t> source;
+      Py_ssize_t codeLength = PyUnicode_GetLength(code);
+      Py_UCS2 *codeChars = PyUnicode_2BYTE_DATA(code);
+      if (!source.init(GLOBAL_CX, (char16_t *)codeChars, codeLength, JS::SourceOwnership::Borrowed)) {
+        setSpiderMonkeyException(GLOBAL_CX);
+        return NULL;
+      }
+      script = JS::Compile(GLOBAL_CX, options, source);
+    }
+    else {
+      JS::SourceText<mozilla::Utf8Unit> source;
+      Py_ssize_t codeLength;
+      const char *codeChars = PyUnicode_AsUTF8AndSize(code, &codeLength);
+      if (!source.init(GLOBAL_CX, codeChars, codeLength, JS::SourceOwnership::Borrowed)) {
+        setSpiderMonkeyException(GLOBAL_CX);
+        return NULL;
+      }
+      script = JS::Compile(GLOBAL_CX, options, source);
     }
-    script = JS::Compile(GLOBAL_CX, options, source);
   } else {
     assert(file);
     script = JS::CompileUtf8File(GLOBAL_CX, options, file);

Making the above change would be a good optimization in the case where the argument is already in UCS2 encoding, since then we can use utf16 SourceText.

Originally posted by @caleb-distributive in #443 (comment)

@Xmader Xmader changed the title String optimization inside pm.eval String optimization for using utf16 SourceText inside pm.eval Oct 4, 2024
@Xmader Xmader changed the title String optimization for using utf16 SourceText inside pm.eval String optimization of using utf16 SourceText inside pm.eval Oct 4, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
Status: Backlog
Development

No branches or pull requests

2 participants