Create a cached template() function

We were previously doing calls to Template() directly, sometimes in a
loop. This caused the same template to be recompiled over and over. This
commit introduces a function template() which caches the results, so
that multiple calls with the same template string does not require
recompilation.
This commit is contained in:
Simon Persson 2019-05-09 18:24:59 +02:00
parent 8bef21a4d5
commit c5075b2855
5 changed files with 14 additions and 7 deletions

View file

@ -25,7 +25,7 @@ import sqlite3
import contextlib
import beets
from beets.util.functemplate import Template
from beets.util import functemplate
from beets.util import py3_path
from beets.dbcore import types
from .query import MatchQuery, NullSort, TrueQuery
@ -597,7 +597,7 @@ class Model(object):
"""
# Perform substitution.
if isinstance(template, six.string_types):
template = Template(template)
template = functemplate.template(template)
return template.substitute(self.formatted(for_path),
self._template_funcs())

View file

@ -31,7 +31,7 @@ from beets import plugins
from beets import util
from beets.util import bytestring_path, syspath, normpath, samefile, \
MoveOperation
from beets.util.functemplate import Template
from beets.util.functemplate import template, Template
from beets import dbcore
from beets.dbcore import types
import beets
@ -855,7 +855,7 @@ class Item(LibModel):
if isinstance(path_format, Template):
subpath_tmpl = path_format
else:
subpath_tmpl = Template(path_format)
subpath_tmpl = template(path_format)
# Evaluate the selected template.
subpath = self.evaluate_template(subpath_tmpl, True)
@ -1134,7 +1134,7 @@ class Album(LibModel):
image = bytestring_path(image)
item_dir = item_dir or self.item_dir()
filename_tmpl = Template(
filename_tmpl = template(
beets.config['art_filename'].as_str())
subpath = self.evaluate_template(filename_tmpl, True)
if beets.config['asciify_paths']:

View file

@ -36,7 +36,7 @@ from beets import logging
from beets import library
from beets import plugins
from beets import util
from beets.util.functemplate import Template
from beets.util.functemplate import template
from beets import config
from beets.util import confit, as_string
from beets.autotag import mb
@ -616,7 +616,7 @@ def get_path_formats(subview=None):
subview = subview or config['paths']
for query, view in subview.items():
query = PF_KEY_QUERIES.get(query, query) # Expand common queries.
path_formats.append((query, Template(view.as_str())))
path_formats.append((query, template(view.as_str())))
return path_formats

View file

@ -35,6 +35,7 @@ import dis
import types
import sys
import six
import functools
SYMBOL_DELIM = u'$'
FUNC_DELIM = u'%'
@ -552,6 +553,9 @@ def _parse(template):
parts.append(remainder)
return Expression(parts)
@functools.lru_cache(maxsize=128)
def template(fmt):
return Template(fmt)
# External interface.

View file

@ -117,6 +117,9 @@ Some improvements have been focused on improving beets' performance:
to be displayed.
Thanks to :user:`pprkut`.
:bug:`3089`
* Querying the library was further improved by reusing compiled teamplates
instead of compiling them over and over again.
Thanks to :user:`SimonPersson`.
* :doc:`/plugins/absubmit`, :doc:`/plugins/badfiles`: Analysis now works in
parallel (on Python 3 only).
Thanks to :user:`bemeurer`.