From 46178afee0038310c4b496b83e94bfeea2f53018 Mon Sep 17 00:00:00 2001 From: Xavier Morel Date: Tue, 29 Aug 2023 08:52:15 +0200 Subject: [PATCH] [IMP] core: precompute (company_id parent_of $x) There are cases where the result of `check_company_domain_parent_of` is used in in a loop, leading to the parent_of relation being recomputed once or even multiple times per iteration during SQL evaluation. Computing the parent relationship turns out to be fairly expensive in worst case scenarios (e.g. lots of companies), so while precomputing doesn't save much for a 1:1 situation (though it does make the job of the expressions compiler a bit simpler), the ability to compute it just once instead of say 140 times does make a huge difference in e.g. some report renderings. Nota: apparently `_check_company_domain` can be called with a string because lol, so there's a special case for that. closes odoo/odoo#133530 X-original-commit: 2f9ae135c9dd7cb09fa83fda7a9b304993cb3edc Related: odoo/enterprise#46518 Signed-off-by: Raphael Collet Signed-off-by: Xavier Morel (xmo) --- odoo/models.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/odoo/models.py b/odoo/models.py index 058906fc533..e5d8d3208a7 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -173,7 +173,18 @@ def to_company_ids(companies): def check_company_domain_parent_of(self, companies): - return ['|', ('company_id', '=', False), ('company_id', 'parent_of', to_company_ids(companies))] + if isinstance(companies, str): + return ['|', ('company_id', '=', False), ('company_id', 'parent_of', [companies])] + + companies = [id for id in to_company_ids(companies) if id] + if not companies: + return [('company_id', '=', False)] + + return ['|', ('company_id', '=', False), ('company_id', 'in', [ + int(parent) + for rec in self.env['res.company'].sudo().browse(companies) + for parent in rec.parent_path.split('/')[:-1] + ])] class MetaModel(api.Meta):