现在,我正在使用字符串、StringIO 或 cStringIO 缓冲字节。但是,我经常需要从缓冲区的左侧删除字节。一种天真的方法会重建整个缓冲区。如果左截断是一种非常常见的操作,是否有最佳方法来做到这一点?Python 的垃圾收集器实际上应该对截断的字节进行 GC。
任何类型的算法(将缓冲区分成小块?)或现有的实现,都会有帮助。
编辑:
我尝试为此使用 Python 2.7 的 memoryview,但遗憾的是,当删除原始引用时,“视图”之外的数据不会被 GC:
# (This will use ~2GB of memory, not 50MB)
memoryview # Requires Python 2.7+
smalls = []
for i in xrange(10):
big = memoryview('z'*(200*1000*1000))
small = big[195*1000*1000:]
del big
smalls.append(small)
print '.',