Commit graph

3 commits

Author SHA1 Message Date
dmahan93
37f040a883 fix pre-commit 2025-05-09 19:14:45 -05:00
codeflash-ai[bot]
837ef6295d
️ Speed up function grab_exact_from_heterogeneous_queue by 1,680%
Here’s a highly optimized version of your code for both **runtime** and **memory**, based on the profile hot spots.

- **Avoid repeated summing** for checking lengths in a growing list — we keep a running sum.
- **Avoid repeatedly copying lists/dicts** by using lists of indices and marking to remove in one pass, and using set operations for fast membership checks.
- **Avoid creating lots of small dicts** and list extensions inside loops.
- **Combine related generator expressions** so costly operations are only done once.
- **Group similar linear scans** into one to minimize number of loops over `queue`.
- Use **pre-allocated lists and sets** where it saves time.

Here's the rewritten function (all comments preserved except where the code logic was changed).



**Key optimizations:**
- Only a *single pass* over queue for setup.
- No repeated `.append(dict)`; pass only indices around until the end.
- Use `.clear()` for lists inside dict to avoid reallocations.
- Use lists of lengths for O(1) access everywhere.
- Maintain a running sum for batch size check, not repeated `sum`.

This should **dramatically cut runtime**, especially at the hot spots from your line profiler output. If you need even more speed and the queue is huge/long-lived, consider reworking the data structure for the queue itself (`deque`, heap, etc.), but for code-level optimization this is near optimal for this algorithm!
2025-04-30 08:58:23 +00:00
Dakota Nous
621d00dd80 first commit 2025-04-29 12:10:10 -07:00