From a7a38be4de3bcb2aba3bfcb2d602375d1c194036 Mon Sep 17 00:00:00 2001 From: Fabien Roger Date: Sat, 19 Feb 2022 15:43:04 +0100 Subject: [PATCH 1/2] :beetle: Prevent checkpoint when the scene has not changed --- pyflow/scene/history.py | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/pyflow/scene/history.py b/pyflow/scene/history.py index 7cd4ec79..c296079f 100644 --- a/pyflow/scene/history.py +++ b/pyflow/scene/history.py @@ -3,7 +3,7 @@ """ Module for the handling an OCBScene history. """ -from typing import TYPE_CHECKING +from typing import TYPE_CHECKING, Optional, OrderedDict import logging from pyflow.core.history import History @@ -25,6 +25,7 @@ class SceneHistory(History): def __init__(self, scene: "Scene", max_stack: int = 50): self.scene = scene + self._hash: Optional[str] = None super().__init__(max_stack) def checkpoint(self, description: str, set_modified=True): @@ -35,9 +36,18 @@ def checkpoint(self, description: str, set_modified=True): set_modified: Whether the scene should be considered modified. """ + + serialized_scene = self.scene.serialize() + + new_serialized_scene_hash = hash(str(serialized_scene)) + if not self.should_checkpoint(new_serialized_scene_hash): + return + + self._hash = new_serialized_scene_hash + history_stamp = { "description": description, - "snapshot": self.scene.serialize(), + "snapshot": serialized_scene, } self.store(history_stamp) if set_modified: @@ -52,3 +62,15 @@ def restore(self): snapshot = stamp["snapshot"] logger.debug("Restored [%s]: %s", self.current, stamp["description"]) self.scene.deserialize(snapshot) + + def should_checkpoint(self, new_serialized_scene_hash: str) -> bool: + """Return true if a checkpoint should be created. + + This is not the case when the previous checkpoint is the same as the new one. + This is the case if there was no previous hash + (as it is the case when the scene is first loaded).""" + + if self._hash is None: + return True + + return self._hash != new_serialized_scene_hash From 012ad40f6c98676136dc50c62f84b0a1bded9441 Mon Sep 17 00:00:00 2001 From: Fabien Roger Date: Sat, 19 Feb 2022 16:59:19 +0100 Subject: [PATCH 2/2] :sparkles: Remove useless import --- pyflow/scene/history.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyflow/scene/history.py b/pyflow/scene/history.py index c296079f..fe4883f9 100644 --- a/pyflow/scene/history.py +++ b/pyflow/scene/history.py @@ -3,7 +3,7 @@ """ Module for the handling an OCBScene history. """ -from typing import TYPE_CHECKING, Optional, OrderedDict +from typing import TYPE_CHECKING, Optional import logging from pyflow.core.history import History