From 447cc82e041739bc1210c0d07af5ea64e44c2e99 Mon Sep 17 00:00:00 2001 From: Peter Dolan Date: Mon, 14 Apr 2025 11:22:41 -0700 Subject: [PATCH] Do not write unchanged items to the library in FtInTitle (#5718) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit FtInTitle performs a library store operation for every item it processes, whether or not the item has changed. By limiting the `item.store()` call to only those cases when the item has changed, the plugin’s performance when processing an entire library improves by two to three orders of magnitude. --- beetsplug/ftintitle.py | 19 ++++++++++++------- docs/changelog.rst | 2 ++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/beetsplug/ftintitle.py b/beetsplug/ftintitle.py index e4f51fd10..3f01af593 100644 --- a/beetsplug/ftintitle.py +++ b/beetsplug/ftintitle.py @@ -111,10 +111,10 @@ class FtInTitlePlugin(plugins.BeetsPlugin): write = ui.should_write() for item in lib.items(ui.decargs(args)): - self.ft_in_title(item, drop_feat, keep_in_artist_field) - item.store() - if write: - item.try_write() + if self.ft_in_title(item, drop_feat, keep_in_artist_field): + item.store() + if write: + item.try_write() self._command.func = func return [self._command] @@ -125,8 +125,8 @@ class FtInTitlePlugin(plugins.BeetsPlugin): keep_in_artist_field = self.config["keep_in_artist"].get(bool) for item in task.imported_items(): - self.ft_in_title(item, drop_feat, keep_in_artist_field) - item.store() + if self.ft_in_title(item, drop_feat, keep_in_artist_field): + item.store() def update_metadata(self, item, feat_part, drop_feat, keep_in_artist_field): """Choose how to add new artists to the title and set the new @@ -156,9 +156,12 @@ class FtInTitlePlugin(plugins.BeetsPlugin): self._log.info("title: {0} -> {1}", item.title, new_title) item.title = new_title - def ft_in_title(self, item, drop_feat, keep_in_artist_field): + def ft_in_title(self, item, drop_feat, keep_in_artist_field) -> bool: """Look for featured artists in the item's artist fields and move them to the title. + + Returns: + True if the item has been modified. False otherwise. """ artist = item.artist.strip() albumartist = item.albumartist.strip() @@ -180,5 +183,7 @@ class FtInTitlePlugin(plugins.BeetsPlugin): self.update_metadata( item, feat_part, drop_feat, keep_in_artist_field ) + return True else: self._log.info("no featuring artists found") + return False diff --git a/docs/changelog.rst b/docs/changelog.rst index 32bd91390..ca2bb50e3 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -100,6 +100,8 @@ Other changes: improve useability for new developers. * :doc:`/plugins/smartplaylist`: URL-encode additional item `fields` within generated EXTM3U playlists instead of JSON-encoding them. +* :doc:`plugins/ftintitle`: Optimize the plugin by avoiding unnecessary writes + to the database. 2.2.0 (December 02, 2024) -------------------------