hpcflow-new2 0.2.0a175__py3-none-any.whl → 0.2.0a177__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,140 @@
1
+ from dataclasses import dataclass
2
+ from collections import defaultdict
3
+ from typing import Dict, List, Optional, Tuple
4
+
5
+ from hpcflow.sdk import app
6
+ from hpcflow.sdk.core.utils import nth_key
7
+ from hpcflow.sdk.log import TimeIt
8
+ from hpcflow.sdk.core.cache import DependencyCache
9
+
10
+
11
+ @dataclass
12
+ class LoopCache:
13
+ """Class to store a cache for use in `Workflow.add_empty_loop` and
14
+ `WorkflowLoop.add_iterations`.
15
+
16
+ Attributes
17
+ ----------
18
+ element_dependents
19
+ Keys are element IDs, values are dicts whose keys are element IDs that depend on
20
+ the key element ID (via `Element.get_dependent_elements_recursively`), and whose
21
+ values are dicts with keys: `group_names`, which is a tuple of the string group
22
+ names associated with the dependent element's element set.
23
+ elements
24
+ Keys are element IDs, values are dicts with keys: `input_statuses`,
25
+ `input_sources`, and `task_insert_ID`.
26
+ zeroth_iters
27
+ Keys are element IDs, values are data associated with the zeroth iteration of that
28
+ element, namely a tuple of iteration ID and `ElementIteration.data_idx`.
29
+ data_idx
30
+ Keys are element IDs, values are data associated with all iterations of that
31
+ element, namely a dict whose keys are the iteration loop index as a tuple, and
32
+ whose values are data indices via `ElementIteration.get_data_idx()`.
33
+ iterations
34
+ Keys are iteration IDs, values are tuples of element ID and iteration index within
35
+ that element.
36
+ task_iterations
37
+ Keys are task insert IDs, values are list of all iteration IDs associated with
38
+ that task.
39
+
40
+ """
41
+
42
+ element_dependents: Dict[int, Dict]
43
+ elements: Dict[int, Dict]
44
+ zeroth_iters: Dict[int, Tuple]
45
+ data_idx: Dict[int, Dict]
46
+ iterations: Dict[int, Tuple]
47
+ task_iterations: Dict[int, List[int]]
48
+
49
+ @TimeIt.decorator
50
+ def get_iter_IDs(self, loop: "app.Loop") -> List[int]:
51
+ """Retrieve a list of iteration IDs belonging to a given loop."""
52
+ return [j for i in loop.task_insert_IDs for j in self.task_iterations[i]]
53
+
54
+ @TimeIt.decorator
55
+ def get_iter_loop_indices(self, iter_IDs: List[int]) -> List[Dict[str, int]]:
56
+ iter_loop_idx = []
57
+ for i in iter_IDs:
58
+ elem_id, idx = self.iterations[i]
59
+ iter_loop_idx.append(dict(nth_key(self.data_idx[elem_id], idx)))
60
+ return iter_loop_idx
61
+
62
+ @TimeIt.decorator
63
+ def update_loop_indices(self, new_loop_name: str, iter_IDs: List[int]):
64
+ elem_ids = {v[0] for k, v in self.iterations.items() if k in iter_IDs}
65
+ for i in elem_ids:
66
+ new_item = {}
67
+ for k, v in self.data_idx[i].items():
68
+ new_k = dict(k)
69
+ new_k.update({new_loop_name: 0})
70
+ new_item[tuple(sorted(new_k.items()))] = v
71
+ self.data_idx[i] = new_item
72
+
73
+ @TimeIt.decorator
74
+ def add_iteration(self, iter_ID, task_insert_ID, element_ID, loop_idx, data_idx):
75
+ """Update the cache to include a newly added iteration."""
76
+ self.task_iterations[task_insert_ID].append(iter_ID)
77
+ new_iter_idx = len(self.data_idx[element_ID])
78
+ self.data_idx[element_ID][tuple(sorted(loop_idx.items()))] = data_idx
79
+ self.iterations[iter_ID] = (element_ID, new_iter_idx)
80
+
81
+ @classmethod
82
+ @TimeIt.decorator
83
+ def build(cls, workflow: "app.Workflow", loops: Optional[List["app.Loop"]] = None):
84
+ """Build a cache of data for use in adding loops and iterations."""
85
+
86
+ deps_cache = DependencyCache.build(workflow)
87
+
88
+ loops = list(workflow.template.loops) + (loops or [])
89
+ task_iIDs = set(j for i in loops for j in i.task_insert_IDs)
90
+ tasks = [workflow.tasks.get(insert_ID=i) for i in sorted(task_iIDs)]
91
+ elem_deps = {}
92
+
93
+ # keys: element IDs, values: dict with keys: tuple(loop_idx), values: data index
94
+ data_idx_cache = {}
95
+
96
+ # keys: iteration IDs, values: tuple of (element ID, integer index into values
97
+ # dict in `data_idx_cache` [accessed via `.keys()[index]`])
98
+ iters = {}
99
+
100
+ # keys: element IDs, values: dict with keys: "input_statues", "input_sources",
101
+ # "task_insert_ID":
102
+ elements = {}
103
+
104
+ zeroth_iters = {}
105
+ task_iterations = defaultdict(list)
106
+ for task in tasks:
107
+ for elem_idx in task.element_IDs:
108
+ element = deps_cache.elements[elem_idx]
109
+ inp_statuses = task.template.get_input_statuses(element.element_set)
110
+ elements[element.id_] = {
111
+ "input_statuses": inp_statuses,
112
+ "input_sources": element.input_sources,
113
+ "task_insert_ID": task.insert_ID,
114
+ }
115
+ elem_deps[element.id_] = {
116
+ i: {
117
+ "group_names": tuple(
118
+ j.name for j in deps_cache.elements[i].element_set.groups
119
+ ),
120
+ }
121
+ for i in deps_cache.elem_elem_dependents_rec[element.id_]
122
+ }
123
+ elem_iters = {}
124
+ for idx, iter_i in enumerate(element.iterations):
125
+ if idx == 0:
126
+ zeroth_iters[element.id_] = (iter_i.id_, iter_i.data_idx)
127
+ loop_idx_key = tuple(sorted(iter_i.loop_idx.items()))
128
+ elem_iters[loop_idx_key] = iter_i.get_data_idx()
129
+ task_iterations[task.insert_ID].append(iter_i.id_)
130
+ iters[iter_i.id_] = (element.id_, idx)
131
+ data_idx_cache[element.id_] = elem_iters
132
+
133
+ return cls(
134
+ element_dependents=elem_deps,
135
+ elements=elements,
136
+ zeroth_iters=zeroth_iters,
137
+ data_idx=data_idx_cache,
138
+ iterations=iters,
139
+ task_iterations=dict(task_iterations),
140
+ )
hpcflow/sdk/core/task.py CHANGED
@@ -2062,29 +2062,36 @@ class WorkflowTask:
2062
2062
  return element_dat_idx
