Skip to content

Commit 9b8f43a

Browse files
avoid unnecessary writes when modifying non-media fields
This patch prevents Item.write() from performing a mediafile.save() when no writable media tag fields have changed. Without this change, commands such as 'beet modify -a onplayer=true' trigger unnecessary file writes, updating mtimes and causing significant slowdowns. Fix verified with a tiny mp3 test case. Signed-off-by: Qianwei Wang <qweiw@umich.edu> Signed-off-by: weiqianwang123 <1416740298@qq.com>
1 parent 2bd77b9 commit 9b8f43a

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

beets/library/models.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -995,6 +995,12 @@ def try_write(self, *args, **kwargs):
995995
log.error("{}", exc)
996996
return False
997997

998+
def _media_tags_changed(self, original, modified):
999+
for f in self._media_tag_fields: # this is defined in the class already
1000+
if getattr(original, f) != getattr(modified, f):
1001+
return True
1002+
return False
1003+
9981004
def try_sync(self, write, move, with_album=True):
9991005
"""Synchronize the item with the database and, possibly, update its
10001006
tags on disk and its path (by moving the file).
@@ -1007,8 +1013,12 @@ def try_sync(self, write, move, with_album=True):
10071013
Similar to calling :meth:`write`, :meth:`move`, and :meth:`store`
10081014
(conditionally).
10091015
"""
1010-
if write:
1016+
original = Item.from_path(self.path)
1017+
1018+
# only write tags if media tags changed
1019+
if write and self._media_tags_changed(original, self):
10111020
self.try_write()
1021+
10121022
if move:
10131023
# Check whether this file is inside the library directory.
10141024
if self._db and self._db.directory in util.ancestry(self.path):

0 commit comments

Comments
 (0)