Skip to content

Conversation

@albertvillanova
Copy link
Member

@albertvillanova albertvillanova commented Oct 17, 2025

Optimize comprehension evaluation with generator-based approach in LocalPythonExecutor.

This PR refactors the comprehension evaluation logic to use a generator-based approach for better performance and memory efficiency.

Performance Benefits

  1. Memory efficiency: Eliminates intermediate list allocations during comprehension evaluation
  2. Lazy evaluation: Only computes what's needed, especially beneficial for set() and dict() where duplicates are discarded
  3. Optimized construction: list(generator) and set(generator) are highly optimized in CPython
  4. Reduced overhead: No more list wrapping/unwrapping in base case evaluators

Memory Complexity Improvement

The refactored implementation reduces memory overhead from O(n) to O(1) for the iteration mechanism itself:

  • Before: Built complete intermediate lists at each recursion level, requiring O(n) memory allocation per level
  • After: Yields elements one at a time using generator state, maintaining only O(1) memory for the iteration machinery regardless of comprehension size

Follow-up to:

Copy link
Collaborator

@aymeric-roucher aymeric-roucher left a comment

Choose a reason for hiding this comment

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

Super cool, thank you !

@albertvillanova albertvillanova merged commit a4850b9 into huggingface:main Oct 23, 2025
3 checks passed
@albertvillanova albertvillanova deleted the fu-1823 branch October 23, 2025 11:08
@suryabdev
Copy link
Contributor

Cool change!

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.

3 participants