Cache track_length_grace and track_length_max access

This commit is contained in:
Šarūnas Nejus 2024-12-27 16:22:24 +00:00
parent 420117b598
commit 4c8d75ff38
No known key found for this signature in database
GPG key ID: DD28F6704DBE3435

View file

@ -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)