Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time, Scheduler: cache creation of pint unit_registry Units #92

Merged
merged 1 commit into from
Apr 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/graph_scheduler/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@
All, AllHaveRun, Always, Condition, ConditionSet, EveryNCalls, Never,
_parse_absolute_unit, _quantity_as_integer,
)
from graph_scheduler.time import Clock, TimeScale
from graph_scheduler.time import _get_pint_unit, Clock, TimeScale

__all__ = [
'Scheduler', 'SchedulerError', 'SchedulingMode',
Expand Down Expand Up @@ -419,7 +419,7 @@ def __init__(
termination_conds=None,
default_execution_id=None,
mode: SchedulingMode = SchedulingMode.STANDARD,
default_absolute_time_unit: typing.Union[str, pint.Quantity] = 1 * _unit_registry.ms,
default_absolute_time_unit: typing.Union[str, pint.Quantity] = _get_pint_unit(1, 'ms'),
**kwargs
):
"""
Expand Down
16 changes: 12 additions & 4 deletions src/graph_scheduler/time.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@
"""


# use a cached call because each call to getattr on a pint unit_registry
# creates new instances of Unit, and this becomes a significant time
# cost compared even to deepcopying
@functools.lru_cache()
def _get_pint_unit(num, unit):
return num * getattr(_unit_registry, unit)


class TimeScaleError(Exception):

def __init__(self, error_value):
Expand Down Expand Up @@ -232,8 +240,8 @@ def __init__(
environment_state_update=0,
environment_sequence=0,
life=0,
absolute=0 * _unit_registry.ms,
absolute_interval=1 * _unit_registry.ms,
absolute=_get_pint_unit(0, 'ms'),
absolute_interval=_get_pint_unit(1, 'ms'),
absolute_time_unit_scale=TimeScale.CONSIDERATION_SET_EXECUTION,
absolute_enabled=False,
**alias_time_values,
Expand All @@ -252,8 +260,8 @@ def __init__(
**{
_time_scale_to_attr_str(ts): v for ts, v in time_scale_values.items()
},
absolute=0 * _unit_registry.ms,
absolute_interval=1 * _unit_registry.ms,
absolute=_get_pint_unit(0, 'ms'),
absolute_interval=_get_pint_unit(1, 'ms'),
absolute_time_unit_scale=TimeScale.CONSIDERATION_SET_EXECUTION,
absolute_enabled=False,
)
Expand Down