Skip to content

Commit d062933

Browse files
CompactAIOfficialCTO Agentclaude
authored
perf: skip rewriting files untouched by in-place anonymization (#22)
In-place anonymization always replaced the destination with the temp file even when no line changed, rewriting every clean trace and churning mtimes. Track whether any line was actually modified and drop the temp file when an in-place run left the content identical; apply the same check to whole-text (non-jsonl) files. Co-authored-by: CTO Agent <cto@paperclip.com> Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
1 parent ff476f3 commit d062933

1 file changed

Lines changed: 12 additions & 3 deletions

File tree

src/teich/anonymize.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ def _anonymize_file(source: Path, destination: Path) -> AnonymizeFileReport:
107107
shutil.copy2(source, destination)
108108
return AnonymizeFileReport(path=source, output_path=destination)
109109
text = anonymizer.anonymize_text(original)
110-
destination.write_text(text, encoding="utf-8")
110+
if text != original or source.resolve() != destination.resolve():
111+
destination.write_text(text, encoding="utf-8")
111112

112113
return AnonymizeFileReport(
113114
path=source,
@@ -132,6 +133,7 @@ def _anonymize_jsonl_file(source: Path, destination: Path, anonymizer: "TraceAno
132133
) as temp_handle,
133134
):
134135
temp_path = Path(temp_handle.name)
136+
changed = False
135137
for raw_line in handle:
136138
stripped = raw_line.strip()
137139
if not stripped:
@@ -140,12 +142,16 @@ def _anonymize_jsonl_file(source: Path, destination: Path, anonymizer: "TraceAno
140142
try:
141143
value = json.loads(raw_line)
142144
except json.JSONDecodeError:
143-
temp_handle.write(anonymizer.anonymize_text(raw_line))
145+
anonymized_line = anonymizer.anonymize_text(raw_line)
146+
if anonymized_line != raw_line:
147+
changed = True
148+
temp_handle.write(anonymized_line)
144149
continue
145150
anonymized = anonymizer.anonymize_value(value)
146151
if anonymized == value:
147152
temp_handle.write(raw_line)
148153
continue
154+
changed = True
149155
line = json.dumps(anonymized, ensure_ascii=False, separators=(",", ":"))
150156
line = line.replace("\u0085", "\\u0085").replace("\u2028", "\\u2028").replace("\u2029", "\\u2029")
151157
temp_handle.write(line + "\n")
@@ -161,7 +167,10 @@ def _anonymize_jsonl_file(source: Path, destination: Path, anonymizer: "TraceAno
161167
)
162168
return
163169
if temp_path is not None:
164-
temp_path.replace(destination)
170+
if not changed and source.resolve() == destination.resolve():
171+
temp_path.unlink()
172+
else:
173+
temp_path.replace(destination)
165174

166175

167176
class TraceAnonymizer:

0 commit comments

Comments
 (0)