2063
2063
 
2064
2064
  @TimeIt.decorator
2065
- def initialise_EARs(self) -> List[int]:
2065
+ def initialise_EARs(self, iter_IDs: Optional[List[int]] = None) -> List[int]:
2066
2066
  """Try to initialise any uninitialised EARs of this task."""
2067
+ if iter_IDs:
2068
+ iters = self.workflow.get_element_iterations_from_IDs(iter_IDs)
2069
+ else:
2070
+ iters = []
2071
+ for element in self.elements:
2072
+ # We don't yet cache Element objects, so `element`, and also it's
2073
+ # `ElementIterations, are transient. So there is no reason to update these
2074
+ # objects in memory to account for the new EARs. Subsequent calls to
2075
+ # `WorkflowTask.elements` will retrieve correct element data from the
2076
+ # store. This might need changing once/if we start caching Element
2077
+ # objects.
2078
+ iters.extend(element.iterations)
2079
+
2067
2080
  initialised = []
2068
- for element in self.elements[:]:
2069
- # We don't yet cache Element objects, so `element`, and also it's
2070
- # `ElementIterations, are transient. So there is no reason to update these
2071
- # objects in memory to account for the new EARs. Subsequent calls to
2072
- # `WorkflowTask.elements` will retrieve correct element data from the store.
2073
- # This might need changing once/if we start caching Element objects.
2074
- for iter_i in element.iterations:
2075
- if not iter_i.EARs_initialised:
2076
- try:
2077
- self._initialise_element_iter_EARs(iter_i)
2078
- initialised.append(iter_i.id_)
2079
- except UnsetParameterDataError:
2080
- # raised by `Action.test_rules`; cannot yet initialise EARs
2081
- self.app.logger.debug(
2082
- f"UnsetParameterDataError raised: cannot yet initialise runs."
2083
- )
2084
- pass
2085
- else:
2086
- iter_i._EARs_initialised = True
2087
- self.workflow.set_EARs_initialised(iter_i.id_)
2081
+ for iter_i in iters:
2082
+ if not iter_i.EARs_initialised:
2083
+ try:
2084
+ self._initialise_element_iter_EARs(iter_i)
2085
+ initialised.append(iter_i.id_)
2086
+ except UnsetParameterDataError:
2087
+ # raised by `Action.test_rules`; cannot yet initialise EARs
2088
+ self.app.logger.debug(
2089
+ f"UnsetParameterDataError raised: cannot yet initialise runs."
2090
+ )
2091
+ pass
2092
+ else:
2093
+ iter_i._EARs_initialised = True
2094
+ self.workflow.set_EARs_initialised(iter_i.id_)
2088
2095
  return initialised
