Skip to content

Commit 7aac86c

Browse files
committed
Small optimization of RLE decoding to avoid temporary lists.
list is a generic container with indirection for every element's type. This makes it slower than a bytearray for basically any operation. >>> timeit("a.extend(a[1:2] * 6024)", "a = bytearray(b'123' * 1024)", globals=globals()) 7.540333299897611 >>> timeit("a.extend([a[1]] * 6024)", "a = bytearray(b'123' * 1024)", globals=globals()) 35.999150899937376
1 parent 6d9ddfd commit 7aac86c

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

‎gimpformats/GimpImageLevel.py‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,20 @@ def _decodeRLE(self, data: bytearray, pixels: int, bpp: int, index: int = 0) ->
127127
index += 1
128128

129129
if 0 <= opcode <= 126: # Short run of identical bytes
130-
val = data[index]
130+
val = data[index:index+1] # use 1-len bytearray to avoid slow list
131131
index += 1
132-
ret[chan].extend([val] * (opcode + 1)) # Extend is faster than append in a loop
132+
ret[chan].extend(val * (opcode + 1)) # Extend is faster than append in a loop
133133
n += opcode + 1
134134

135135
elif opcode == 127: # Long run of identical bytes
136136
m = data[index]
137137
index += 1
138138
b = data[index]
139139
index += 1
140-
val = data[index]
140+
val = data[index:index+1] # use 1-len bytearray to avoid slow list
141141
index += 1
142142
amt = (m << 8) + b
143-
ret[chan].extend([val] * amt)
143+
ret[chan].extend(val * amt)
144144
n += amt
145145

146146
elif opcode == 128: # Long run of different bytes

0 commit comments

Comments
 (0)