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>
This commit is contained in:
weiqianwang123 2025-12-07 21:08:27 -05:00
parent 2bd77b9895
commit 9b8f43ad97

View file

@ -995,6 +995,12 @@ class Item(LibModel):
log.error("{}", exc)
return False
def _media_tags_changed(self, original, modified):
for f in self._media_tag_fields: # this is defined in the class already
if getattr(original, f) != getattr(modified, f):
return True
return False
def try_sync(self, write, move, with_album=True):
"""Synchronize the item with the database and, possibly, update its
tags on disk and its path (by moving the file).
@ -1007,8 +1013,12 @@ class Item(LibModel):
Similar to calling :meth:`write`, :meth:`move`, and :meth:`store`
(conditionally).
"""
if write:
original = Item.from_path(self.path)
# only write tags if media tags changed
if write and self._media_tags_changed(original, self):
self.try_write()
if move:
# Check whether this file is inside the library directory.
if self._db and self._db.directory in util.ancestry(self.path):