From 73ef7801f2dc5a0dad2a81a9d401d7b95a906db6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?C=C3=A9dric=20Krier?= Date: Mon, 6 Jun 2011 17:15:17 +0200 Subject: [PATCH] Add threshold on number of fields prefetched issue1889 review34004 --- trytond/const.py | 2 ++ trytond/model/browse.py | 45 ++++++++++++++++++++++++++++++++--------- 2 files changed, 37 insertions(+), 10 deletions(-) diff --git a/trytond/const.py b/trytond/const.py index 3f5ee9783e..2a569f5f6b 100644 --- a/trytond/const.py +++ b/trytond/const.py @@ -17,3 +17,5 @@ OPERATORS = ( '<', '>', ) + +BROWSE_FIELD_TRESHOLD = 100 diff --git a/trytond/model/browse.py b/trytond/model/browse.py index a23ac30eb4..1d389efc4f 100644 --- a/trytond/model/browse.py +++ b/trytond/model/browse.py @@ -2,8 +2,10 @@ #this repository contains the full copyright notices and license terms. from __future__ import with_statement import contextlib +from itertools import islice, ifilter, ifilterfalse from trytond.model import fields from trytond.transaction import Transaction +from trytond.const import BROWSE_FIELD_TRESHOLD class BrowseRecordList(list): ''' @@ -89,6 +91,7 @@ class BrowseRecord(object): 'does not exist in model "%s"!' \ % (name, self._model._name)) + ffields = {} if col.loading == 'eager': field_access_obj = self._model.pool.get('ir.model.field.access') fread_accesses = {} @@ -101,17 +104,39 @@ class BrowseRecord(object): to_remove = set(x for x, y in fread_accesses.iteritems() if not y and x != name) - ffields = dict((fname, field) for fname, (_, _, field) - in self._model._inherit_fields.iteritems() - if field.loading == 'eager' - and fname not in self._model._columns + threshold = BROWSE_FIELD_TRESHOLD + inherit_threshold = threshold - len(self._model._columns) + + def not_cached(item): + fname, field = item + return (fname not in self._data[self._id] + and fname not in self._local_data[self._id]) + def to_load(item): + fname, field = item + return (field.loading == 'eager' and fname not in to_remove) - ffields.update(dict((fname, field) for fname, field - in self._model._columns.iteritems() - if field.loading == 'eager' - and fname not in to_remove)) - else: - ffields = {name: col} + def overrided(item): + fname, field = item + return fname in self._model._columns + + if inherit_threshold > 0: + ifields = ((fname, field) + for fname, (_, _, field) in + self._model._inherit_fields.iteritems()) + ifields = ifilterfalse(overrided, + ifilter(to_load, + ifilter(not_cached, ifields))) + ifields = islice(ifields, 0, inherit_threshold) + ffields.update(ifields) + threshold -= inherit_threshold + + ifields = ifilter(to_load, + ifilter(not_cached, + self._model._columns.iteritems())) + ifields = islice(ifields, 0, threshold) + ffields.update(ifields) + + ffields[name] = col # add datetime_field for field in ffields: