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

Optimization: avoid setting role_name for each function call #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

matthewhegarty
Copy link
Contributor

I notice that when using the mixin, there are multiple repeated db calls to get the user's role name:

For example, I can see the following statement appearing 5 times for one request.

SELECT `auth_group`.`id`, `auth_group`.`name` FROM `auth_group` INNER JOIN `authuser_groups` ON (`auth_group`.`id` = `authuser_groups`.`group_id`) WHERE `authuser_groups`.`authuser_id` = 6

The reason is because _get_role() is called for each function in the VIEWSET_METHOD_REGISTRY, which involves a call to the db each time.

There is a simple performance enhancement which is to retain the role_name value on the object. I cannot foresee any issues with this, since the object exists for the lifetime of the request.

@@ -29,8 +29,9 @@ def __init__(self, **_kwargs):
def _call_role_fn(self, fn, *args, **kwargs):
"""Attempts to call a role-scoped method"""
try:
role_name = self._get_role(self.request.user)
role_fn = "{}_for_{}".format(fn, role_name)
if not getattr(self, '_role_name', None):

Choose a reason for hiding this comment

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

try using lru_cache from functools python module for result cache

https://docs.python.org/3/library/functools.html#functools.lru_cache

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.

2 participants