Skip to content
This repository was archived by the owner on Mar 15, 2021. It is now read-only.

Commit 648be07

Browse files
author
Arnau Siches
committed
fix: Avoid collecting unnecessary snapshots
When the logs are collected in relaxed mode it does not need to compute the current snapshot for every single entry. This fix speeds up large registers by ~2x. Signed-off-by: Arnau Siches <arnau.siches@digital.cabinet-office.gov.uk>
1 parent 767c4c6 commit 648be07

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

‎registers/log.py‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ def slice(log: Log, start_position: int) -> List[Command]:
175175
return commands
176176

177177

178+
Result = Tuple[Optional[Entry], Optional[ValidationError]]
179+
180+
178181
def _collect_command(command: Command,
179182
data: Log,
180183
metadata: Log,
@@ -198,26 +201,24 @@ def _collect_command(command: Command,
198201
raise OrphanEntry(entry)
199202

200203
if entry.scope == Scope.System:
201-
metadata.insert(blob)
202-
record = metadata.snapshot().get(entry.key)
203-
(_, err) = _collect_entry(entry, record)
204-
205-
if err and not relaxed:
206-
errors.append(err)
207-
else:
208-
metadata.insert(entry)
204+
_collect_pair(entry, blob, metadata, errors, relaxed)
205+
209206
else:
210-
data.insert(blob)
211-
record = data.snapshot().get(entry.key)
212-
(_, err) = _collect_entry(entry, record)
207+
_collect_pair(entry, blob, data, errors, relaxed)
213208

214-
if err and not relaxed:
215-
errors.append(err)
216-
else:
217-
data.insert(entry)
218209

210+
def _collect_pair(entry: Entry, blob: Blob, log: Log,
211+
errors: List[ValidationError], relaxed: bool):
212+
log.insert(blob)
219213

220-
Result = Tuple[Optional[Entry], Optional[ValidationError]]
214+
if not relaxed:
215+
record = log.snapshot().get(entry.key)
216+
(_, err) = _collect_entry(entry, record)
217+
218+
if err:
219+
errors.append(err)
220+
221+
log.insert(entry)
221222

222223

223224
def _collect_entry(entry: Entry, record: Optional[Record]) -> Result:

0 commit comments

Comments
 (0)