From 55bf82880cfb5d295e8b2ad378b042ab94834033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Voet=20=28ryv=29?= Date: Fri, 4 Dec 2020 12:49:48 +0000 Subject: [PATCH] [FIX] stock_account: fix performance of `_get_in_move_lines` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A `stock.move` can contain thousands of `move_line_ids` (when tracked by SN). The union of recordset (`|=`) doesn't scale well due to the N² complexity (for 10000 SML to validate, ). Replace recordset union by `OrderedSet` of ids + browse (linear complexity). Before for 10000 SML to validate: - the `_get_in_move_lines` take 14% time in the pyflame After in the same condition: - the `_get_in_move_lines` isn't present anymore in the pyflame opw-2347525 X-original-commit: 0acbf6f961e4e946c9e487343ad2cc0129400b7f --- addons/stock_account/models/stock_move.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/addons/stock_account/models/stock_move.py b/addons/stock_account/models/stock_move.py index 2c90c4af584..46cfc895e4a 100644 --- a/addons/stock_account/models/stock_move.py +++ b/addons/stock_account/models/stock_move.py @@ -5,7 +5,7 @@ from collections import defaultdict from odoo import api, fields, models, _ from odoo.exceptions import UserError -from odoo.tools import float_is_zero +from odoo.tools import float_is_zero, OrderedSet import logging _logger = logging.getLogger(__name__) @@ -54,13 +54,13 @@ class StockMove(models.Model): :rtype: recordset """ self.ensure_one() - res = self.env['stock.move.line'] + res = OrderedSet() for move_line in self.move_line_ids: if move_line.owner_id and move_line.owner_id != move_line.company_id.partner_id: continue if not move_line.location_id._should_be_valued() and move_line.location_dest_id._should_be_valued(): - res |= move_line - return res + res.add(move_line.id) + return self.env['stock.move.line'].browse(res) def _is_in(self): """Check if the move should be considered as entering the company so that the cost method