From 4c8d75ff38aa8debd98aa566489ea4ee77133fcf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0ar=C5=ABnas=20Nejus?= Date: Fri, 27 Dec 2024 16:22:24 +0000 Subject: [PATCH] Cache track_length_grace and track_length_max access --- beets/autotag/match.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/beets/autotag/match.py b/beets/autotag/match.py index 8ac7bb3d58..db4a35b132 100644 --- a/beets/autotag/match.py +++ b/beets/autotag/match.py @@ -22,6 +22,7 @@ import re from collections.abc import Iterable, Sequence from enum import IntEnum +from functools import cache from typing import TYPE_CHECKING, Any, NamedTuple, TypeVar, cast import lap @@ -149,8 +150,16 @@ def track_index_changed(item: Item, track_info: TrackInfo) -> bool: return item.track not in (track_info.medium_index, track_info.index) -track_length_grace = config["match"]["track_length_grace"].as_number() -track_length_max = config["match"]["track_length_max"].as_number() +@cache +def get_track_length_grace() -> float: + """Get cached grace period for track length matching.""" + return config["match"]["track_length_grace"].as_number() + + +@cache +def get_track_length_max() -> float: + """Get cached maximum track length for track length matching.""" + return config["match"]["track_length_max"].as_number() def track_distance( @@ -161,13 +170,17 @@ def track_distance( """Determines the significance of a track metadata change. Returns a Distance object. `incl_artist` indicates that a distance component should be included for the track artist (i.e., for various-artist releases). + + ``track_length_grace`` and ``track_length_max`` configuration options are + cached because this function is called many times during the matching + process and their access comes with a performance overhead. """ dist = hooks.Distance() # Length. if info_length := track_info.length: - diff = abs(item.length - info_length) - track_length_grace - dist.add_ratio("track_length", diff, track_length_max) + diff = abs(item.length - info_length) - get_track_length_grace() + dist.add_ratio("track_length", diff, get_track_length_max()) # Title. dist.add_string("track_title", item.title, track_info.title)