Last active
July 8, 2025 09:19
-
-
Save polyvertex/b1f78221ab8587b24bdd9f9a00d421b7 to your computer and use it in GitHub Desktop.
entro.py - simple entropy check to evaluate whether compression is worth it
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from collections import Counter | |
from typing import Union | |
def should_compress( | |
data: Union[bytes | bytearray | memoryview], | |
/, *, | |
threshold: int = 128) -> bool: | |
# do not compress if buffer size is < threshold | |
if len(data) < threshold: | |
return False | |
# count unique bytes | |
frequency = Counter(data) | |
# compress if less than 90% unique bytes | |
return (float(len(frequency)) / float(min(len(data), 256))) < 0.9 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment