From 60160c38a03e61355865ed9c2e221b8bf9580ba5 Mon Sep 17 00:00:00 2001 From: Pierre Rust Date: Wed, 7 May 2014 00:03:05 +0200 Subject: [PATCH] Shortcut some tests in Model initialization When Model objects (Albums and Items) are loaded from the database a lot of time is wasted checking if the object is dirty and if fields are fixed or flexattr. This change alow bypassing these checks when we know that the data is correct ; it should only be used when loading Model from the database. With this, listing all tracks in my test database (8200 items) takes 4 seconds while it took 31 seconds previously. --- beets/dbcore/db.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/beets/dbcore/db.py b/beets/dbcore/db.py index 0c03f39ea..ba89ab233 100644 --- a/beets/dbcore/db.py +++ b/beets/dbcore/db.py @@ -95,7 +95,7 @@ class Model(object): # Basic operation. - def __init__(self, db=None, **values): + def __init__(self, db=None, fixed=None, flexattr=None, **values): """Create a new object with an optional Database association and initial field values. """ @@ -105,6 +105,19 @@ class Model(object): self._values_flex = {} # Initial contents. + # For fields that are explicitly given as fixed or flex, we skip + # the checks made by update() to speed things up when loading objects + # from the database + if fixed: + for (key, value) in fixed.iteritems(): + # read path buffers. + if key == 'path': + value = str(value) + self._values_fixed[key] = self._fields[key].normalize(value) + if flexattr: + for (key, value) in flexattr: + self._values_flex[key] = value + self.update(values) self.clear_dirty() @@ -492,13 +505,11 @@ class Results(object): (row['id'],) ) values = dict(row) - values.update( - dict((row['key'], row['value']) for row in flex_rows) - ) + flex_values = dict((row['key'], row['value']) for row in flex_rows) # Construct the Python object and yield it if it passes the # predicate. - obj = self.model_class(self.db, **values) + obj = self.model_class(self.db, values, flex_values) if not self.query or self.query.match(obj): yield obj