Skip to content

Commit

Permalink
Merge pull request #22 from borgbackup/struct-optimize
Browse files Browse the repository at this point in the history
struct: only parse value_format once
  • Loading branch information
ThomasWaldmann authored Oct 27, 2024
2 parents ddc0e85 + e411674 commit fa2cf4a
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
2 changes: 1 addition & 1 deletion borghash.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cdef class HashTable:

cdef class HashTableNT:
cdef int key_size
cdef str value_format
cdef object value_type
cdef object value_struct
cdef int value_size
cdef HashTable inner
10 changes: 5 additions & 5 deletions borghash.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,8 @@ cdef class HashTableNT:
if value_type is None:
raise ValueError("value_type must be specified (a namedtuple type corresponding to value_format).")
self.key_size = key_size
self.value_format = value_format
self.value_size = struct.calcsize(self.value_format)
self.value_struct = struct.Struct(value_format)
self.value_size = self.value_struct.size
self.value_type = value_type
self.inner = HashTable(key_size=self.key_size, value_size=self.value_size, capacity=capacity)
_fill(self, items)
Expand All @@ -369,10 +369,10 @@ cdef class HashTableNT:
value = self.value_type(*value)
else:
raise TypeError(f"Expected an instance of {self.value_type}, got {type(value)}")
return struct.pack(self.value_format, *value)
return self.value_struct.pack(*value)

def _to_namedtuple_value(self, binary_value: bytes) -> Any:
unpacked_data = struct.unpack(self.value_format, binary_value)
unpacked_data = self.value_struct.unpack(binary_value)
return self.value_type(*unpacked_data)

def _set_raw(self, key: bytes, value: bytes) -> None:
Expand Down Expand Up @@ -460,7 +460,7 @@ cdef class HashTableNT:
meta = {
'key_size': self.key_size,
'value_size': self.value_size,
'value_format': self.value_format,
'value_format': self.value_struct.format,
'value_type_name': self.value_type.__name__,
'value_type_fields': self.value_type._fields,
'capacity': self.inner.capacity,
Expand Down

0 comments on commit fa2cf4a

Please sign in to comment.