2089
2096
 
2090
2097
  @TimeIt.decorator
@@ -2097,7 +2104,6 @@ class WorkflowTask:
2097
2104
  param_src_updates = {}
2098
2105
 
2099
2106
  count = 0
2100
- # TODO: generator is an IO op here, can be pre-calculated/cached?
2101
2107
  for act_idx, action in self.template.all_schema_actions():
2102
2108
  log_common = (
2103
2109
  f"for action {act_idx} of element iteration {element_iter.index} of "
@@ -2151,8 +2157,7 @@ class WorkflowTask:
2151
2157
  metadata={},
2152
2158
  )
2153
2159
 
2154
- for pid, src in param_src_updates.items():
2155
- self.workflow._store.update_param_source(pid, src)
2160
+ self.workflow._store.update_param_source(param_src_updates)
2156
2161
 
2157
2162
  @TimeIt.decorator
2158
2163
  def _add_element_set(self, element_set):
hpcflow/sdk/core/utils.py CHANGED
@@ -3,7 +3,7 @@ import enum
3
3
  from functools import wraps
4
4
  import contextlib
5
5
  import hashlib
6
- from itertools import accumulate
6
+ from itertools import accumulate, islice
7
7
  import json
8
8
  import keyword
9
9
  import os
@@ -871,3 +871,13 @@ def dict_values_process_flat(d, callable):
871
871
  out[k] = proc_idx_k
872
872
 
873
873
  return out
874
+
875
+
876
+ def nth_key(dct, n):
877
+ it = iter(dct)
878
+ next(islice(it, n, n), None)
879
+ return next(it)
880
+
881
+
882
+ def nth_value(dct, n):
883
+ return dct[nth_key(dct, n)]
@@ -25,6 +25,7 @@ from hpcflow.sdk.core import (
25
25
  ABORT_EXIT_CODE,
26
26
  )
27
27
  from hpcflow.sdk.core.actions import EARStatus
28
+ from hpcflow.sdk.core.loop_cache import LoopCache
28
29
  from hpcflow.sdk.log import TimeIt
29
30
  from hpcflow.sdk.persistence import store_cls_from_str, DEFAULT_STORE_FORMAT
30
31
  from hpcflow.sdk.persistence.base import TEMPLATE_COMP_TYPES, AnySEAR
@@ -41,6 +42,7 @@ from hpcflow.sdk.submission.schedulers.direct import DirectScheduler
41
42
  from hpcflow.sdk.typing import PathLike
42
43
  from hpcflow.sdk.core.json_like import ChildObjectSpec, JSONLike
43
44
  from .utils import (
45
+ nth_key,
44
46
  read_JSON_file,
45
47
  read_JSON_string,
46
48
  read_YAML_str,
@@ -625,19 +627,28 @@ class Workflow:
625
627
  )
626
628
  with wk._store.cached_load():
627
629
  with wk.batch_update(is_workflow_creation=True):
628
- for idx, task in enumerate(template.tasks):
630
+ with wk._store.cache_ctx():
631
+ for idx, task in enumerate(template.tasks):
632
+ if status:
633
+ status.update(
634
+ f"Adding task {idx + 1}/{len(template.tasks)} "
635
+ f"({task.name!r})..."
636
+ )
637
+ wk._add_task(task)
629
638
  if status:
630
639
  status.update(
631
- f"Adding task {idx + 1}/{len(template.tasks)} "
632
- f"({task.name!r})..."
640
+ f"Preparing to add {len(template.loops)} loops..."
633
641
  )
