[TupleVariation] Slightly speed up compileCoords

Small but measurable speedup.
This commit is contained in:
Behdad Esfahbod 2023-10-14 12:09:13 -04:00
parent 9eeee75b03
commit cc09d5f381

View File

@ -166,15 +166,15 @@ class TupleVariation(object):
return b"".join(tupleData), auxData return b"".join(tupleData), auxData
def compileCoord(self, axisTags): def compileCoord(self, axisTags):
result = bytearray() result = []
axes = self.axes axes = self.axes
for axis in axisTags: for axis in axisTags:
triple = axes.get(axis) triple = axes.get(axis)
if triple is None: if triple is None:
result.extend(b"\0\0") result.append(b"\0\0")
else: else:
result.extend(struct.pack(">h", fl2fi(triple[1], 14))) result.append(struct.pack(">h", fl2fi(triple[1], 14)))
return bytes(result) return b"".join(result)
def compileIntermediateCoord(self, axisTags): def compileIntermediateCoord(self, axisTags):
needed = False needed = False
@ -187,13 +187,13 @@ class TupleVariation(object):
break break
if not needed: if not needed:
return None return None
minCoords = bytearray() minCoords = []
maxCoords = bytearray() maxCoords = []
for axis in axisTags: for axis in axisTags:
minValue, value, maxValue = self.axes.get(axis, (0.0, 0.0, 0.0)) minValue, value, maxValue = self.axes.get(axis, (0.0, 0.0, 0.0))
minCoords.extend(struct.pack(">h", fl2fi(minValue, 14))) minCoords.append(struct.pack(">h", fl2fi(minValue, 14)))
maxCoords.extend(struct.pack(">h", fl2fi(maxValue, 14))) maxCoords.append(struct.pack(">h", fl2fi(maxValue, 14)))
return minCoords + maxCoords return b"".join(minCoords + maxCoords)
@staticmethod @staticmethod
def decompileCoord_(axisTags, data, offset): def decompileCoord_(axisTags, data, offset):
@ -802,7 +802,7 @@ def inferRegion_(peak):
intermediateEndTuple fields. intermediateEndTuple fields.
""" """
start, end = {}, {} start, end = {}, {}
for (axis, value) in peak.items(): for axis, value in peak.items():
start[axis] = min(value, 0.0) # -0.3 --> -0.3; 0.7 --> 0.0 start[axis] = min(value, 0.0) # -0.3 --> -0.3; 0.7 --> 0.0
end[axis] = max(value, 0.0) # -0.3 --> 0.0; 0.7 --> 0.7 end[axis] = max(value, 0.0) # -0.3 --> 0.0; 0.7 --> 0.7
return (start, end) return (start, end)