⚡ Optimize random sampling of subscriptions#2
Conversation
Co-authored-by: Alchemist-Aloha <103620968+Alchemist-Aloha@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
💡 What: Replaced the full list shuffle
(subs.toList()..shuffle()).take(20)with an O(K) random-index rejection sampling algorithm that loops K times.🎯 Why: Previously we duplicated a list that could be thousands of items long into a new list, then shuffled the entire list, just to select up to 20 random elements from it. This was an O(N) memory and time operation where N can be large, when picking 20 elements can be done in O(K) time and space.
📊 Measured Improvement: We ran a benchmark allocating a list of 5,000 subscriptions and simulating sampling 20 elements 10,000 times:
PR created automatically by Jules for task 16529597882922160225 started by @Alchemist-Aloha