634
- wk._add_task(task)
635
- for idx, loop in enumerate(template.loops):
636
- if status:
637
- status.update(
638
- f"Adding loop {idx + 1}/" f"{len(template.loops)}..."
639
- )
640
- wk._add_loop(loop)
642
+ if template.loops:
643
+ # TODO: if loop with non-initialisable actions, will fail
644
+ cache = LoopCache.build(workflow=wk, loops=template.loops)
645
+ for idx, loop in enumerate(template.loops):
646
+ if status:
647
+ status.update(
648
+ f"Adding loop {idx + 1}/"
649
+ f"{len(template.loops)} ({loop.name!r})"
650
+ )
651
+ wk._add_loop(loop, cache=cache, status=status)
641
652
  except Exception:
642
653
  if status:
643
654
  status.stop()
@@ -1101,7 +1112,7 @@ class Workflow:
1101
1112
 
1102
1113
  @TimeIt.decorator
1103
1114
  def _add_empty_loop(
1104
- self, loop: app.Loop
1115
+ self, loop: app.Loop, cache: LoopCache
1105
1116
  ) -> Tuple[app.WorkflowLoop, List[app.ElementIteration]]:
1106
1117
  """Add a new loop (zeroth iterations only) to the workflow."""
1107
1118
 
@@ -1114,15 +1125,15 @@ class Workflow:
1114
1125
  self.template._add_empty_loop(loop_c)
1115
1126
 
1116
1127
  # all these element iterations will be initialised for the new loop:
1117
- iters = self.get_element_iterations_of_tasks(loop_c.task_insert_IDs)
1118
- iter_IDs = [i.id_ for i in iters]
1128
+ iter_IDs = cache.get_iter_IDs(loop_c)
1129
+ iter_loop_idx = cache.get_iter_loop_indices(iter_IDs)
1119
1130
 
1120
1131
  # create and insert a new WorkflowLoop:
1121
1132
  new_loop = self.app.WorkflowLoop.new_empty_loop(
1122
1133
  index=new_index,
1123
1134
  workflow=self,
1124
1135
  template=loop_c,
1125
- iterations=iters,
1136
+ iter_loop_idx=iter_loop_idx,
1126
1137
  )
1127
1138
  self.loops.add_object(new_loop)
1128
1139
  wk_loop = self.loops[new_index]
@@ -1144,15 +1155,28 @@ class Workflow:
1144
1155
 
1145
1156
  self._pending["loops"].append(new_index)
1146
1157
 
1158
+ # update cache loop indices:
1159
+ cache.update_loop_indices(new_loop_name=loop_c.name, iter_IDs=iter_IDs)
1160
+
1147
1161
  return wk_loop
1148
1162
 
1149
1163
  @TimeIt.decorator
1150
- def _add_loop(self, loop: app.Loop) -> None:
1151
- new_wk_loop = self._add_empty_loop(loop)
1164
+ def _add_loop(
1165
+ self, loop: app.Loop, cache: Optional[Dict] = None, status: Optional[Any] = None
1166
+ ) -> None:
1167
+ if not cache:
1168
+ cache = LoopCache.build(workflow=self, loops=[loop])
1169
+ new_wk_loop = self._add_empty_loop(loop, cache)
1152
1170
  if loop.num_iterations is not None:
1153
1171
  # fixed number of iterations, so add remaining N > 0 iterations:
1154
- for _ in range(loop.num_iterations - 1):
1155
- new_wk_loop.add_iteration()
1172
+ if status:
1173
+ status_prev = status.status
1174
+ for iter_idx in range(loop.num_iterations - 1):
1175
+ if status:
1176
+ status.update(
1177
+ f"{status_prev}: iteration {iter_idx + 2}/{loop.num_iterations}."
1178
+ )
1179
+ new_wk_loop.add_iteration(cache=cache)
1156
1180
 
1157
1181
  def add_loop(self, loop: app.Loop) -> None:
1158
1182
  """Add a loop to a subset of workflow tasks."""
@@ -1326,6 +1350,7 @@ class Workflow:
1326
1350
  iters.append(iter_i)
1327
1351
  return iters
1328
1352
 
1353
+ @TimeIt.decorator
1329
1354
  def get_elements_from_IDs(self, id_lst: Iterable[int]) -> List[app.Element]:
1330
1355
  """Return element objects from a list of IDs."""
1331
1356
 
@@ -1334,6 +1359,7 @@ class Workflow:
1334
1359
  task_IDs = [i.task_ID for i in store_elems]
1335
1360
  store_tasks = self._store.get_tasks_by_IDs(task_IDs)
