Skip to content

speed up matching of filter fields by using a set #9684

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

Conversation

KommuSoft
Copy link

A set uses O(1) average lookup time, so we first get all sorting fields (takes O(n)), but then we can do the matching in O(n), whereas now it is worst-case O(n^2).

@auvipy auvipy requested a review from Copilot April 16, 2025 07:46
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

Comments suppressed due to low confidence (1)

rest_framework/filters.py:314

  • Switching from a list to a set alters the ordering and potential handling of duplicates. Confirm that downstream code does not rely on the order or duplicate behavior of valid_fields.
valid_fields = {item[0] for item in self.get_valid_fields(queryset, view, {'request': request})}

@@ -311,7 +311,7 @@ def get_valid_fields(self, queryset, view, context={}):
return valid_fields

def remove_invalid_fields(self, queryset, fields, view, request):
valid_fields = [item[0] for item in self.get_valid_fields(queryset, view, {'request': request})]
valid_fields = {item[0] for item in self.get_valid_fields(queryset, view, {'request': request})}
Copy link
Member

Choose a reason for hiding this comment

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

can you add/update tests to verify this

@tomchristie
Copy link
Member

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

3 participants