sampsyo/beets — pull request #751 by geigerzaehler (2014-05-06 09:31:42+00:00)
Title: Make _formatted_mapping lazy and an object

The motivation for this is to increase the performance of template evalutation (see #724). It is an alternative solution to #745. All credits for pinning down this bottleneck go to @PierreRust.

Previously, the `model._formatted_mapping()` method returned a dictionary that served as the environment (mapping of variable names to values) when evaluating a template. To populate the dictionary, we iterated over all keys in the model, formatted the values, and assigned it to the dictionary. This meant we formatted every field, even if the template did not require it.

With this commit `_formatted_mapping()` does not return a populated dictionary but a proxy (or view) instance. The object only knows about the model and the keys it provides the formatted view for. If a variable is requested by the template it computes the formatted value on the fly.

For me (~650 albums) the performance gain with `./beet list -af '$album: $tracktotal - $year'` is in the order of a magnitude.

The class-based approach has one additional advantage: In the future, we can separate the formatting logic from the database logic.