perf: cache redundant sum() calls in repetition filters#924
perf: cache redundant sum() calls in repetition filters#924dubin555 wants to merge 1 commit intodatajuicer:mainfrom
Conversation
character_repetition_filter.py: - Cache sum(freq_char_ngrams) into local variable `total` to avoid computing O(n) sum twice (once for zero-check, once for division) - Remove redundant list() in sorted() call - Use list.count(1) instead of len([el for el if el == 1]) word_repetition_filter.py: - Cache sum(freq_word_ngrams) into local variable `total` to avoid computing O(n) sum twice Co-Authored-By: Claude <noreply@anthropic.com>
Summary of ChangesHello @dubin555, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request focuses on enhancing the performance of repetition filters by optimizing how frequency sums are calculated. It addresses redundant Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request effectively improves performance by eliminating redundant sum() calls in CharacterRepetitionFilter and WordRepetitionFilter, caching the result in a local variable. The changes also include other good optimizations, such as removing a redundant list() conversion and using the more efficient list.count() method. I've added one suggestion in word_repetition_filter.py for a further minor performance gain by using a generator expression, which aligns with the goal of this PR. Overall, these are solid improvements to the codebase.
|
Thanks for the review and approval! Agreed — I'll leave the code as-is and skip the generator expression change. Readability matters more here. |
Summary
Eliminate redundant
sum()computations inCharacterRepetitionFilterandWordRepetitionFilter.Problem
character_repetition_filter.py (line 71-74)
Additional issues on line 65-66:
sorted(list(values))—list()is redundant sincesorted()accepts any iterablelen([el for el in freq_char_ngrams if el == 1])— creates a temporary list just to count;list.count(1)is more efficientword_repetition_filter.py (line 106-107)
Fix
Cache the
sum()result into a local variabletotal:Test plan
python -m py_compile🤖 Generated with Claude Code