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.
This commit is contained in:
Campbell Barton 2011-02-28 04:37:24 +00:00
parent ea5664c0d1
commit e09189cf50
2 changed files with 31 additions and 19 deletions

@ -103,9 +103,11 @@ def load_scripts(reload_scripts=False, refresh_scripts=False):
: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 @@ def load_scripts(reload_scripts=False, refresh_scripts=False):
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 @@ def _bpy_module_classes(module, is_registered=False):
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 register_module(module, verbose=False):
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()

@ -567,18 +567,15 @@ TypeMap = {}
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((weakref.ref(result), sf[0], sf[1]))
TypeMap.setdefault(module, []).append(ref(result))
return result
@ -586,7 +583,20 @@ class RNAMeta(type):
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):
@ -600,7 +610,7 @@ class OrderedMeta(RNAMeta):
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