From 9b8f43ad9785133f79897d5011fdacc2b13faadc Mon Sep 17 00:00:00 2001 From: weiqianwang123 <1416740298@qq.com> Date: Sun, 7 Dec 2025 21:08:27 -0500 Subject: [PATCH] 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 Signed-off-by: weiqianwang123 <1416740298@qq.com> --- beets/library/models.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/beets/library/models.py b/beets/library/models.py index cbee2a411..dc4ec8c39 100644 --- a/beets/library/models.py +++ b/beets/library/models.py @@ -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):