[gjs] strings: Avoid copy of utf16 data in case where g_mem_is_system_malloc()



commit bd8d0f2ad9a2974930dc3ac943a319125769e7d3
Author: Colin Walters <walters verbum org>
Date:   Tue Dec 4 16:35:35 2012 -0500

    strings: Avoid copy of utf16 data in case where g_mem_is_system_malloc()
    
    This should be the common case.  Just noticed this while reading the
    code for unrelated reasons.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=689659

 gjs/jsapi-util-string.c |   13 +++++++++----
 1 files changed, 9 insertions(+), 4 deletions(-)
---
diff --git a/gjs/jsapi-util-string.c b/gjs/jsapi-util-string.c
index 5a340be..d94ae69 100644
--- a/gjs/jsapi-util-string.c
+++ b/gjs/jsapi-util-string.c
@@ -147,10 +147,15 @@ gjs_string_from_utf8(JSContext  *context,
 
     JS_BeginRequest(context);
 
-    s = JS_NewUCStringCopyN(context,
-                            (jschar*)u16_string,
-                            u16_string_length);
-    g_free(u16_string);
+    if (g_mem_is_system_malloc()) {
+        /* Avoid a copy - assumes that g_malloc == js_malloc == malloc */
+        s = JS_NewUCString(context, u16_string, u16_string_length);
+    } else {
+        s = JS_NewUCStringCopyN(context,
+                                (jschar*)u16_string,
+                                u16_string_length);
+        g_free(u16_string);
+    }
 
     if (!s) {
         JS_EndRequest(context);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]