Skip to content

Instantly share code, notes, and snippets.

@polyvertex
Last active July 8, 2025 09:19
Show Gist options
  • Save polyvertex/b1f78221ab8587b24bdd9f9a00d421b7 to your computer and use it in GitHub Desktop.
Save polyvertex/b1f78221ab8587b24bdd9f9a00d421b7 to your computer and use it in GitHub Desktop.
entro.py - simple entropy check to evaluate whether compression is worth it
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