Skip to content

Commit

Permalink
Cache track_length_grace and track_length_max access
Browse files Browse the repository at this point in the history
  • Loading branch information
snejus committed Dec 27, 2024
1 parent 420117b commit 4c8d75f
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions beets/autotag/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand All @@ -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)
Expand Down

0 comments on commit 4c8d75f

Please sign in to comment.