odoo icon indicating copy to clipboard operation
odoo copied to clipboard

[FW][PERF] product: speed up _create_variant_ids with lots of exclusions

Open fw-bot opened this issue 1 year ago • 2 comments

When there are a relatively high number of exclusions + a lot of combinations for a given product_template, create_variant_ids can become slow. The main bottlneck is the call to _is_combination_possible_by_config. More specifically, calling attribute_lines.product_template_value_ids and self._get_own_attribute_exclusions for each combination is pretty slow.

To speed that up, this commit introduces a new method, _filter_combinations_impossible_by_config. This method accepts a sequence of combinations and yield those that are valid w.r.t. the exclusions. Because values that only depends on self are computed once before looping through the combinations, this lead to a significant speedup. This method is a generator to avoid MemoryErrors, be able to raise a UserError after "creating" 1000 variants and be consistent with choices made in _create_variant_ids. https://github.com/odoo/odoo/blob/0042b9d3eece219d4e89fe9c6ecb9971a1f3bf12/addons/product/models/product_template.py#L710

speedup

Customer saas-16.4 database with 378 templates, 3832 products, 44 product.attributes, 186 product.attribute.values, 79 product.template.attribute.lines, 484 product.template.attribute.values, 374 product.template.attribute.exclusions. No dynamic attributes.

Timing to write on product.template, adding a new attribute value on a ProductTemplate Form increasing the number of combinations.

Combinations Before PR After PR
4 219ms 218ms
55 785ms 440ms
216 1.36s 592ms
1 944 28s 809ms
33 048 5min30s 3.78s
231 336 +15min (timeout) 12s

I confirm I have signed the CLA and read the PR guidelines at www.odoo.com/submit-pr

Forward-Port-Of: odoo/odoo#164191 Forward-Port-Of: odoo/odoo#162239

fw-bot avatar May 16 '24 13:05 fw-bot

Pull request status dashboard.

robodoo avatar May 16 '24 13:05 robodoo

This PR targets saas-17.1 and is part of the forward-port chain. Further PRs will be created up to master.

More info at https://github.com/odoo/odoo/wiki/Mergebot#forward-port

fw-bot avatar May 16 '24 13:05 fw-bot

@AurelienVD child PR odoo/odoo#165819 was modified / updated and has become a normal PR. This PR (and any of its parents) will need to be merged independently as approvals won't cross.

fw-bot avatar May 22 '24 09:05 fw-bot

@fw-bot r+

Aurelienvd avatar May 22 '24 09:05 Aurelienvd