Skip to content

Commit 4551fe3

Browse files
committed
Remove memoryview usage
Since `struct.unpack_from` is already an offset-based approach at the C level, and `result.extend` requires data copy anyway, avoiding a `memoryview` wrapper prevents redundant Python object allocation and pointer-shifting overhead, yielding optimal runtime memory footprint and CPU performance.
1 parent 4eae06b commit 4551fe3

1 file changed

Lines changed: 7 additions & 8 deletions

File tree

Lib/zipfile/__init__.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2717,20 +2717,19 @@ def _strip_extra_fields(data, field_ids):
27172717
return result
27182718

27192719
# use memoryview for zero-copy slices
2720-
mv = memoryview(data)
2721-
mv_len = len(mv)
2720+
data_len = len(data)
27222721
pos = 0
2723-
while pos + 4 <= mv_len:
2724-
xid, xlen = struct.unpack_from('<HH', mv, pos)
2725-
if pos + 4 + xlen > mv_len:
2722+
while pos + 4 <= data_len:
2723+
xid, xlen = struct.unpack_from('<HH', data, pos)
2724+
if pos + 4 + xlen > data_len:
27262725
break
27272726
if xid not in field_ids:
2728-
result.extend(mv[pos:pos + 4 + xlen])
2727+
result.extend(data[pos:pos + 4 + xlen])
27292728
pos += 4 + xlen
27302729

27312730
# keep remaining trailing bytes (e.g. truncated or malformed data)
2732-
if pos < mv_len:
2733-
result.extend(mv[pos:])
2731+
if pos < data_len:
2732+
result.extend(data[pos:])
27342733

27352734
return result
27362735

0 commit comments

Comments
 (0)