Skip to content

Commit

Permalink
Try to make bytes-XORing faster
Browse files Browse the repository at this point in the history
  • Loading branch information
jwodder committed Feb 21, 2022
1 parent 91b87d7 commit 7c1ca60
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/fscacher/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
from hashlib import md5
from inspect import Parameter, signature
import logging
from operator import xor
import os
import os.path as op
import shutil
import sys
import time
import appdirs
import joblib
Expand Down Expand Up @@ -200,14 +200,14 @@ def __init__(self):
self.hash_ords = None

def add_file(self, path, fprint: FileFingerprint):
fprint_hash = list(
md5(ascii((str(path), fprint.to_tuple())).encode("us-ascii")).digest()
)
fprint_hash = md5(
ascii((str(path), fprint.to_tuple())).encode("us-ascii")
).digest()
if self.hash_ords is None:
self.hash_ords = fprint_hash
self.last_modified = fprint.mtime_ns
else:
self.hash_ords = list(map(xor, self.hash_ords, fprint_hash))
self.hash_ords = xor_bytes(self.hash_ords, fprint_hash)
if self.last_modified < fprint.mtime_ns:
self.last_modified = fprint.mtime_ns

Expand All @@ -222,3 +222,10 @@ def to_tuple(self):
return (None,)
else:
return (bytes(self.hash_ords).hex(),)


def xor_bytes(b1: bytes, b2: bytes) -> bytes:
length = max(len(b1), len(b2))
i1 = int.from_bytes(b1, sys.byteorder)
i2 = int.from_bytes(b2, sys.byteorder)
return (i1 ^ i2).to_bytes(length, sys.byteorder)
16 changes: 16 additions & 0 deletions src/fscacher/tests/test_util.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import pytest
from ..cache import xor_bytes


@pytest.mark.parametrize(
"b1,b2,r",
[
(b"\x12", b"\x34", b"\x26"),
(b"\0\x12", b"\0\x34", b"\0\x26"),
(b"\x12\0", b"\x34\0", b"\x26\0"),
(b"\x12\xAB", b"\x34", b"\x26\xAB"),
(b"\x12\xAB", b"\x34\xCD", b"\x26\x66"),
],
)
def test_xor_bytes(b1: bytes, b2: bytes, r: bytes) -> None:
assert xor_bytes(b1, b2) == r

0 comments on commit 7c1ca60

Please sign in to comment.