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

Performance updates in self.update_user_groups method #230

Closed
wants to merge 3 commits into from

Conversation

theognis1002
Copy link

@theognis1002 theognis1002 commented Apr 8, 2022

Made some very slight performance updates in the self.update_user_groups method. values_list() retrieves a subset of data without the overhead of creating model instances

@sondrelg
Copy link
Member

sondrelg commented Apr 9, 2022

The values_list changes look good, but can you explain why it makes sense to replace set with add? I'm not that familiar with this code, so just want to know your reasoning before diving in further 🙂

@JonasKs
Copy link
Member

JonasKs commented Apr 10, 2022

Group tests fails, so I'm not sure if this works as intended?

@theognis1002
Copy link
Author

@JonasKs ahh thx! didn't see that they failed at the time. I reverted it back to set.

@codecov
Copy link

codecov bot commented Apr 11, 2022

Codecov Report

Merging #230 (e94bb5a) into master (5067f8f) will not change coverage.
The diff coverage is 100.0%.

@@          Coverage Diff           @@
##           master    #230   +/-   ##
======================================
  Coverage    86.0%   86.0%           
======================================
  Files          11      11           
  Lines         515     515           
======================================
  Hits          443     443           
  Misses         72      72           
Impacted Files Coverage Δ
django_auth_adfs/backend.py 85.8% <100.0%> (ø)

@JonasKs
Copy link
Member

JonasKs commented Apr 11, 2022

This looks good to me.

@sondrelg, @tim-schilling , opinions? 😊

@sondrelg
Copy link
Member

Could you please explain how

list(existing_groups.iterator()) + new_groups

is more performant than this?

existing_groups + new_groups

I'm sure it could be, but to me it just looks like extra steps 🙂

@theognis1002
Copy link
Author

@sondrelg list(existing_groups.iterator()) was just taken from L213 and moved down. This was so that the queryset would not be evaluated before it was needed

@sondrelg
Copy link
Member

Ah yeah I see 👍

That being said, I'm not sure the iterator() call is worth keeping. Doing a little testing, it seems like the list call is all that's needed.

from django.contrib.auth.models import Group

g = Group.objects.all()

print(list(g) == list(g.iterator()))

>> True

I could be wrong though 🙂

Another alternative to combining lists here might be to merge querysets (if new_groups's queryset is available), using the pipe (|) operator.

Otherwise, looks good to me!

@tim-schilling
Copy link
Member

This looks good to me. It reminds me that I need to find time to do that benchmarking on #220

@JonasKs
Copy link
Member

JonasKs commented May 6, 2022

Hey @theognis1002 , could you address @sondrelg's comment so we can merge? 😊

existing_groups = list(Group.objects.filter(name__in=claim_groups).iterator())
existing_group_names = frozenset(group.name for group in existing_groups)
existing_groups = Group.objects.filter(name__in=claim_groups)
existing_group_names = frozenset(existing_groups.values_list("name", flat=True))
Copy link
Contributor

Choose a reason for hiding this comment

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

This values_list call will generate a SQL query and the lazyexisting_groups is casted to list later (one query), the new code adds a new SQL query.

@tim-schilling
Copy link
Member

I'm closing this since we merged #255. If you feel that this still improves the situation, please rebase on main and re-open the PR.

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.

5 participants