From 087706e2b05f28dae18a5570face2e1743dc23bf Mon Sep 17 00:00:00 2001 From: Will Gebhardt Date: Mon, 15 Jul 2024 14:16:37 -0400 Subject: [PATCH] Sped up compiler --- ngcsimlib/compilers/command_compiler.py | 4 ++-- ngcsimlib/compilers/component_compiler.py | 6 ++++-- ngcsimlib/compilers/op_compiler.py | 9 +++++++-- ngcsimlib/metaComponent.py | 9 ++++++++- 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/ngcsimlib/compilers/command_compiler.py b/ngcsimlib/compilers/command_compiler.py index 8c79051..e0c51a5 100644 --- a/ngcsimlib/compilers/command_compiler.py +++ b/ngcsimlib/compilers/command_compiler.py @@ -92,8 +92,8 @@ def compiled(compartment_values, **kwargs): critical(f"Missing keyword argument \"{n}\" in compiled function." f"\tExpected keyword arguments {needed_args}") - for exc, outs, name in exc_order: - _comps = {key: compartment_values[key] for key in needed_comps} + for exc, outs, name, comp_ids in exc_order: + _comps = {key: compartment_values[key] for key in comp_ids} vals = exc(**kwargs, **_comps) if len(outs) == 1: compartment_values[outs[0]] = vals diff --git a/ngcsimlib/compilers/component_compiler.py b/ngcsimlib/compilers/component_compiler.py index eed9b92..20691a6 100644 --- a/ngcsimlib/compilers/component_compiler.py +++ b/ngcsimlib/compilers/component_compiler.py @@ -102,11 +102,13 @@ def compile(component, resolver): funParams = {narg: component.__dict__[narg] for narg in params} + comp_key_key = [(narg.split('/')[-1], narg) for narg in comp_ids] + def compiled(**kwargs): funArgs = {narg: kwargs.get(narg) for narg in _args} - funComps = {narg.split('/')[-1]: kwargs.get(narg) for narg in comp_ids} + funComps = {knarg: kwargs.get(narg) for knarg, narg in comp_key_key} return pure_fn.__func__(**funParams, **funArgs, **funComps) - exc_order.append((compiled, out_ids, component.name)) + exc_order.append((compiled, out_ids, component.name, comp_ids)) return exc_order diff --git a/ngcsimlib/compilers/op_compiler.py b/ngcsimlib/compilers/op_compiler.py index 36efce9..2f32fff 100644 --- a/ngcsimlib/compilers/op_compiler.py +++ b/ngcsimlib/compilers/op_compiler.py @@ -85,8 +85,13 @@ def compile(op): else: iids.append(str(s.path)) + additional_idds = [] + for _, _, _, _iids in exc_order: + additional_idds.extend(_iids) + + # print(additional_idds) def _op_compiled(**kwargs): - computed_values = [cmd(**kwargs) for cmd, _, _ in exc_order] + computed_values = [cmd(**kwargs) for cmd, _, _, _ in exc_order] compartment_args = [kwargs.get(narg) for narg in iids] _val_loc = 0 @@ -103,4 +108,4 @@ def _op_compiled(**kwargs): return op.operation(*_args) - return (_op_compiled, [str(output)], "op") + return (_op_compiled, [str(output)], op.__class__.__name__, iids + additional_idds) diff --git a/ngcsimlib/metaComponent.py b/ngcsimlib/metaComponent.py index 7d2b7cb..8d5b6f4 100644 --- a/ngcsimlib/metaComponent.py +++ b/ngcsimlib/metaComponent.py @@ -1,7 +1,7 @@ from ngcsimlib.compartment import Compartment from ngcsimlib.utils import get_current_context from ngcsimlib.utils.help import Guides -from ngcsimlib.logger import debug +from ngcsimlib.logger import debug, warn class MetaComponent(type): @@ -82,7 +82,14 @@ def wrapped_init(self, *args, **kwargs): else: cls.pre_init(self, *args, **kwargs) x._orig_init(self, *args, **kwargs) + args_count = self._orig_init.__code__.co_argcount + _kwargs = self._orig_init.__code__.co_varnames[:args_count] + for key, value in kwargs.items(): + if key not in _kwargs: + debug(f"There is an extra param {key} in component constructor for {self.name}") cls.post_init(self, *args, **kwargs) + if hasattr(self, "_setup"): + self._setup() x.__init__ = wrapped_init