[pygobject/bochecha/gvariant-unpack: 1/2] variant: Get the type string only once when unpacking



commit a7149a0ba83921591fe309a9cde0f8e43b55739c
Author: Mathieu Bridon <mathieu hashbang fr>
Date:   Tue Jul 23 15:32:53 2019 +0200

    variant: Get the type string only once when unpacking
    
    This saves a bit of work. Unpacking many GVariants in a loop, this
    shaved roughly 6% of the total time.

 gi/overrides/GLib.py | 16 +++++++++-------
 1 file changed, 9 insertions(+), 7 deletions(-)
---
diff --git a/gi/overrides/GLib.py b/gi/overrides/GLib.py
index 186902e8..a383f583 100644
--- a/gi/overrides/GLib.py
+++ b/gi/overrides/GLib.py
@@ -236,19 +236,21 @@ class Variant(GLib.Variant):
             'g': self.get_string,  # signature
         }
 
+        type_string = self.get_type_string()
+
         # simple values
-        la = LEAF_ACCESSORS.get(self.get_type_string())
+        la = LEAF_ACCESSORS.get(type_string)
         if la:
             return la()
 
         # tuple
-        if self.get_type_string().startswith('('):
+        if type_string.startswith('('):
             res = [self.get_child_value(i).unpack()
                    for i in range(self.n_children())]
             return tuple(res)
 
         # dictionary
-        if self.get_type_string().startswith('a{'):
+        if type_string.startswith('a{'):
             res = {}
             for i in range(self.n_children()):
                 v = self.get_child_value(i)
@@ -256,21 +258,21 @@ class Variant(GLib.Variant):
             return res
 
         # array
-        if self.get_type_string().startswith('a'):
+        if type_string.startswith('a'):
             return [self.get_child_value(i).unpack()
                     for i in range(self.n_children())]
 
         # variant (just unbox transparently)
-        if self.get_type_string().startswith('v'):
+        if type_string.startswith('v'):
             return self.get_variant().unpack()
 
         # maybe
-        if self.get_type_string().startswith('m'):
+        if type_string.startswith('m'):
             if not self.n_children():
                 return None
             return self.get_child_value(0).unpack()
 
-        raise NotImplementedError('unsupported GVariant type ' + self.get_type_string())
+        raise NotImplementedError('unsupported GVariant type ' + type_string)
 
     @classmethod
     def split_signature(klass, signature):


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