Bug 739510. JSObjectBuilder: Speed up construction of char * properties. r=ehsan
authorJeff Muizelaar <jmuizelaar@mozilla.com>
Mon, 26 Mar 2012 23:35:28 -0400
changeset 91804 5130ac1fa079c536c1a265d9842eaca52faa3546
parent 91803 0d0ac6a97737209b487ba276b97f3d102363603d
child 91805 8c3acc1224692cf4ef8a741ef2d8af88e502f7a9
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersehsan
bugs739510
milestone14.0a1
Bug 739510. JSObjectBuilder: Speed up construction of char * properties. r=ehsan This eliminates two additional copies: NS_ConvertASCIItoUTF16 and PromiseFlatString by constructing the JSString directly. It is also designed so that literal strings will have their strlen computed at compile time.
tools/profiler/JSObjectBuilder.h
--- a/tools/profiler/JSObjectBuilder.h
+++ b/tools/profiler/JSObjectBuilder.h
@@ -79,20 +79,33 @@ class JSObjectBuilder
       mOk = JS_FALSE;
 
     if (!mOk)
       return;
 
     mOk = JS_DefineProperty(mCx, aObject, name, STRING_TO_JSVAL(string), NULL, NULL, JSPROP_ENUMERATE);
   }
 
+  void DefineProperty(JSObject *aObject, const char *name, const char *value, size_t valueLength)
+  {
+    if (!mOk)
+      return;
+
+    JSString *string = JS_NewStringCopyN(mCx, value, valueLength);
+    if (!string) {
+      mOk = JS_FALSE;
+      return;
+    }
+
+    mOk = JS_DefineProperty(mCx, aObject, name, STRING_TO_JSVAL(string), NULL, NULL, JSPROP_ENUMERATE);
+  }
+
   void DefineProperty(JSObject *aObject, const char *name, const char *value)
   {
-    nsAutoString string = NS_ConvertASCIItoUTF16(value);
-    DefineProperty(aObject, name, string);
+    DefineProperty(aObject, name, value, strlen(value));
   }
 
   void ArrayPush(JSObject *aArray, int value)
   {
     if (!mOk)
       return;
 
     jsval objval = INT_TO_JSVAL(value);