Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions src/codomyrmex/data_curation/minhash.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,17 @@ def _shingle(self, text: str) -> set[int]:
shingles = set()
for i in range(len(text) - self.shingle_size + 1):
shingle = text[i : i + self.shingle_size]
h = int(
hashlib.md5(shingle.encode(), usedforsecurity=False).hexdigest(), 16
) % self._p
# Optimization: Bypassing hex string allocation (`hexdigest()`) and using
# `int.from_bytes` on the full digest is ~3x faster. We use "big" endian
# to match the mathematical value of int(hex_string, 16) exactly,
# preserving backward compatibility of all generated signatures.
h = (
int.from_bytes(
hashlib.md5(shingle.encode(), usedforsecurity=False).digest(),
"big",
)
% self._p
)
shingles.add(h)
return shingles or {0} # Prevent empty set

Expand Down
Loading