Skip to content
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

Add caching of OptInFieldsMixin fields property #1093

Merged
merged 2 commits into from
Dec 1, 2021

Conversation

glennmatthews
Copy link
Contributor

Fixes: #N/A

Profiling of API requests with Silk showed that one potential bottleneck was repeated calls to serializer.fields, which is an instance attribute in baseline django-rest-framework but is overridden into a property in our OptInFieldsMixin class. It appears that this property was accessed about 9 times per model instance being retrieved. As a result, adding some basic caching here pays performance dividends - in my local testing, using the development server running in Docker, an API query to retrieve 1000 Devices (minus config-context) is reduced from about 40 seconds to about 30 seconds.

The diffs are a bit difficult to read but basically all this is, is moving most of the body of the property function into an if self.__pruned_fields is None: block.

@nniehoff
Copy link
Contributor

FYI in my testing with 5k devices, this improves response time ~2x

Copy link
Contributor

@jathanism jathanism left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My comment is minor. Just thinking perhaps a chance to cleanup something that doesn't appear to need to be there, but otherwise this is good to go!

nautobot/core/api/serializers.py Show resolved Hide resolved
@itdependsnetworks
Copy link
Contributor

Does it make sense to patch to the 1.1 version?

@dgarros
Copy link
Contributor

dgarros commented Nov 29, 2021

Does it make sense to patch to the 1.1 version?

Yes please, I came here to ask the same question.

@jvanderaa
Copy link
Contributor

I will add a +1 on the patch of 1.1.x

@glennmatthews glennmatthews merged commit 4da4e34 into develop Dec 1, 2021
@glennmatthews
Copy link
Contributor Author

glennmatthews commented Dec 1, 2021

I'm working on preparing a 1.1.6 release to include this fix and #1108.

@glennmatthews glennmatthews deleted the gfm-cache-pruned-fields branch December 1, 2021 15:00
glennmatthews added a commit that referenced this pull request Dec 1, 2021
glennmatthews added a commit that referenced this pull request Dec 3, 2021
* Add caching of opt-in fields pruned_fields property

* Address review comment
glennmatthews added a commit that referenced this pull request Dec 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants