Skip to content

Performance: replace custom sort by native sort#605

Merged
niknetniko merged 2 commits intomasterfrom
perf/native-sort
Jan 24, 2026
Merged

Performance: replace custom sort by native sort#605
niknetniko merged 2 commits intomasterfrom
perf/native-sort

Conversation

@bmesuere
Copy link
Member

This pull request replaces the custom sort in utils.py by the built-in sort with cmp_to_key. There's a slight difference in output: the new version is stable whereas the original was not. All tests still pass.

In addition, I fixed a small bug in order fallback.

Performance of the function itself is ~50% better. When profiling the function in an actual exercise, the improvement was closer to 30%. Probably because it is then often called on (very) small lists. For exercises using sets, the impact is more noticable.

Next to the performance benefic, this simplifies the code.

Copilot AI review requested due to automatic review settings January 17, 2026 18:22
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request refactors the sorted_no_duplicates function in utils.py to use Python's native sorted() function with cmp_to_key instead of a custom TimSort implementation. The change delivers ~30-50% performance improvement while simplifying the code significantly. The PR also fixes a bug in the TypeError fallback path where a boolean was incorrectly returned instead of an integer comparison result.

Changes:

  • Replaced custom TimSort/insertion sort implementation with Python's built-in sorted() using cmp_to_key
  • Fixed TypeError fallback in the order() function to return proper comparison integers instead of boolean
  • Simplified duplicate filtering logic to be more straightforward

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@niknetniko niknetniko merged commit 6e80fb8 into master Jan 24, 2026
7 checks passed
@niknetniko niknetniko deleted the perf/native-sort branch January 24, 2026 16:58
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.

4 participants