Browse Source
With this patch we save about 83% of the execution time when generating invoices in batch. # Optimizations made ## Recompute once at the end of the batch This part avoids recomputing many fields per record. Instead, global recomputations are triggered at the end of the block: ```python with _self.env.norecompute(): ... invoices.compute_taxes() _self.recompute() ``` Notice the explicit call to `compute_taxes()`, which was explicit before also, but it was done once per invoice, losing batch-computing boost. ## Disabling prefetch for extra fields It's done in this part: ```python _self = self.with_context(prefetch_fields=False) ``` Prefetching makes sense when we are going to use a lot of fields for a model that has only a few. In our case, we are using not much fields, but the models involved have lots of them. This produces more queries to get those fields, but the queries are noticeably smaller. At the end of the day, it saves a lot of time, which is what matters. ## Disabling track mail creation This part does it: ```diff ctx.update({ + 'mail_notrack': True, 'next_date': next_date, ``` It makes that when creating invoices, we don't create the "Invoice created" message. ## Precomputing price Obtaining `price_unit` from `contract.recurring_invoice_line_ids` was quite expenisve in terms of CPU, and it was being made once per line, each one in a different context, which means also a different cache. Instead of that, lines now share a single context, and are computed before starting the batch. This code precomputes stuff: ```python # Precompute expensive computed fields in batch recurring_lines = _self.mapped("recurring_invoice_line_ids") recurring_lines._fields["price_unit"].determine_value(recurring_lines) ``` And the usage of 2 different environments done inside `_create_invoice()` (`self` and `_self`) guarantee that the invoices are filled with the correct data, but also that the lines use the cached precomputed value instead of having to compute it each time. # Performance gain According to my tests, generating 10 invoices took 62 seconds before, and it takes about 18 seconds now.pull/266/head
Jairo Llopis
6 years ago
2 changed files with 44 additions and 17 deletions
Loading…
Reference in new issue