[IMP] core: do not try to translate selection values for nothing

When we are only trying to know accepted raw values, there is no
need to fetch the translations for the description part of the
selection tuples.

This performance problem was first observed on res.config.settings
record creation, where the values are pruned to avoid writes and
invalidation for unmodified related values (cf create override
in base/models/res_config.py & 5a4e7b711e).

After more analysis, the 'problem' also impacts record updates on
any related field (readonly or not).

* Res.config.settings performance gains:

In a database with test_main_flows (& its diverse dependencies),
this reduces by ~50% the number of queries done when triggering
the creation of res.config.settings record through the interface
(aka with all the related updatable fields receiving their current
value as create value).

For the creation of 150 res.config.settings records, with full cache
invalidation between each record creation, we notice a gain of:

* 11853 to 6303 total queries
* 11.2s to 7.5s total creation time

Part-of: odoo/odoo#83104
This commit is contained in:
Victor Feyens
2022-02-08 11:07:46 +00:00
parent 856c1691c7
commit 65baadeb75
2 changed files with 4 additions and 3 deletions
@@ -3164,14 +3164,14 @@ class TestSelectionUpdates(common.TransactionCase):
related_record = self.env[self.MODEL_BASE].create({'my_selection': 'foo'})
with self.assertQueryCount(2): # defaults (readonly related field), INSERT
record = self.env[self.MODEL_RELATED].create({'selection_id': related_record.id})
with self.assertQueryCount(4):
with self.assertQueryCount(0):
record.related_selection = 'bar'
def test_selection_related(self):
related_record = self.env[self.MODEL_BASE].create({'my_selection': 'foo'})
with self.assertQueryCount(2): # defaults (related field), INSERT
record = self.env[self.MODEL_RELATED_UPDATE].create({'selection_id': related_record.id})
with self.assertQueryCount(7):
with self.assertQueryCount(3):
record.related_selection = 'bar'
+2 -1
View File
@@ -2589,7 +2589,8 @@ class Selection(Field):
if isinstance(selection, str):
selection = getattr(env[self.model_name], selection)()
elif callable(selection):
selection = selection(env[self.model_name])
model = env[self.model_name].with_context(lang=None)
selection = selection(model)
return [value for value, _ in selection]
def convert_to_column(self, value, record, values=None, validate=True):