Cython: optimize native Python memoryview

风格不统一 提交于 2019-12-05 16:06:35

This answer linked by @CodeSurgeon is a possibility to do it. However, since Cython 0.28 we have a much cleaner way - the typed read-only memoryviews:

%%cython
mv = memoryview(b'1234')
cdef const unsigned char[:] tmv=mv  #"const" is possible since Cython 0.28

Obviously you can only read from this memoryview (this is a Good Thing) and there is no copying involved.

You could also say: but this is unsigned char and not char! True - this is also a Good Thing: bytes are unsigned chars and the whole point of typed memoryviews that you aren't mixing up the types!

Another reason I think the linked solution is kind of dangerous - you have the whole power of C to shoot yourself in the foot, because it casts types and constness away. See for example:

%%cython    
def bad_things(a):
   cdef const unsigned char[:] safe=a
   cdef char *unsafe=<char *> &safe[0] #who needs const and types anyway?
   unsafe[0]=52   #replace through `4`

And now:

 >>> A=b'a'
 >>> bad_things(A)
 >>> print(A)   #take it Python - I pwned your immutability!
 b'4'
 >>> print(b'a')
 b'4'
 >>> #Help! What are you doing Python?

Because Python has a pool of small strings, which are immutable (or so the Python thinks), and we have changed the object to which b'a' is linked to b'4' we should brace ourself for funny results and happy debugging...

In overall, this is a no-brainer: stick to typed memoryviews which guarantee type- and constness-safety.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!