Skip to content

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.

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