Skip to content

Commit

Permalink
tools/mpremote: Add readline support to mount.
Browse files Browse the repository at this point in the history
This significantly speeds up readline on files opened
directly from a mpremote mount.

Signed-off-by: Andrew Leech <[email protected]>
  • Loading branch information
Andrew Leech committed Oct 31, 2023
1 parent 6f55e00 commit 473a1dd
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions tools/mpremote/mpremote/transport_serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -380,13 +380,14 @@ def umount_local(self):
"CMD_ILISTDIR_NEXT": 3,
"CMD_OPEN": 4,
"CMD_CLOSE": 5,
"CMD_READ": 6,
"CMD_WRITE": 7,
"CMD_SEEK": 8,
"CMD_REMOVE": 9,
"CMD_RENAME": 10,
"CMD_MKDIR": 11,
"CMD_RMDIR": 12,
"CMD_READLINE": 6,
"CMD_READ": 7,
"CMD_WRITE": 8,
"CMD_SEEK": 9,
"CMD_REMOVE": 10,
"CMD_RENAME": 11,
"CMD_MKDIR": 12,
"CMD_RMDIR": 13,
}

fs_hook_code = """\
Expand Down Expand Up @@ -562,12 +563,14 @@ def readinto(self, buf):
return n
def readline(self):
l = ''
while 1:
c = self.read(1)
l += c
if c == '\\n' or c == '':
return l
c = self.cmd
c.begin(CMD_READLINE)
c.wr_s8(self.fd)
n = c.rd_u32()
buf = bytearray(n)
c.rd_bytes(buf)
c.end()
return bytes(buf)
def readlines(self):
ls = []
Expand Down Expand Up @@ -854,6 +857,15 @@ def do_read(self):
self.wr_bytes(buf)
# self.log_cmd(f"read {fd} {n} -> {len(buf)}")

def do_readline(self):
fd = self.rd_s8()
buf = self.data_files[fd][0].readline()
self.wr_u32(len(buf))
if self.data_files[fd][1]:
buf = bytes(buf, "utf8")
self.wr_bytes(buf)
# self.log_cmd(f"readline {fd} -> {len(buf)}")

def do_seek(self):
fd = self.rd_s8()
n = self.rd_s32()
Expand Down Expand Up @@ -927,6 +939,7 @@ def do_rmdir(self):
fs_hook_cmds["CMD_OPEN"]: do_open,
fs_hook_cmds["CMD_CLOSE"]: do_close,
fs_hook_cmds["CMD_READ"]: do_read,
fs_hook_cmds["CMD_READLINE"]: do_readline,
fs_hook_cmds["CMD_WRITE"]: do_write,
fs_hook_cmds["CMD_SEEK"]: do_seek,
fs_hook_cmds["CMD_REMOVE"]: do_remove,
Expand Down

0 comments on commit 473a1dd

Please sign in to comment.