1336
1361
 
1362
+ element_idx_by_task = defaultdict(set)
1337
1363
  index_paths = []
1338
1364
  for el, tk in zip(store_elems, store_tasks):
1339
1365
  elem_idx = tk.element_IDs.index(el.id_)
@@ -1343,15 +1369,23 @@ class Workflow:
1343
1369
  "task_idx": tk.index,
1344
1370
  }
1345
1371
  )
1372
+ element_idx_by_task[tk.index].add(elem_idx)
1373
+
1374
+ elements_by_task = {}
1375
+ for task_idx, elem_idx in element_idx_by_task.items():
1376
+ task = self.tasks[task_idx]
1377
+ elements_by_task[task_idx] = dict(
1378
+ zip(elem_idx, task.elements[list(elem_idx)])
1379
+ )
1346
1380
 
1347
1381
  objs = []
1348
1382
  for idx_dat in index_paths:
1349
- task = self.tasks[idx_dat["task_idx"]]
1350
- elem = task.elements[idx_dat["elem_idx"]]
1383
+ elem = elements_by_task[idx_dat["task_idx"]][idx_dat["elem_idx"]]
1351
1384
  objs.append(elem)
1352
1385
 
1353
1386
  return objs
1354
1387
 
1388
+ @TimeIt.decorator
1355
1389
  def get_element_iterations_from_IDs(
1356
1390
  self, id_lst: Iterable[int]
1357
1391
  ) -> List[app.ElementIteration]:
@@ -1365,6 +1399,8 @@ class Workflow:
1365
1399
  task_IDs = [i.task_ID for i in store_elems]
1366
1400
  store_tasks = self._store.get_tasks_by_IDs(task_IDs)
1367
1401
 
1402
+ element_idx_by_task = defaultdict(set)
1403
+
1368
1404
  index_paths = []
1369
1405
  for it, el, tk in zip(store_iters, store_elems, store_tasks):
1370
1406
  iter_idx = el.iteration_IDs.index(it.id_)
@@ -1376,11 +1412,18 @@ class Workflow:
1376
1412
  "task_idx": tk.index,
1377
1413
  }
1378
1414
  )
1415
+ element_idx_by_task[tk.index].add(elem_idx)
1416
+
1417
+ elements_by_task = {}
1418
+ for task_idx, elem_idx in element_idx_by_task.items():
1419
+ task = self.tasks[task_idx]
1420
+ elements_by_task[task_idx] = dict(
1421
+ zip(elem_idx, task.elements[list(elem_idx)])
1422
+ )
1379
1423
 
1380
1424
  objs = []
1381
1425
  for idx_dat in index_paths:
1382
- task = self.tasks[idx_dat["task_idx"]]
1383
- elem = task.elements[idx_dat["elem_idx"]]
1426
+ elem = elements_by_task[idx_dat["task_idx"]][idx_dat["elem_idx"]]
1384
1427
  iter_ = elem.iterations[idx_dat["iter_idx"]]
1385
1428
  objs.append(iter_)
1386
1429
 
@@ -716,6 +716,11 @@ class PersistentStore(ABC):
716
716
  """Cache for number of persistent tasks."""
717
717
  return self._cache["num_tasks"]
718
718
 
719
+ @property
720
+ def num_EARs_cache(self):
721
+ """Cache for total number of persistent EARs."""
722
+ return self._cache["num_EARs"]
723
+
719
724
  @property
720
725
  def param_sources_cache(self):
721
726
  """Cache for persistent parameter sources."""
@@ -730,6 +735,10 @@ class PersistentStore(ABC):
730
735
  def num_tasks_cache(self, value):
731
736
  self._cache["num_tasks"] = value
732
737
 
738
+ @num_EARs_cache.setter
739
+ def num_EARs_cache(self, value):
740
+ self._cache["num_EARs"] = value
741
+
733
742
  def _reset_cache(self):
734
743
  self._cache = {
735
744
  "tasks": {},
@@ -739,6 +748,7 @@ class PersistentStore(ABC):
739
748
  "param_sources": {},
740
749
  "num_tasks": None,
741
750
  "parameters": {},
751
+ "num_EARs": None,
742
752
  }
743
753
 
744
754
  @contextlib.contextmanager
@@ -873,6 +883,7 @@ class PersistentStore(ABC):
873
883
  """Get the total number of persistent and pending element iterations."""
874
884
  return self._get_num_persistent_elem_iters() + len(self._pending.add_elem_iters)
