Skip to content
Snippets Groups Projects
Commit e09189cf authored by Campbell Barton's avatar Campbell Barton
Browse files

complete lazy loading of py modules to use to a reduced set of pythons...

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.
parent ea5664c0
No related branches found
No related tags found
No related merge requests found
...@@ -103,8 +103,10 @@ def load_scripts(reload_scripts=False, refresh_scripts=False): ...@@ -103,8 +103,10 @@ def load_scripts(reload_scripts=False, refresh_scripts=False):
:arg refresh_scripts: only load scripts which are not already loaded as modules. :arg refresh_scripts: only load scripts which are not already loaded as modules.
:type refresh_scripts: bool :type refresh_scripts: bool
""" """
import time use_time = _bpy.app.debug
if use_time:
import time
t_main = time.time() t_main = time.time()
loaded_modules = set() loaded_modules = set()
...@@ -213,7 +215,7 @@ def load_scripts(reload_scripts=False, refresh_scripts=False): ...@@ -213,7 +215,7 @@ def load_scripts(reload_scripts=False, refresh_scripts=False):
import gc import gc
print("gc.collect() -> %d" % gc.collect()) print("gc.collect() -> %d" % gc.collect())
if _bpy.app.debug: if use_time:
print("Python Script Load Time %.4f" % (time.time() - t_main)) print("Python Script Load Time %.4f" % (time.time() - t_main))
...@@ -418,27 +420,27 @@ def _bpy_module_classes(module, is_registered=False): ...@@ -418,27 +420,27 @@ def _bpy_module_classes(module, is_registered=False):
typemap_list = _bpy_types.TypeMap.get(module, ()) typemap_list = _bpy_types.TypeMap.get(module, ())
i = 0 i = 0
while i < len(typemap_list): while i < len(typemap_list):
cls_weakref, path, line = typemap_list[i] cls_weakref = typemap_list[i]
cls = cls_weakref() cls = cls_weakref()
if cls is None: if cls is None:
del typemap_list[i] del typemap_list[i]
else: else:
if is_registered == cls.is_registered: if is_registered == cls.is_registered:
yield (cls, path, line) yield cls
i += 1 i += 1
def register_module(module, verbose=False): def register_module(module, verbose=False):
if verbose: if verbose:
print("bpy.utils.register_module(%r): ..." % module) 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: if verbose:
print(" %s of %s:%s" % (cls, path, line)) print(" %r" % cls)
try: try:
register_class(cls) register_class(cls)
except: 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) print("\t", path, "line", line)
import traceback import traceback
traceback.print_exc() traceback.print_exc()
...@@ -451,13 +453,13 @@ def register_module(module, verbose=False): ...@@ -451,13 +453,13 @@ def register_module(module, verbose=False):
def unregister_module(module, verbose=False): def unregister_module(module, verbose=False):
if verbose: if verbose:
print("bpy.utils.unregister_module(%r): ..." % module) 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: if verbose:
print(" %s of %s:%s" % (cls, path, line)) print(" %r" % cls)
try: try:
unregister_class(cls) unregister_class(cls)
except: 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) print("\t", path, "line", line)
import traceback import traceback
traceback.print_exc() traceback.print_exc()
......
...@@ -567,18 +567,15 @@ TypeMap = {} ...@@ -567,18 +567,15 @@ TypeMap = {}
class RNAMeta(type): class RNAMeta(type):
def __new__(cls, name, bases, classdict, **args): def __new__(cls, name, bases, classdict, **args):
result = type.__new__(cls, name, bases, classdict) result = type.__new__(cls, name, bases, classdict)
if bases and bases[0] != StructRNA: if bases and bases[0] is not StructRNA:
import traceback from _weakref import ref as ref
import weakref
module = result.__module__ module = result.__module__
# first part of packages only # first part of packages only
if "." in module: if "." in module:
module = module[:module.index(".")] 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 return result
...@@ -586,7 +583,20 @@ class RNAMeta(type): ...@@ -586,7 +583,20 @@ class RNAMeta(type):
def is_registered(cls): def is_registered(cls):
return "bl_rna" in cls.__dict__ 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): class RNAMetaPropGroup(RNAMeta, StructMetaPropGroup):
...@@ -600,7 +610,7 @@ class OrderedMeta(RNAMeta): ...@@ -600,7 +610,7 @@ class OrderedMeta(RNAMeta):
cls.order = list(attributes.keys()) cls.order = list(attributes.keys())
def __prepare__(name, bases, **kwargs): def __prepare__(name, bases, **kwargs):
return collections.OrderedDict() return OrderedDictMini() # collections.OrderedDict()
# Only defined so operators members can be used by accessing self.order # Only defined so operators members can be used by accessing self.order
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment