Skip to content

Commit

Permalink
use bytearray instead of bytes in the playlist file add op to avoid u…
Browse files Browse the repository at this point in the history
…nnecessary object creation
  • Loading branch information
azuline committed Oct 27, 2023
1 parent 1c74fef commit e143753
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions rose/virtualfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ def __init__(self, config: Config):
# add it to the playlist.
#
# The state is a mapping of (path, fh) -> (playlist_name, ext, bytes).
self.playlist_additions_in_progress: dict[tuple[str, int], tuple[str, str, bytes]] = {}
self.playlist_additions_in_progress: dict[tuple[str, int], tuple[str, str, bytearray]] = {}
self.fhgen = FileDescriptorGenerator()
super().__init__()

Expand Down Expand Up @@ -428,7 +428,7 @@ def open(self, path: str, flags: int) -> int:
self.playlist_additions_in_progress[(path, fh)] = (
p.playlist,
Path(p.file).suffix,
b"",
bytearray(),
)
return fh
# Otherwise, continue on...
Expand Down Expand Up @@ -459,8 +459,9 @@ def write(self, path: str, data: bytes, offset: int, fh: int) -> int:

if pap := self.playlist_additions_in_progress.get((path, fh), None):
logger.debug("Matched write to an in-progress playlist addition.")
playlist, ext, b = pap
self.playlist_additions_in_progress[(path, fh)] = (playlist, ext, b[:offset] + data)
_, _, b = pap
del b[offset:]
b.extend(data)
return len(data)

os.lseek(fh, offset, os.SEEK_SET)
Expand All @@ -472,8 +473,8 @@ def truncate(self, path: str, length: int, fh: int | None = None) -> None:
if fh:
if pap := self.playlist_additions_in_progress.get((path, fh), None):
logger.debug("Matched truncate to an in-progress playlist addition.")
playlist, ext, b = pap
self.playlist_additions_in_progress[(path, fh)] = (playlist, ext, b[:length])
_, _, b = pap
del b[length:]
return
return os.ftruncate(fh, length)

Expand Down

0 comments on commit e143753

Please sign in to comment.