[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [35256] trunk/blender/release/scripts/ modules: complete lazy loading of py modules to use to a reduced set of pythons modules , gives ~40% speedup on cold & warm start (without netrender).

Campbell Barton ideasman42 at gmail.com
Mon Feb 28 05:37:25 CET 2011


Revision: 35256
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=35256
Author:   campbellbarton
Date:     2011-02-28 04:37:24 +0000 (Mon, 28 Feb 2011)
Log Message:
-----------
complete lazy loading of py modules to use to a reduced set of pythons modules, gives ~40% speedup on cold & warm start (without netrender).
- use own OrderedDictMini class, pythons collections.OrderedDict is overkill, 179 sloc. replaced with own, 11 lines.
- remove code which stored the class file & line per RNA subclass, this was useful but would raise its own exception every time to generate a stack trace to get the class info so we could use of the class failed to register. the class stores its module & name which can be enough to find where it was defined.

Modified Paths:
--------------
    trunk/blender/release/scripts/modules/bpy/utils.py
    trunk/blender/release/scripts/modules/bpy_types.py

Modified: trunk/blender/release/scripts/modules/bpy/utils.py
===================================================================
--- trunk/blender/release/scripts/modules/bpy/utils.py	2011-02-28 03:17:53 UTC (rev 35255)
+++ trunk/blender/release/scripts/modules/bpy/utils.py	2011-02-28 04:37:24 UTC (rev 35256)
@@ -103,9 +103,11 @@
     :arg refresh_scripts: only load scripts which are not already loaded as modules.
     :type refresh_scripts: bool
     """
-    import time
+    use_time = _bpy.app.debug
 
-    t_main = time.time()
+    if use_time:
+        import time
+        t_main = time.time()
 
     loaded_modules = set()
 
@@ -213,7 +215,7 @@
         import gc
         print("gc.collect() -> %d" % gc.collect())
 
-    if _bpy.app.debug:
+    if use_time:
         print("Python Script Load Time %.4f" % (time.time() - t_main))
 
 
@@ -418,27 +420,27 @@
     typemap_list = _bpy_types.TypeMap.get(module, ())
     i = 0
     while i < len(typemap_list):
-        cls_weakref, path, line = typemap_list[i]
+        cls_weakref = typemap_list[i]
         cls = cls_weakref()
 
         if cls is None:
             del typemap_list[i]
         else:
             if is_registered == cls.is_registered:
-                yield (cls, path, line)
+                yield cls
             i += 1
 
 
 def register_module(module, verbose=False):
     if verbose:
         print("bpy.utils.register_module(%r): ..." % module)
-    for cls, path, line in _bpy_module_classes(module, is_registered=False):
+    for cls in _bpy_module_classes(module, is_registered=False):
         if verbose:
-            print("    %s of %s:%s" % (cls, path, line))
+            print("    %r" % cls)
         try:
             register_class(cls)
         except:
-            print("bpy.utils.register_module(): failed to registering class '%s.%s'" % (cls.__module__, cls.__name__))
+            print("bpy.utils.register_module(): failed to registering class %r" % cls)
             print("\t", path, "line", line)
             import traceback
             traceback.print_exc()
@@ -451,13 +453,13 @@
 def unregister_module(module, verbose=False):
     if verbose:
         print("bpy.utils.unregister_module(%r): ..." % module)
-    for cls, path, line in _bpy_module_classes(module, is_registered=True):
+    for cls in _bpy_module_classes(module, is_registered=True):
         if verbose:
-            print("    %s of %s:%s" % (cls, path, line))
+            print("    %r" % cls)
         try:
             unregister_class(cls)
         except:
-            print("bpy.utils.unregister_module(): failed to unregistering class '%s.%s'" % (cls.__module__, cls.__name__))
+            print("bpy.utils.unregister_module(): failed to unregistering class %r" % cls)
             print("\t", path, "line", line)
             import traceback
             traceback.print_exc()

Modified: trunk/blender/release/scripts/modules/bpy_types.py
===================================================================
--- trunk/blender/release/scripts/modules/bpy_types.py	2011-02-28 03:17:53 UTC (rev 35255)
+++ trunk/blender/release/scripts/modules/bpy_types.py	2011-02-28 04:37:24 UTC (rev 35256)
@@ -567,28 +567,38 @@
 class RNAMeta(type):
     def __new__(cls, name, bases, classdict, **args):
         result = type.__new__(cls, name, bases, classdict)
-        if bases and bases[0] != StructRNA:
-            import traceback
-            import weakref
+        if bases and bases[0] is not StructRNA:
+            from _weakref import ref as ref
             module = result.__module__
 
             # first part of packages only
             if "." in module:
                 module = module[:module.index(".")]
 
-            sf = traceback.extract_stack(limit=2)[0]
+            TypeMap.setdefault(module, []).append(ref(result))
 
-            TypeMap.setdefault(module, []).append((weakref.ref(result), sf[0], sf[1]))
-
         return result
 
     @property
     def is_registered(cls):
         return "bl_rna" in cls.__dict__
 
-import collections
 
+class OrderedDictMini(dict):
+    def __init__(self, *args):
+        self.order = []
+        dict.__init__(self, args)
 
+    def __setitem__(self, key, val):
+        dict.__setitem__(self, key, val)
+        if key not in self.order:
+            self.order.append(key)
+
+    def __delitem__(self, key):
+        dict.__delitem__(self, key)
+        self.order.remove(key)
+
+
 class RNAMetaPropGroup(RNAMeta, StructMetaPropGroup):
     pass
 
@@ -600,7 +610,7 @@
         cls.order = list(attributes.keys())
 
     def __prepare__(name, bases, **kwargs):
-        return collections.OrderedDict()
+        return OrderedDictMini()  # collections.OrderedDict()
 
 
 # Only defined so operators members can be used by accessing self.order




More information about the Bf-blender-cvs mailing list