[gjs: 4/10] jsapi: Avoid creating unnecessary stringstreams when debug logging




commit 750b2acdaec2eb0886c82edf20a3e60d925078af
Author: Philip Chimento <philip chimento gmail com>
Date:   Sun Jan 23 10:14:25 2022 -0800

    jsapi: Avoid creating unnecessary stringstreams when debug logging
    
    In several cases we can return the result of gjs_debug_... directly,
    instead of creating a stringstream to insert it into. Only create a
    stringstream in gjs_debug_value() if we are actually going to insert
    something into it.

 gjs/jsapi-util-string.cpp | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)
---
diff --git a/gjs/jsapi-util-string.cpp b/gjs/jsapi-util-string.cpp
index 81017ce7c..238134e5f 100644
--- a/gjs/jsapi-util-string.cpp
+++ b/gjs/jsapi-util-string.cpp
@@ -616,36 +616,32 @@ gjs_debug_object(JSObject * const obj)
 std::string
 gjs_debug_value(JS::Value v)
 {
-    std::ostringstream out;
     if (v.isNull())
         return "null";
     if (v.isUndefined())
         return "undefined";
     if (v.isInt32()) {
+        std::ostringstream out;
         out << v.toInt32();
         return out.str();
     }
     if (v.isDouble()) {
+        std::ostringstream out;
         out << v.toDouble();
         return out.str();
     }
     if (v.isBigInt()) {
+        std::ostringstream out;
         // technically this prints v % INT64_MAX
         out << JS::ToBigInt64(v.toBigInt()) << 'n';
         return out.str();
     }
-    if (v.isString()) {
-        out << gjs_debug_string(v.toString());
-        return out.str();
-    }
-    if (v.isSymbol()) {
-        out << gjs_debug_symbol(v.toSymbol());
-        return out.str();
-    }
-    if (v.isObject()) {
-        out << gjs_debug_object(&v.toObject());
-        return out.str();
-    }
+    if (v.isString())
+        return gjs_debug_string(v.toString());
+    if (v.isSymbol())
+        return gjs_debug_symbol(v.toSymbol());
+    if (v.isObject())
+        return gjs_debug_object(&v.toObject());
     if (v.isBoolean())
         return (v.toBoolean() ? "true" : "false");
     if (v.isMagic())


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