← index #6105PR #8797
Related · high · value 1.105
QUERY · ISSUE

uasyncio V3 stream readline: is this correct?

openby peterhinchopened 2020-06-02updated 2020-06-04
extmod

The readline method returns if the final character is a newline. However it is possible that self.s.readline() might return more than one line. Should Stream.readline find the first newline and return a partial result? CPython docs:

Read one line, where “line” is a sequence of bytes ending with \n.

In the existing design the onus is on the device s to return a maximum of one line. Is this correct?

CANDIDATE · PULL REQUEST

extmod/uasyncio: Use list of bytes in readexactly() and readline() of StreamReader.

closedby AmirHmZzopened 2022-06-21updated 2023-06-17
extmod

This PR changes the way StreamReader.readexactly() and StreamReader.readline() stash data in order to read a certain number of bytes and effects on projects which use readexactly() and readline() in their specific protocol. The new method creates a list and append retrieved values from stream to the list and joins them at the end. I also do a benchmark in order to compare these two method. The benchmark source is accessible from here and the results on ESP-32-WROOM32 running on micropython1.19 were:

MAX_PART_SIZES = 128 ( 10 calls to s.read() )
1000 calls to readexactly1 to read 1024 bytes took 230085437 CPU Ticks
1000 calls to readexactly2 to read 1024 bytes took 181515566 CPU Ticks
Calling readexactly1 to read 1024B allocated 6032B of RAM
Calling readexactly2 to read 1024B allocated 2384B of RAM

MAX_PART_SIZES = 256 ( 5 calls to s.read() )
1000 calls to readexactly1 to read 1024 bytes took 146521271 CPU Ticks
1000 calls to readexactly2 to read 1024 bytes took 128840148 CPU Ticks
Calling readexactly1 to read 1024B allocated 3600B of RAM
Calling readexactly2 to read 1024B allocated 2240B of RAM

MAX_PART_SIZES = 1024 ( 1 call to s.read() )
1000 calls to readexactly1 to read 1024 bytes took 89548003 CPU Ticks
1000 calls to readexactly2 to read 1024 bytes took 79781526 CPU Ticks
Calling readexactly1 to read 1024B allocated 1104B of RAM
Calling readexactly2 to read 1024B allocated 1088B of RAM

readexactly1 is the current implementation and readexactly2 is the PR implementation of readexactly() method. As you can see multiple calls to the s.read() can result multiple concatenating bytes process and allocating another bytes object with bigger size each time. This method also allocates less RAM which can reduce RAM fragmentation.

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