- cpython/Objects/bytesobject.c
- cpython/Include/bytesobject.h
- cpython/Objects/clinic/bytesobject.c.h
The memory layout of PyBytesObject looks like memory layout of tuple object and memory layout of int object, but simpler than any of them.
bytes object is an immutable object, whenever you need to modify a bytes object, you need to create a new one, which keeps the implementation simple.
s = b""Let's initialize a bytes object with ASCII characters
s = b"abcdefg123"s = "我是帅哥".encode("utf8")The field ob_shash should store the hash value of the byte object, value -1 means not computed yet.
The first time the hash value is computed, it will be cached in the ob_shash field.
The cached hash value can save recalculation and speed up dictionary lookups
The field ob_size is inside every PyVarObject. PyBytesObject uses this field to store size information to keep O(1) time complexity for the len() operation and to track the size of non-ASCII strings (which may contain null characters inside)
The PyBytesObject is a Python wrapper of C-style null-terminated strings, with ob_shash for caching the hash value and ob_size for storing size information
The implementation of PyBytesObject looks like the embstr encoding in redis
redis-cli
127.0.0.1:6379> set a "hello"
OK
127.0.0.1:6379> object encoding a
"embstr"



