Hello everyone, 

why this line is inside the loop?

https://github.com/odoo/odoo/blob/132c7e75e57321c77aac9250a2587c74c5da7857/addons/account/models/account_move.py#L1364

We were removing 100k~ account.move.line records for testing purposes. It took ages. So we do some little investigation and saw this, we took out from the loop the process duration became minutes from hours.

Regards from Projet
addons/account/models/account_move.py
return res
    @api.multi
    def unlink(self):
        """ When removing a link between entries, we need to revert the eventual journal entries we created to book the
            fluctuation of the foreign currency's exchange rate.
            We need also to reconcile together the origin currency difference line and its reversal in order to completly
            cancel the currency difference entry on the partner account (otherwise it will still appear on the aged balance
            for example).
        """
        # we 
        to_unlink = self
        exchange_rate_entries = self.env['account.move'].search([('rate_diff_partial_rec_id', 'in', self.ids)])
        for rec in self:
            partial_rec_set = OrderedDict.fromkeys([rec])
            aml_set = self.env['account.move.line']
            total_debit = 0
            total_credit = 0
            total_amount_currency = 0
            currency = None
            for partial_rec in partial_rec_set:
                if currency is None:
                    currency = partial_rec.currency_id
                for aml in [partial_rec.debit_move_id, partial_rec.credit_move_id]:
                    if aml not in aml_set:
                        total_debit += aml.debit
                        total_credit += aml.credit
                        aml_set |= aml
                        if aml.currency_id and aml.currency_id == currency:
                            total_amount_currency += aml.amount_currency
                    for x in aml.matched_debit_ids | aml.matched_credit_ids: