Skip to content

Commit 038ff7b

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 038ff7b

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

tools/mpremote/mpremote/transport_serial.py

Lines changed: 27 additions & 13 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 = []
@@ -747,7 +752,7 @@ def __mount():
747752

748753
# Apply basic compression on hook code.
749754
for key, value in fs_hook_cmds.items():
750-
fs_hook_code = re.sub(key, str(value), fs_hook_code)
755+
fs_hook_code = re.sub(rf'(\W){key}(\W)', rf"\g<1>{value}\g<2>", fs_hook_code)
751756
fs_hook_code = re.sub(" *#.*$", "", fs_hook_code, flags=re.MULTILINE)
752757
fs_hook_code = re.sub("\n\n+", "\n", fs_hook_code)
753758
fs_hook_code = re.sub(" ", " ", fs_hook_code)
@@ -887,6 +892,14 @@ def do_read(self):
887892
self.wr_bytes(buf)
888893
# self.log_cmd(f"read {fd} {n} -> {len(buf)}")
889894

895+
def do_readline(self):
896+
fd = self.rd_s8()
897+
buf = self.data_files[fd][0].readline()
898+
if self.data_files[fd][1]:
899+
buf = bytes(buf, "utf8")
900+
self.wr_bytes(buf)
901+
# self.log_cmd(f"readline {fd} -> {len(buf)}")
902+
890903
def do_seek(self):
891904
fd = self.rd_s8()
892905
n = self.rd_s32()
@@ -960,6 +973,7 @@ def do_rmdir(self):
960973
fs_hook_cmds["CMD_OPEN"]: do_open,
961974
fs_hook_cmds["CMD_CLOSE"]: do_close,
962975
fs_hook_cmds["CMD_READ"]: do_read,
976+
fs_hook_cmds["CMD_READLINE"]: do_readline,
963977
fs_hook_cmds["CMD_WRITE"]: do_write,
964978
fs_hook_cmds["CMD_SEEK"]: do_seek,
965979
fs_hook_cmds["CMD_REMOVE"]: do_remove,

0 commit comments

Comments
 (0)