jazzband / django-waffle

A feature flipper for Django
https://waffle.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.13k stars 259 forks source link

Improve performance of `is_active_for_user`. #346

Closed jmduke closed 5 years ago

jmduke commented 5 years ago

Howdy! Thanks for a great package. I have a small change for improving is_active_for_user query performance when checking flag enrollment for a flag without groups; namely, skipping the user.groups call (which generally results in a DB call.)

This should be a no-op; set().intersection(<anything>) == set(), so this should just be a easy little way to short-circuit.

jmduke commented 5 years ago

Here's an example before and after of the change. In the context processor for this view, I'm grabbing a list of views with Flag.get_all() and iterating over them. Think something like:

{flag.name: flag.is_active_for_user(obj.user) for flag in Flag.get_all()}

Before:

Screen Shot 2019-09-22 at 4 51 44 PM

After:

Screen Shot 2019-09-22 at 4 52 05 PM