[gjs/march-maintenance: 3/6] object: Return early from property resolve if name starts with -



commit 1f29c6d9a9d89c53aaa3a45a3e2170724b81b6b0
Author: Philip Chimento <philip chimento gmail com>
Date:   Wed Mar 11 22:49:18 2020 -0700

    object: Return early from property resolve if name starts with -
    
    This is a small optimization. GObject property names must start with a
    letter. Any other character gets turned into a dash by
    canonicalize_name(), so if the property name starts with a dash after
    canonicalization then it's not a valid property name and can't be a
    GObject property, so we can return early from any code that is checking
    whether it is or not.
    
    I noticed that resolve was checking for _init and _instance_init, so
    this seems like a pretty common case to optimize for.

 gi/object.cpp | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
---
diff --git a/gi/object.cpp b/gi/object.cpp
index 96e2062a..0371e2fb 100644
--- a/gi/object.cpp
+++ b/gi/object.cpp
@@ -614,6 +614,10 @@ static void canonicalize_key(const GjsAutoChar& key) {
 GJS_USE
 static bool is_ginterface_property_name(GIInterfaceInfo* info,
                                         const char* name) {
+    // Optimization: GObject property names must start with a letter
+    if (name[0] == '-')
+        return false;
+
     int n_props = g_interface_info_get_n_properties(info);
     GjsAutoPropertyInfo prop_info;
 
@@ -669,6 +673,10 @@ bool ObjectPrototype::resolve_no_info(JSContext* cx, JS::HandleObject obj,
     if (resolve_props == ConsiderMethodsAndProperties) {
         canonical_name = gjs_hyphen_from_camel(name);
         canonicalize_key(canonical_name);
+
+        // Optimization: GObject property names must start with a letter
+        if (canonical_name[0] == '-')
+            canonical_name.reset();
     }
 
     GIInterfaceInfo** interfaces;
@@ -710,7 +718,7 @@ bool ObjectPrototype::resolve_no_info(JSContext* cx, JS::HandleObject obj,
             }
         }
 
-        if (resolve_props == ConsiderOnlyMethods)
+        if (!canonical_name)
             continue;
 
         /* If the name refers to a GObject property, lazily define the property
@@ -737,6 +745,10 @@ is_gobject_property_name(GIObjectInfo *info,
     GjsAutoChar canonical_name = gjs_hyphen_from_camel(name);
     canonicalize_key(canonical_name);
 
+    // Optimization: GObject property names must start with a letter
+    if (canonical_name[0] == '-')
+        return false;
+
     for (ix = 0; ix < n_props; ix++) {
         prop_info = g_object_info_get_property(info, ix);
         if (strcmp(canonical_name, prop_info.name()) == 0)


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