odoo/odoo#162442

Created by Upgrade, Carsten Wolff (cawo)
Closed
label
odoo-dev:17.0-fix_model_flush_mem_use-cawo
head
394569743ae0f8ce3a6b5b5d68d74abbbb9690de
target
17.0

[FIX] core: reduce memory use of BaseModel._flush()

Motivation: MemoryError exceptions when a large number of records on the same model have dirty fields. Such often happens during upgrades.

In the current implementation, the cached data is re-arranged in multiple steps using local data structures. The most problematic is `id_vals[record.id][field.name]`, because it creates a dictionary with a potentially long field name (think studio fields) as key for each dirty record. For thousands of records, this quickly accumulates to 10s of MiB in RAM.

The idea of this patch is:
1. collect all dirty ids for all dirty fields on the model. This does not cost additional memory, since the list of ids per field will be pop()'ed from the cache.
2. Walk over fields and ids collecting all fields and values of each id in the same loop, directly building the `updates` dictionary, without creating the intermediate data structure.