Skip to content

Commit fe0bc26

Browse files
committed
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 78728dc commit fe0bc26

File tree

1 file changed

+31
-14
lines changed

1 file changed

+31
-14
lines changed

tools/mpremote/mpremote/transport_serial.py

Lines changed: 31 additions & 14 deletions
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,15 +751,18 @@ 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)
751754
fs_hook_code = re.sub(" *#.*$", "", fs_hook_code, flags=re.MULTILINE)
752755
fs_hook_code = re.sub("\n\n+", "\n", fs_hook_code)
753756
fs_hook_code = re.sub(" ", " ", fs_hook_code)
754757
fs_hook_code = re.sub("rd_", "r", fs_hook_code)
755758
fs_hook_code = re.sub("wr_", "w", fs_hook_code)
756759
fs_hook_code = re.sub("buf4", "b4", fs_hook_code)
757760

761+
# Replace hook command names with the integer command index.
762+
# Handle keys in reverse sort order to to avoid eg. READ replacing half of READLINE
763+
for key, value in reversed(sorted(fs_hook_cmds.items())):
764+
fs_hook_code = re.sub(key, str(value), fs_hook_code)
765+
758766

759767
class PyboardCommand:
760768
def __init__(self, fin, fout, path, unsafe_links=False):
@@ -887,6 +895,14 @@ def do_read(self):
887895
self.wr_bytes(buf)
888896
# self.log_cmd(f"read {fd} {n} -> {len(buf)}")
889897

898+
def do_readline(self):
899+
fd = self.rd_s8()
900+
buf = self.data_files[fd][0].readline()
901+
if self.data_files[fd][1]:
902+
buf = bytes(buf, "utf8")
903+
self.wr_bytes(buf)
904+
# self.log_cmd(f"readline {fd} -> {len(buf)}")
905+
890906
def do_seek(self):
891907
fd = self.rd_s8()
892908
n = self.rd_s32()
@@ -960,6 +976,7 @@ def do_rmdir(self):
960976
fs_hook_cmds["CMD_OPEN"]: do_open,
961977
fs_hook_cmds["CMD_CLOSE"]: do_close,
962978
fs_hook_cmds["CMD_READ"]: do_read,
979+
fs_hook_cmds["CMD_READLINE"]: do_readline,
963980
fs_hook_cmds["CMD_WRITE"]: do_write,
964981
fs_hook_cmds["CMD_SEEK"]: do_seek,
965982
fs_hook_cmds["CMD_REMOVE"]: do_remove,

0 commit comments

Comments
 (0)