• Kirill Smelkov's avatar
    golang_str: Switch bstr/ustr to cdef classes · 4546aaec
    Kirill Smelkov authored
    For gpython to switch builtin str/unicode to bstr/ustr we will need
    bstr/ustr to have exactly the same C layout as builtin string types.
    This is possible to achieve only via `cdef class`. It is also good to
    switch to `cdef class` for RAM savings - from https://github.com/cython/cython/pull/5212#issuecomment-1387659026 :
    
        # what Cython does at runtime for `class MyBytes(bytes)`
        In [3]: MyBytes = type('MyBytes', (bytes,), {'__slots__': ()})
    
        In [4]: MyBytes
        Out[4]: __main__.MyBytes
    
        In [5]: a = bytes(b'123')
    
        In [6]: b = MyBytes(b'123')
    
        In [7]: a
        Out[7]: b'123'
    
        In [8]: b
        Out[8]: b'123'
    
        In [9]: a == b
        Out[9]: True
    
        In [10]: import sys
    
        In [11]: sys.getsizeof(a)
        Out[11]: 36
    
        In [12]: sys.getsizeof(b)
        Out[12]: 52
    
    So with `cdef class` we gain more control and optimize memory usage.
    
    This was not done before because cython forbids to `cdef class X(bytes)` due to
    https://github.com/cython/cython/issues/711. We work it around in setup.py with
    draft for proper patch pre-posted to upstream in https://github.com/cython/cython/pull/5212 .
    4546aaec
setup.py 15.8 KB