875
885
 
886
+ @TimeIt.decorator
876
887
  def _get_num_total_EARs(self):
877
888
  """Get the total number of persistent and pending EARs."""
878
889
  return self._get_num_persistent_EARs() + len(self._pending.add_EARs)
@@ -1296,9 +1307,11 @@ class PersistentStore(ABC):
1296
1307
  self.save()
1297
1308
 
1298
1309
  @TimeIt.decorator
1299
- def update_param_source(self, param_id: int, source: Dict, save: bool = True) -> None:
1300
- self.logger.debug(f"Updating parameter ID {param_id!r} source to {source!r}.")
1301
- self._pending.update_param_sources[param_id] = source
1310
+ def update_param_source(
1311
+ self, param_sources: Dict[int, Dict], save: bool = True
1312
+ ) -> None:
1313
+ self.logger.debug(f"Updating parameter sources with {param_sources!r}.")
1314
+ self._pending.update_param_sources.update(param_sources)
1302
1315
  if save:
1303
1316
  self.save()
1304
1317
 
@@ -303,12 +303,13 @@ class JSONPersistentStore(PersistentStore):
303
303
 
304
304
  def _get_num_persistent_tasks(self) -> int:
305
305
  """Get the number of persistent tasks."""
306
- if self.num_tasks_cache is not None:
306
+ if self.use_cache and self.num_tasks_cache is not None:
307
307
  num = self.num_tasks_cache
308
308
  else:
309
309
  with self.using_resource("metadata", action="read") as md:
310
310
  num = len(md["tasks"])
311
- self.num_tasks_cache = num
311
+ if self.use_cache and self.num_tasks_cache is None:
312
+ self.num_tasks_cache = num
312
313
  return num
313
314
 
314
315
  def _get_num_persistent_loops(self) -> int:
@@ -333,8 +334,14 @@ class JSONPersistentStore(PersistentStore):
333
334
 
334
335
  def _get_num_persistent_EARs(self) -> int:
335
336
  """Get the number of persistent EARs."""
336
- with self.using_resource("metadata", action="read") as md:
337
- return len(md["runs"])
337
+ if self.use_cache and self.num_EARs_cache is not None:
338
+ num = self.num_EARs_cache
339
+ else:
340
+ with self.using_resource("metadata", action="read") as md:
341
+ num = len(md["runs"])
342
+ if self.use_cache and self.num_EARs_cache is None:
343
+ self.num_EARs_cache = num
344
+ return num
338
345
 
339
346
  def _get_num_persistent_parameters(self):
340
347
  with self.using_resource("parameters", "read") as params:
@@ -275,6 +275,7 @@ class PendingChanges:
275
275
  EAR_ids = list(self.add_EARs.keys())
276
276
  self.logger.debug(f"commit: adding pending EARs with IDs: {EAR_ids!r}")
277
277
  self.store._append_EARs(EARs)
278
+ self.store.num_EARs_cache = None # invalidate cache
278
279
  # pending start/end times/snapshots, submission indices, and skips that belong
279
280
  # to pending EARs are now committed (accounted for in `get_EARs` above):
280
281
  self.set_EAR_submission_indices = {
@@ -408,6 +409,7 @@ class PendingChanges:
408
409
  @TimeIt.decorator
409
410
  def commit_loop_indices(self) -> None:
410
411
  """Make pending update to element iteration loop indices persistent."""
412
+ # TODO: batch up
411
413
  for iter_ID, loop_idx in self.update_loop_indices.items():
412
414
  self.logger.debug(
413
415
  f"commit: updating loop indices of iteration ID {iter_ID!r} with "
@@ -774,9 +774,16 @@ class ZarrPersistentStore(PersistentStore):
774
774
  """Get the number of persistent element iterations."""
775
775
  return len(self._get_iters_arr())
776
776
 
777
+ @TimeIt.decorator
777
778
  def _get_num_persistent_EARs(self) -> int:
778
779
  """Get the number of persistent EARs."""
779
- return len(self._get_EARs_arr())
780
+ if self.use_cache and self.num_EARs_cache is not None:
781
+ num = self.num_EARs_cache
782
+ else:
783
+ num = len(self._get_EARs_arr())
784
+ if self.use_cache and self.num_EARs_cache is None:
785
+ self.num_EARs_cache = num
786
+ return num
780
787
 
781
788
  def _get_num_persistent_parameters(self):
782
789
  return len(self._get_parameter_base_array())