[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 <rco@odoo.com>
Signed-off-by: Xavier Morel (xmo) <xmo@odoo.com>
This commit is contained in:
Xavier Morel
2023-09-04 08:03:36 +00:00
parent 08518bbad3
commit 46178afee0
+12 -1
View File
@@ -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):