mirror of
https://github.com/beetbox/beets.git
synced 2025-12-27 19:12:40 +01:00
Cache track_length_grace and track_length_max access
This commit is contained in:
parent
420117b598
commit
4c8d75ff38
1 changed files with 17 additions and 4 deletions
|
|
@ -22,6 +22,7 @@ import datetime
|
|||
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)
|
||||
|
|
|
|||
Loading…
Reference in a new issue