Skip to content

Commit 86526e9

Browse files
pi-anldpgeorge
authored andcommitted
tools/mpremote: Optimise readline support in mount.
This significantly speeds up readline on files opened directly from an mpremote mount. Signed-off-by: Andrew Leech <[email protected]>
1 parent 5fdd249 commit 86526e9

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

tools/mpremote/mpremote/transport_serial.py

+27-14
Original file line numberDiff line numberDiff line change
@@ -404,12 +404,13 @@ def umount_local(self):
404404
"CMD_OPEN": 4,
405405
"CMD_CLOSE": 5,
406406
"CMD_READ": 6,
407-
"CMD_WRITE": 7,
408-
"CMD_SEEK": 8,
409-
"CMD_REMOVE": 9,
410-
"CMD_RENAME": 10,
411-
"CMD_MKDIR": 11,
412-
"CMD_RMDIR": 12,
407+
"CMD_READLINE": 7,
408+
"CMD_WRITE": 8,
409+
"CMD_SEEK": 9,
410+
"CMD_REMOVE": 10,
411+
"CMD_RENAME": 11,
412+
"CMD_MKDIR": 12,
413+
"CMD_RMDIR": 13,
413414
}
414415

415416
fs_hook_code = """\
@@ -592,12 +593,16 @@ def readinto(self, buf):
592593
return n
593594
594595
def readline(self):
595-
l = ''
596-
while 1:
597-
c = self.read(1)
598-
l += c
599-
if c == '\\n' or c == '':
600-
return l
596+
c = self.cmd
597+
c.begin(CMD_READLINE)
598+
c.wr_s8(self.fd)
599+
data = c.rd_bytes(None)
600+
c.end()
601+
if self.is_text:
602+
data = str(data, 'utf8')
603+
else:
604+
data = bytes(data)
605+
return data
601606
602607
def readlines(self):
603608
ls = []
@@ -746,8 +751,7 @@ def __mount():
746751
"""
747752

748753
# Apply basic compression on hook code.
749-
for key, value in fs_hook_cmds.items():
750-
fs_hook_code = re.sub(key, str(value), fs_hook_code)
754+
fs_hook_code = re.sub(r"CMD_[A-Z_]+", lambda m: str(fs_hook_cmds[m.group(0)]), fs_hook_code)
751755
fs_hook_code = re.sub(" *#.*$", "", fs_hook_code, flags=re.MULTILINE)
752756
fs_hook_code = re.sub("\n\n+", "\n", fs_hook_code)
753757
fs_hook_code = re.sub(" ", " ", fs_hook_code)
@@ -887,6 +891,14 @@ def do_read(self):
887891
self.wr_bytes(buf)
888892
# self.log_cmd(f"read {fd} {n} -> {len(buf)}")
889893

894+
def do_readline(self):
895+
fd = self.rd_s8()
896+
buf = self.data_files[fd][0].readline()
897+
if self.data_files[fd][1]:
898+
buf = bytes(buf, "utf8")
899+
self.wr_bytes(buf)
900+
# self.log_cmd(f"readline {fd} -> {len(buf)}")
901+
890902
def do_seek(self):
891903
fd = self.rd_s8()
892904
n = self.rd_s32()
@@ -960,6 +972,7 @@ def do_rmdir(self):
960972
fs_hook_cmds["CMD_OPEN"]: do_open,
961973
fs_hook_cmds["CMD_CLOSE"]: do_close,
962974
fs_hook_cmds["CMD_READ"]: do_read,
975+
fs_hook_cmds["CMD_READLINE"]: do_readline,
963976
fs_hook_cmds["CMD_WRITE"]: do_write,
964977
fs_hook_cmds["CMD_SEEK"]: do_seek,
965978
fs_hook_cmds["CMD_REMOVE"]: do_remove,

0 commit comments

Comments
 (0)