diff options
| author | Lex Neva <github.com@lexneva.name> | 2022-08-04 21:17:41 -0400 |
|---|---|---|
| committer | Lex Neva <github.com@lexneva.name> | 2023-02-18 22:34:47 -0500 |
| commit | 2865f4161e4b2e8dc63177ef60a83820ea4f761d (patch) | |
| tree | 6fb58104c7d6e51c56ec4c673f0c835f95c10ee3 /lib | |
| parent | 98bc2e2ff9c843a64c3db355290ed541e6708312 (diff) | |
consistent cache key for Stitch objects
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/elements/element.py | 25 | ||||
| -rw-r--r-- | lib/stitch_plan/stitch.py | 30 |
2 files changed, 40 insertions, 15 deletions
diff --git a/lib/elements/element.py b/lib/elements/element.py index 7164c17c..84a9199b 100644 --- a/lib/elements/element.py +++ b/lib/elements/element.py @@ -396,7 +396,15 @@ class EmbroideryElement(object): # we don't care about the previous stitch previous_stitch = None - return get_stitch_plan_cache().get(self._get_cache_key(previous_stitch)) + cache_key = self._get_cache_key(previous_stitch) + stitch_groups = get_stitch_plan_cache().get(cache_key) + + if stitch_groups: + debug.log(f"used cache for {self.node.get('id')} {self.node.get(INKSCAPE_LABEL)}") + else: + debug.log(f"did not use cache for {self.node.get('id')} {self.node.get(INKSCAPE_LABEL)}, key={cache_key}") + + return stitch_groups def uses_previous_stitch(self): """Returns True if the previous stitch can affect this Element's stitches. @@ -408,12 +416,16 @@ class EmbroideryElement(object): @debug.time def _save_cached_stitch_groups(self, stitch_groups, previous_stitch): stitch_plan_cache = get_stitch_plan_cache() - stitch_plan_cache[self._get_cache_key(previous_stitch)] = stitch_groups + cache_key = self._get_cache_key(previous_stitch) + if cache_key not in stitch_plan_cache: + stitch_plan_cache[cache_key] = stitch_groups if previous_stitch is not None: # Also store it with None as the previous stitch, so that it can be used next time # if we don't care about the previous stitch - stitch_plan_cache[self._get_cache_key(None)] = stitch_groups + cache_key = self._get_cache_key(None) + if cache_key not in stitch_plan_cache: + stitch_plan_cache[cache_key] = stitch_groups def get_params_and_values(self): params = {} @@ -436,11 +448,13 @@ class EmbroideryElement(object): cache_key_generator.update([(c.command, c.target_point) for c in self.commands]) cache_key_generator.update(self._get_patterns_cache_key_data()) - # TODO: include commands and patterns that apply to this element + cache_key = cache_key_generator.get_cache_key() + debug.log(f"cache key for {self.node.get('id')} {self.node.get(INKSCAPE_LABEL)} {previous_stitch}: {cache_key}") - return cache_key_generator.get_cache_key() + return cache_key def embroider(self, last_stitch_group): + debug.log(f"starting {self.node.get('id')} {self.node.get(INKSCAPE_LABEL)}") if last_stitch_group: previous_stitch = last_stitch_group.stitches[-1] else: @@ -463,6 +477,7 @@ class EmbroideryElement(object): self._save_cached_stitch_groups(stitch_groups, previous_stitch) + debug.log(f"ending {self.node.get('id')} {self.node.get(INKSCAPE_LABEL)}") return stitch_groups def fatal(self, message, point_to_troubleshoot=False): diff --git a/lib/stitch_plan/stitch.py b/lib/stitch_plan/stitch.py index 67a0fd64..c1553ce5 100644 --- a/lib/stitch_plan/stitch.py +++ b/lib/stitch_plan/stitch.py @@ -55,16 +55,17 @@ class Stitch(Point): return instance def __repr__(self): - return "Stitch(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" % (self.x, - self.y, - self.color, - "JUMP" if self.jump else " ", - "TRIM" if self.trim else " ", - "STOP" if self.stop else " ", - "TIE MODUS" if self.tie_modus else " ", - "FORCE LOCK STITCHES" if self.force_lock_stitches else " ", - "NO TIES" if self.no_ties else " ", - "COLOR CHANGE" if self.color_change else " ") + return "Stitch(%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)" % (self.x, + self.y, + self.color, + self.tags, + "JUMP" if self.jump else " ", + "TRIM" if self.trim else " ", + "STOP" if self.stop else " ", + self.tie_modus, + "FORCE LOCK STITCHES" if self.force_lock_stitches else " ", + "NO TIES" if self.no_ties else " ", + "COLOR CHANGE" if self.color_change else " ") def _set(self, attribute, value, base_stitch): # Set an attribute. If the caller passed a Stitch object, use its value, unless @@ -104,3 +105,12 @@ class Stitch(Point): attributes = dict(vars(self)) attributes['tags'] = list(attributes['tags']) return attributes + + def __getstate__(self): + # This is used by pickle. We want to sort the tag list so that the + # pickled representation is stable, since it's used to generate cache + # keys. + state = self.__json__() + state['tags'].sort() + + return state |
