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

Optimize SetCookieOnResponse() #2819

Closed
wants to merge 3 commits into from
Closed

Optimize SetCookieOnResponse() #2819

wants to merge 3 commits into from

Conversation

guscarreon
Copy link
Contributor

As implemented in #1004, function SetCookieOnResponse() removes the oldest elements from the cookie.uids map if it needs extra space to meet the cfg.MaxCookieSizeBytes threshold. But the implementation can be improved. Let:

   n = len(cookie.uids)
   m = cookie.uids that need to be removed  so cookieSize <= cfg.MaxCookieSizeBytes

The current implementatio has square time complexity O(n^2) because m = n in the worst case scenario. This pull request removes the nested for loop in favor of sorting by expiration date so the new time complexity is O(n*logn).

Copy link
Contributor

@SyntaxNode SyntaxNode left a comment

Choose a reason for hiding this comment

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

I like the initiative to improve the performance of this algorithm. Please coordinate with @AlexBVolcy who is currently working in this area of code (user sync cookie).

})

// Delete elements until we need no more space to free
for _, oldestKey := range uuidKeys {
Copy link
Contributor

Choose a reason for hiding this comment

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

IMHO it would be more expressive to use currSize <= cfg.MaxCookieSizeBytes as the for condition and pop the last item from the slice which would be the next oldest item.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point. Modified

@bsardo
Copy link
Collaborator

bsardo commented Jun 7, 2023

Discussed offline. Closing as this logic will be incorporated in an upcoming PR.

@bsardo bsardo closed this Jun 7, 2023
@guscarreon guscarreon mentioned this pull request Jul 7, 2023
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.

4 participants