From bb14f3a443eb356acdfc41829890f00a71d18e88 Mon Sep 17 00:00:00 2001 From: Arnau Siches Date: Wed, 29 May 2019 08:28:15 +0100 Subject: [PATCH] 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 --- registers/log.py | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/registers/log.py b/registers/log.py index 27cd5ff..a27b85a 100644 --- a/registers/log.py +++ b/registers/log.py @@ -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, @@ -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: