← index #2552Issue #4331
Related · high · value 0.257
QUERY · ISSUE

Add .offset()/.seek() method for memoryview for inplace "slicing"

openby pfalconopened 2016-10-23updated 2018-12-01
enhancementrfc

Here's an idiom to send a large data buffer thru a short-write stream (e.g., non-blocking socket):

        buffer = memoryview(data).cast('b')
        while buffer:
            try:
                nsent = self._socket_send(buffer, flags)
                buffer = buffer[nsent:]

The last statement still produces bunch of object garbage. If we could do

buffer.offset(nsent)

it would be cool. Suggestions for better name are welcome.

CANDIDATE · ISSUE

Standardizing on an (extension) method to reinitialize an object inplace

openby pfalconopened 2018-12-01updated 2018-12-01

It's well-known that a good way to fight memory fragmentation is preallocation and reuse of buffers objects. "And reuse" clause should not be underestimated, because that's the real action which happens there - a preallocated object is reused, using inplace operations. This is easy for objects like buffers, but this idea can be generalized beyond them. One case is being talked at https://github.com/micropython/micropython/issues/2552#issuecomment-262707357 - reusing memoryview with another buffer. But it goes beyond that still. E.g. reusing a closed socket to connect again without allocating a new instance. Reusing uzlib.DecompIO to decompress another stream. Etc.

Related:
https://github.com/micropython/micropython/issues/2622, https://github.com/micropython/micropython/issues/2552, https://github.com/micropython/micropython/issues/2180, https://github.com/micropython/micropython/issues/917

P.S. I understand this has zero chance being implemented in the mainline, I'm going to implement this in my fork. Posting here for wider community review.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied