You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.
Usage of sys.getsizeof is fundamentally incorrect to measuring message length. getsizeof returns the internal size of the Python object, not the actual encoded size of the final message. TL;DR; it kind of works for ascii, but utterly breaks down for unicode strings.
Here's a quick demonstration from an interactive shell:
>>>sys.getsizeof('') # Basic empty string takes up 49 bytes49>>>sys.getsizeof('For ascii string it kind of works, but not really')
98>>>sys.getsizeof('Let\'s add some ünicode to themix, 👌🏻')
220>>># Quickly the sizeof spikes, as it is the internal representation after all.>>># Let's try again by not relying on how the string is implemented behind the>>># scenes. Instead we encode the string to a byte array, which most likely>>># has less magic and grows in size more predictably.>>>sys.getsizeof('For ascii string it kind of works, but not really'.encode('utf8'))
82>>>sys.getsizeof('Let\'s add some ünicode to themix, 👌🏻'.encode('utf8'))
76>>># Much better, but in this context, getsizeof really shouldn't be used,>>># instead we should be counting the bytes that will be actually used up>>># when sending the pushes.>>>len('For ascii string it kind of works, but not really'.encode('utf8'))
49>>>len('Let\'s add some ünicode to themix, 👌🏻'.encode('utf8'))
43
The text was updated successfully, but these errors were encountered:
In platform_strategy.py:
Usage of sys.getsizeof is fundamentally incorrect to measuring message length. getsizeof returns the internal size of the Python object, not the actual encoded size of the final message. TL;DR; it kind of works for ascii, but utterly breaks down for unicode strings.
Here's a quick demonstration from an interactive shell:
The text was updated successfully, but these errors were encountered: