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