Skip to content

Commit 05027d9

Browse files
Andrew Leechpi-anl
authored andcommitted
tools/mpremote: Add readline support to mount.
This significantly speeds up readline on files opened directly from a mpremote mount. Signed-off-by: Andrew Leech <[email protected]>
1 parent 7924b31 commit 05027d9

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

tools/mpremote/mpremote/transport_serial.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -382,13 +382,14 @@ def umount_local(self):
382382
"CMD_ILISTDIR_NEXT": 3,
383383
"CMD_OPEN": 4,
384384
"CMD_CLOSE": 5,
385-
"CMD_READ": 6,
386-
"CMD_WRITE": 7,
387-
"CMD_SEEK": 8,
388-
"CMD_REMOVE": 9,
389-
"CMD_RENAME": 10,
390-
"CMD_MKDIR": 11,
391-
"CMD_RMDIR": 12,
385+
"CMD_READLINE": 6,
386+
"CMD_READ": 7,
387+
"CMD_WRITE": 8,
388+
"CMD_SEEK": 9,
389+
"CMD_REMOVE": 10,
390+
"CMD_RENAME": 11,
391+
"CMD_MKDIR": 12,
392+
"CMD_RMDIR": 13,
392393
}
393394

394395
fs_hook_code = """\
@@ -571,12 +572,14 @@ def readinto(self, buf):
571572
return n
572573
573574
def readline(self):
574-
l = ''
575-
while 1:
576-
c = self.read(1)
577-
l += c
578-
if c == '\\n' or c == '':
579-
return l
575+
c = self.cmd
576+
c.begin(CMD_READLINE)
577+
c.wr_s8(self.fd)
578+
n = c.rd_u32()
579+
buf = bytearray(n)
580+
c.rd_bytes(buf)
581+
c.end()
582+
return bytes(buf)
580583
581584
def readlines(self):
582585
ls = []
@@ -866,6 +869,15 @@ def do_read(self):
866869
self.wr_bytes(buf)
867870
# self.log_cmd(f"read {fd} {n} -> {len(buf)}")
868871

872+
def do_readline(self):
873+
fd = self.rd_s8()
874+
buf = self.data_files[fd][0].readline()
875+
self.wr_u32(len(buf))
876+
if self.data_files[fd][1]:
877+
buf = bytes(buf, "utf8")
878+
self.wr_bytes(buf)
879+
# self.log_cmd(f"readline {fd} -> {len(buf)}")
880+
869881
def do_seek(self):
870882
fd = self.rd_s8()
871883
n = self.rd_s32()
@@ -939,6 +951,7 @@ def do_rmdir(self):
939951
fs_hook_cmds["CMD_OPEN"]: do_open,
940952
fs_hook_cmds["CMD_CLOSE"]: do_close,
941953
fs_hook_cmds["CMD_READ"]: do_read,
954+
fs_hook_cmds["CMD_READLINE"]: do_readline,
942955
fs_hook_cmds["CMD_WRITE"]: do_write,
943956
fs_hook_cmds["CMD_SEEK"]: do_seek,
944957
fs_hook_cmds["CMD_REMOVE"]: do_remove,

0 commit comments

Comments
 (0)