ifcopenshell.api.run optimization

cache usecase classes instead of importing their modules and getting them each time, makes empty api call twice as fast
This commit is contained in:
Andrej730
2024-04-09 16:18:11 +05:00
parent da838a2c7c
commit 8df04091ff
@@ -70,6 +70,8 @@ ARGUMENTS_DEPRECATION = {
} }
CACHED_USECASE_CLASSES = dict()
def run( def run(
usecase_path: str, usecase_path: str,
ifc_file: Optional[ifcopenshell.file] = None, ifc_file: Optional[ifcopenshell.file] = None,
@@ -108,9 +110,12 @@ def run(
# except: # except:
# print(usecase_path, vcs_settings) # print(usecase_path, vcs_settings)
importlib.import_module(f"ifcopenshell.api.{usecase_path}") usecase_class = CACHED_USECASE_CLASSES.get(usecase_path)
module, usecase = usecase_path.split(".") if usecase_class is None:
usecase_class = getattr(getattr(getattr(ifcopenshell.api, module), usecase), "Usecase") importlib.import_module(f"ifcopenshell.api.{usecase_path}")
module, usecase = usecase_path.split(".")
usecase_class = getattr(getattr(getattr(ifcopenshell.api, module), usecase), "Usecase")
CACHED_USECASE_CLASSES[usecase_path] = usecase_class
if ifc_file: if ifc_file:
result = usecase_class(ifc_file, **settings).execute() result = usecase_class(ifc_file, **settings).execute()