-
-
Notifications
You must be signed in to change notification settings - Fork 31.1k
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
Made Field.error_messages a cached property. #15415
Conversation
This speeds up field creation and reduces memory usage.
So generally speaking (AFAIK), introducing the In this case, it's kind of all of them; they can be less eager because they're not required for further work within Here, for example, is initing a couple of fields on baseline/main as of 25514b6 on my machine:
and after the patch (manually applied):
So creation does get quicker. Good for both fields defined forever on models ahead-of-time, and for those generated by When we access the
and then after patch:
The cost is init + error_messages, which seems the best way to account for the fact that you'd otherwise be getting an outlier on the first access and the rest would be invalidated by having been cached in the dict. You can see there's some additional cost to gating it behind the cached property, but that should be amortized over the lifetime of the Field definition (the cost falls to I can't currently think of a reason this wouldn't just be a net-positive based on the numbers I'm collecting which agree with Colin's data, unless it transpired that I'd suggest that if this does land, it may be worth revisiting the #15410 PR subsequently, with an eye to the fact that Fields are spawned at runtime so there is potentially a memory/speed saving to consider there independently, which I'd entirely forgotten about (... again despite #15277, sigh. Sorry!). It being separate PRs actually makes it easier to evaluated as a whole though, so that's nice. If there's other usages/use-cases that we've not considered in this discussion to date, someone remind us so we can try and account for those too ;) |
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.
Tentatively agreeing at this stage, pending anyone thinking of issues/use-cases we've not thought of, or data-points indicating it would be a generally negative for existing, relatively common use X/Y/Z.
@collinanderson Thanks 👍 |
Thanks! |
This speeds up field creation and reduces memory usage.
Refs #15410
Tested speed using this code:
baseline:
min: 0.0077, median: 0.0082
with patch:
min: 0.0057, median: 0.0063
So about 23% faster creation. Some fields don't use model validation, like
annotate(output_field=DecimalField())
.