PerformanceHighlight of Weekly #004

Faster access checks on many messages

_get_forbidden_access no longer rebuilds the list of allowed ids on every iteration when processing tens of thousands of messages.

Versions18.019.0
ModulesDiscuss mail
AppProductivity

Pull requests and commits

BranchPull requestCommit
18.0odoo/odoo#294595 ↗36ae520fb0 ↗
19.0odoo/odoo#294926 ↗82fb248d41 ↗

Commit message

[PERF] mail: Reduce .ids calls in _get_forbidden_access

When processing a few tens of thousands of mail.message records through _get_forbidden_access, a list of allowed record ids will repeatably be rebuilt unecessarily.

As the allowed ids are a subset of the record ids that are being iterated over, this change saves time by avoiding the unecessary rebuilding of the allowed IDs list + processing of already filtered out document IDs.

Steps to Reproduce on Runbot:
1. Create large amount of records using the following code:

```py
for i in range(0, BIG_NUMBER):
    account_payment_rec = env['account.payment'].sudo().create({ #the model here shouldn't matter
        'amount': 13.3,
        'payment_type': 'inbound',
        'partner_type': 'customer',
        'partner_id': 9,
            })
    message_rec = env["mail.message"].sudo().with_user(1).create({
                'model': 'account.payment',
                'res_id': account_payment_rec.id,
                'body': 'test',
                'date': '2001-01-01',
                'author_id': 1,
                'message_type': 'comment',
                'subtype_id': env.ref('mail.mt_note').id,
                'create_uid': 1
            })

    env["ir.attachment"].sudo().create({
                "name": "test",
                "datas": b'',
                "res_model": 'mail.message',
                "res_id": message_rec.id,
                "type": "binary",
                "mimetype": "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
                "public": False
            })
```

2. Add a many2many field to form view of a model
3. Access a record of that model, experience slowness

Here is a performance benchmark when accessing the `ir.attachment` studio field (record.x_studio_...)

Number Of Records: | 10 | 100 | 1000 | 10000 | 50000 | 100000
-- | -- | -- | -- | -- | -- | --
Time Before (s) | 0.003801873 | 0.004506955 | 0.024505367 | 1.148872476 | 27.53143206 | 5m 11s
Time After (s) | 0.003434098 | 0.004359007 | 0.015066793 | 0.13064502 | 0.634392667 | 1.260203857

opw-6629401

My stack

Pick the versions and apps you run. Weekly digests, this week's table and All changes then show only what concerns you. Saved in this browser.

Versions
Apps
Modules