Skip to content

Commit

Permalink
Merge pull request #3258 from SimonPersson/performance-2388
Browse files Browse the repository at this point in the history
Improvements to query performance
  • Loading branch information
sampsyo committed May 9, 2019
2 parents 8bef21a + 7df4e23 commit 38bbb7d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 8 deletions.
4 changes: 2 additions & 2 deletions beets/dbcore/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
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
Expand Down Expand Up @@ -597,7 +597,7 @@ def evaluate_template(self, template, for_path=False):
"""
# 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())

Expand Down
6 changes: 3 additions & 3 deletions beets/library.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
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
Expand Down Expand Up @@ -855,7 +855,7 @@ def destination(self, fragment=False, basedir=None, platform=None,
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)
Expand Down Expand Up @@ -1134,7 +1134,7 @@ def art_destination(self, image, item_dir=None):
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']:
Expand Down
4 changes: 2 additions & 2 deletions beets/ui/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
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
Expand Down Expand Up @@ -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


Expand Down
15 changes: 14 additions & 1 deletion beets/util/functemplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import types
import sys
import six
import functools

SYMBOL_DELIM = u'$'
FUNC_DELIM = u'%'
Expand Down Expand Up @@ -553,8 +554,20 @@ def _parse(template):
return Expression(parts)


# External interface.
# Decorator that enables lru_cache on py3, and no caching on py2.
def cached(func):
if six.PY2:
# Sorry python2 users, no caching for you :(
return func
return functools.lru_cache(maxsize=128)(func)


@cached
def template(fmt):
return Template(fmt)


# External interface.
class Template(object):
"""A string template, including text, Symbols, and Calls.
"""
Expand Down
3 changes: 3 additions & 0 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down

0 comments on commit 38bbb7d

Please sign in to comment.