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.
This commit is contained in:
Pierre Rust 2014-05-07 00:03:05 +02:00
parent 6b3bea99a0
commit 60160c38a0

View file

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