perf: optimize all_zeros using fast bytes comparison (#3078)

* perf: optimize all_zeros using fast bytes comparison
This commit is contained in:
Mike Hunhoff
2026-05-18 02:20:10 -06:00
committed by GitHub
parent db0e1536ce
commit ced180ddbc
2 changed files with 7 additions and 2 deletions
+1
View File
@@ -115,6 +115,7 @@
- fix: replace assert with isinstance guard in get_callee for invalid MethodSpec tokens @williballenthin (SURF-41)
- fix: incorrect bytes() constructor usage in buf_filled_with @mike-hunhoff #3077
- fix: remove redundant code related to cli loading @mike-hunhoff #3076
- fix: optimize all_zeros using fast bytes comparison @mike-hunhoff #3078
### capa Explorer Web
+6 -2
View File
@@ -14,7 +14,6 @@
import struct
import builtins
from typing import Iterator
MIN_STACKSTRING_LEN = 8
@@ -108,7 +107,12 @@ def reformat_forwarded_export_name(forwarded_name: str) -> str:
def all_zeros(bytez: bytes) -> bool:
return all(b == 0 for b in builtins.bytes(bytez))
# Using `bytez == b'\x00' * len(bytez)` is much faster than `all(b == 0 for b in bytez)`
# because it relies on the optimized C implementation of bytes comparison.
# While it creates a temporary bytes object, the buffers passed here are small
# (typically capped at MAX_BYTES_FEATURE_SIZE = 256 bytes), so the memory overhead is negligible.
bytez = bytes(bytez)
return bytez == b"\x00" * len(bytez)
def twos_complement(val: int, bits: int) -> int: