Skip to content

⚡ Optimize loop in purge_cold over cold_data paths#51

Open
dioncx wants to merge 1 commit into
mainfrom
jules-8034458014061861489-6c910fb6
Open

⚡ Optimize loop in purge_cold over cold_data paths#51
dioncx wants to merge 1 commit into
mainfrom
jules-8034458014061861489-6c910fb6

Conversation

@dioncx

@dioncx dioncx commented Jun 12, 2026

Copy link
Copy Markdown
Owner

💡 What: Replaced the loop in purge_cold inside json_memory/memory.py over cold_data paths with list comprehensions and slicing. Replaced the individual element checking with a precalculated split index.

🎯 Why: Iterating over paths_by_age evaluating index math conditionally to append to lists individually incurs high loop overhead. List comprehensions and slicing are evaluated natively by C in Python, bypassing python interpreter overhead.

📊 Measured Improvement: The change yields a > 50% performance improvement on large data.

Baseline measured:
Original logic running 1,000,000 paths purging 950,000 paths took 0.2057s.
Optimized logic took 0.0886s.
Improvement: ~56.94%

Important Edge case:
A standard paths_by_age[:-keep_last] will fail on keep_last == 0 since paths_by_age[:-0] evaluates to paths_by_age[:] copying the whole array. It is solved safely via split_idx = max(0, len(paths_by_age) - keep_last) calculating the index precisely.


PR created automatically by Jules for task 8034458014061861489 started by @dioncx

💡 What: Replaced the enumerate loop checking the index against the length on every iteration with a calculated split index and two list comprehensions using list slicing.
🎯 Why: Iterating over paths_by_age to conditionally append elements individually incurs high loop overhead and redundant computation.
📊 Measured Improvement: Measured using a script generating 1,000,000 dummy paths and purging 950,000. Time decreased from ~0.2057s down to ~0.0886s, netting a ~56.9% performance improvement on large structures. Note: The code handles the edge case where keep_last evaluates to 0 safely to avoid the array[:-0] pitfall that duplicates the entire list.

Co-authored-by: dioncx <148190661+dioncx@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 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 @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

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.

1 participant