Skip to content

Commit f7a7f20

Browse files
committed
Cache using .npz instead of .json
1 parent 9f28974 commit f7a7f20

File tree

1 file changed

+10
-31
lines changed

1 file changed

+10
-31
lines changed

auto_editor/analyze.py

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
import numpy as np
1010

1111
from auto_editor import version
12-
from auto_editor.lang.json import Lexer, Parser, dump
1312
from auto_editor.utils.subtitle_tools import convert_ass_to_text
1413
from auto_editor.wavfile import read
1514

@@ -145,49 +144,29 @@ def all(self) -> NDArray[np.bool_]:
145144

146145
def read_cache(self, tag: str, obj: dict[str, Any]) -> None | np.ndarray:
147146
workfile = os.path.join(
148-
os.path.dirname(self.temp), f"ae-{version}", "cache.json"
147+
os.path.dirname(self.temp), f"ae-{version}", "cache.npz"
149148
)
150149

151150
try:
152-
with open(workfile, encoding="utf-8") as file:
153-
cache = Parser(Lexer(workfile, file)).expr()
154-
except Exception:
151+
npzfile = np.load(workfile, allow_pickle=False)
152+
except Exception as e:
153+
self.log.debug(e)
155154
return None
156155

157-
if f"{self.src.path.resolve()}" not in cache:
156+
key = f"{self.src.path}:{obj_tag(tag, self.tb, obj)}"
157+
if key not in npzfile.files:
158158
return None
159159

160-
key = obj_tag(tag, self.tb, obj)
161-
162-
if key not in (root := cache[f"{self.src.path.resolve()}"]):
163-
return None
164-
165-
return np.asarray(root[key]["arr"], dtype=root[key]["type"])
160+
self.log.debug("Using cache")
161+
return npzfile[key]
166162

167163
def cache(self, tag: str, obj: dict[str, Any], arr: np.ndarray) -> np.ndarray:
168164
workdur = os.path.join(os.path.dirname(self.temp), f"ae-{version}")
169-
workfile = os.path.join(workdur, "cache.json")
170165
if not os.path.exists(workdur):
171166
os.mkdir(workdur)
172167

173-
key = obj_tag(tag, self.tb, obj)
174-
175-
try:
176-
with open(workfile, encoding="utf-8") as file:
177-
json_object = Parser(Lexer(workfile, file)).expr()
178-
except Exception:
179-
json_object = {}
180-
181-
entry = {"type": str(arr.dtype), "arr": arr.tolist()}
182-
src_key = f"{self.src.path}"
183-
184-
if src_key in json_object:
185-
json_object[src_key][key] = entry
186-
else:
187-
json_object[src_key] = {key: entry}
188-
189-
with open(os.path.join(workdur, "cache.json"), "w", encoding="utf-8") as file:
190-
dump(json_object, file)
168+
tag = obj_tag(tag, self.tb, obj)
169+
np.savez(os.path.join(workdur, "cache.npz"), **{f"{self.src.path}:{tag}": arr})
191170

192171
return arr
193172

0 commit comments

Comments
 (0)