Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ def progress_summary(
"""Return completed and total lesson counts for user."""
completed_ids = _completed_lesson_ids(progress_service, user_id)
total_count = len(learning_path.lessons)
completed_count = sum(
1 for lesson in learning_path.lessons if lesson.id in completed_ids
# ⚑ Bolt: Use len(list comp) over sum(generator) for Python 3.12+ performance
completed_count = len(
[1 for lesson in learning_path.lessons if lesson.id in completed_ids]
)
Comment on lines +104 to 106

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

medium

While using len() with a list comprehension might offer a performance improvement on specific Python versions (e.g., 3.12+), it introduces a significant memory overhead by creating a temporary list. The sum() with a generator expression approach is more memory-efficient and is generally the preferred idiom for counting items in an iterable, especially when the number of items could be large.

The performance gain from this micro-optimization is unlikely to be substantial in the context of the overall application and may not justify the increased memory consumption and reliance on version-specific CPython optimizations.

A more concise and idiomatic way to achieve this count is by summing the boolean results of the condition directly within a generator. This approach is both readable and memory-efficient. With this change, the comment on line 103 should also be removed as it would no longer be applicable.

    completed_count = sum(lesson.id in completed_ids for lesson in learning_path.lessons)

return completed_count, total_count

Expand Down
Loading