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

Commit

Permalink
fix: Avoid collecting unnecessary snapshots
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Arnau Siches committed May 29, 2019
1 parent 05f51ca commit 8626345
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions registers/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ def slice(log: Log, start_position: int) -> List[Command]:
return commands


Result = Tuple[Optional[Entry], Optional[ValidationError]]


def _collect_command(command: Command,
data: Log,
metadata: Log,
Expand All @@ -198,26 +201,24 @@ def _collect_command(command: Command,
raise OrphanEntry(entry)

if entry.scope == Scope.System:
metadata.insert(blob)
record = metadata.snapshot().get(entry.key)
(_, err) = _collect_entry(entry, record)

if err and not relaxed:
errors.append(err)
else:
metadata.insert(entry)
_collect_pair(entry, blob, metadata, errors, relaxed)

else:
data.insert(blob)
record = data.snapshot().get(entry.key)
(_, err) = _collect_entry(entry, record)
_collect_pair(entry, blob, data, errors, relaxed)

if err and not relaxed:
errors.append(err)
else:
data.insert(entry)

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

Result = Tuple[Optional[Entry], Optional[ValidationError]]
if not relaxed:
record = log.snapshot().get(entry.key)
(_, err) = _collect_entry(entry, record)

if err:
errors.append(err)

log.insert(entry)


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

0 comments on commit 8626345

Please sign in to comment.