New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Diff improvements (performance + included_fields
)
#776
Conversation
Codecov Report
@@ Coverage Diff @@
## master #776 +/- ##
==========================================
+ Coverage 97.69% 97.79% +0.10%
==========================================
Files 19 19
Lines 999 999
Branches 151 151
==========================================
+ Hits 976 977 +1
Misses 10 10
+ Partials 13 12 -1
Continue to review full report at Codecov.
|
Turns out the current code also causes additional queries for excluded fields, which isn't good. Would be great to get this merged and released soon, as the performance improvements are vast! |
If you could rebase on master and make sure all the new branches are covered by tests, will re-review. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would leave the KeyError handling in place for invalid fields and add this test to the HistoricalRecordsTest:
+ with self.assertRaises(KeyError):
+ delta = new_record.diff_against(old_record, included_fields=["this_field_does_not_exist"])
Also rebase on current master, then if we get a clean run we'll be all set.
This stops unnecesary queries from `model_to_dict` for M2M values
Queries are always run when using a base model for some reason.
This not only means comparison is done on primitive values, but as a side effect removes the extra query for base models
Description
Reduce the number of queries needed when calling
diff_against
(down to 0).Also implement
included_fields
, so only certain fields will be compared. In this new implementation of diffing, old fields won't be compared at all, and can be deferred from querying if needed.Related Issue
N/A for performance improvement.
included_fields
was mentioned in #576 (comment), and as I was here, I added. Can extract into a separate PR if needed.Motivation and Context
model_to_dict
will do queries for M2M fields against real tables. As the history tables don't use actual relationships, it's safe to use those for the lookups without additional queries. Removing this removes accidental queries on models with M2M fields, and stops loading them into memory unnecessarily.Additionally, diffing involved getting real instances of the model using
.instance
, which was unnecessary. The diffing can be using the history instances directly, saving a lot of computation. The fields list is pulled off the real model which meanshistory_*
fields are still excluded correctly..instance
will sometimes do queries, hence removing the need for this can additionally improve performance.There are also additional tests to check that diffing is pure and doesn't touch the DB, to help prevent regressions.
How Has This Been Tested?
Tests have been added to cover changes
Has been run against live project unittests successfully
Screenshots (if appropriate):
Types of changes
Checklist:
make format
command to format my codeAUTHORS.rst
CHANGES.rst