kopia lustrzana https://github.com/micropython/micropython-lib
usb-device-cdc: Optimise writing small data so it doesn't require alloc.
Only allocate a memoryview when the (first) write was partial. Signed-off-by: Matthias Urlichs <matthias@urlichs.de>pull/991/head
rodzic
05a56c3cad
commit
9307e21dfb
|
@ -1,3 +1,3 @@
|
|||
metadata(version="0.1.1")
|
||||
metadata(version="0.1.2")
|
||||
require("usb-device")
|
||||
package("usb")
|
||||
|
|
|
@ -350,10 +350,9 @@ class CDCInterface(io.IOBase, Interface):
|
|||
###
|
||||
|
||||
def write(self, buf):
|
||||
# use a memoryview to track how much of 'buf' we've written so far
|
||||
# (unfortunately, this means a 1 block allocation for each write, but it's otherwise allocation free.)
|
||||
start = time.ticks_ms()
|
||||
mv = memoryview(buf)
|
||||
mv = buf
|
||||
|
||||
while True:
|
||||
# Keep pushing buf into _wb into it's all gone
|
||||
nbytes = self._wb.write(mv)
|
||||
|
@ -362,6 +361,10 @@ class CDCInterface(io.IOBase, Interface):
|
|||
if nbytes == len(mv):
|
||||
return len(buf) # Success
|
||||
|
||||
# if buf couldn't be fully written on the first attempt
|
||||
# convert it to a memoryview to track partial writes
|
||||
if mv is buf:
|
||||
mv = memoryview(buf)
|
||||
mv = mv[nbytes:]
|
||||
|
||||
# check for timeout
|
||||
|
|
Ładowanie…
Reference in New Issue