Optimize 'get_artifact_type' from ArtifactRegistry

Now to get an artifact type object by its name we iterate
by all available classes and compare their names. It leads
to complexity O(n).

It's better to generate a dict of available types and get
them directly from there with complexity O(1).

Change-Id: Icc13cd6f844bc93c8d5a80ba963068cde3ef133e
This commit is contained in:
Mike Fedosin 2017-10-11 23:03:34 +03:00
parent 8a5e5caa44
commit 7ac7bb092c

View File

@ -89,6 +89,8 @@ class ArtifactRegistry(vo_base.VersionedObjectRegistry):
returning appropriate artifact types based on artifact type name.
"""
enabled_types = {}
@classmethod
def register_all_artifacts(cls):
"""Register all artifacts in Glare."""
@ -116,6 +118,10 @@ class ArtifactRegistry(vo_base.VersionedObjectRegistry):
else:
raise exception.TypeNotFound(name=type_name)
# Fill enabled_types
for name, af_type in cls.obj_classes().items():
cls.enabled_types[af_type[0].get_type_name()] = af_type[0]
@classmethod
def get_artifact_type(cls, type_name):
"""Return artifact type based on artifact type name.
@ -123,10 +129,9 @@ class ArtifactRegistry(vo_base.VersionedObjectRegistry):
:param type_name: name of artifact type
:return: artifact class
"""
for name, af_type in cls.obj_classes().items():
if af_type[0].get_type_name() == type_name:
return af_type[0]
raise exception.TypeNotFound(name=type_name)
if type_name not in cls.enabled_types:
raise exception.TypeNotFound(name=type_name)
return cls.enabled_types[type_name]
@classmethod
def reset_registry(cls):