Skip to content

Add sort.py to microbenchmarks #6086

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

jackoconnordev
Copy link
Contributor

@jackoconnordev jackoconnordev commented Aug 9, 2025

Description

  • Adds a new microbenchmark for sorted (and by extension sort) builtin functions
  • Documents how to run individual benchmarks to the relevant README.

I chose 5 * ITERATIONS to better show the divergence between the sort implementations, while trying to avoid individual microbenchmark runs timing out.

Sample benchmark results

Violin plot
image

Line Chart
image

Manually benchmark

Using larger list sizes really shows the difference. Sorting 1_000_000 random numbers:

  • CPython => 0.3 seconds
  • RustPython => ~17 minutes
✦ ❯ time python -c "from random import random; sorted([random() for i in range(1_000_000)]); print('DONE');"
DONE

real    0m0.309s
user    0m0.274s
sys    0m0.036s

✦ ❯ time cargo run --release -- -c "from random import random; sorted([random() for i in range(1_000_000)]); print('DONE');"
    Finished `release` profile [optimized] target(s) in 0.16s
     Running `target/release/rustpython -c 'from random import random; sorted([random() for i in range(1_000_000)]); print('\''DONE'\'');'`
DONE

real    16m52.217s
user    16m51.926s
sys    0m0.174s

Summary by CodeRabbit

  • New Features
    • Added a new benchmark script to measure the performance of sorting random floating-point numbers.
  • Documentation
    • Updated the benchmarking README with instructions and examples for running specific benchmarks using name matching.

Copy link
Contributor

coderabbitai bot commented Aug 9, 2025

Walkthrough

A new Python benchmark script for sorting random floats was added, and the benchmarking README was updated to include instructions for running specific benchmarks by name using cargo bench. There were no changes to benchmark functionality or error handling.

Changes

Cohort / File(s) Change Summary
Benchmark Documentation
benches/README.md
Enhanced README with instructions for running a subset of benchmarks using name matching.
Sort Microbenchmark
benches/microbenchmarks/sort.py
Added a new Python script that benchmarks sorting a list of random floats using the list’s sort() method.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~7 minutes

Poem

🐇
In the meadow of code, a new script appears,
Sorting random numbers, swift as rabbit ears.
The README now guides with clarity bright,
Run just what you need—no more, no less, in sight.
Benchmarks hop forward, in orderly delight!

Note

🔌 MCP (Model Context Protocol) integration is now available in Early Access!

Pro users can now connect to remote MCP servers under the Integrations page to get reviews and chat conversations that understand additional development context.

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (1)
benches/README.md (1)

10-16: Extend example to include list.sort() microbench and show exact-match usage

Once you add the in-place list.sort() microbenchmark, surface it here. Also consider an exact-match example to avoid unintentionally matching other benchmarks.

Apply:

 ```shell
-cargo bench sort
+cargo bench sort
+# Or, for the in-place list.sort() microbenchmark:
+cargo bench list_sort
+# For an exact match on just "sort":
+cargo bench '^sort$'

</blockquote></details>

</blockquote></details>

<details>
<summary>📜 Review details</summary>

**Configuration used: .coderabbit.yml**
**Review profile: CHILL**
**Plan: Pro**


<details>
<summary>📥 Commits</summary>

Reviewing files that changed from the base of the PR and between a9a9e3bf118f6c631ef99fe7174215254f846097 and d96712bd950005d8d813bd3a4d8f44fb0426ec58.

</details>

<details>
<summary>📒 Files selected for processing (2)</summary>

* `benches/README.md` (1 hunks)
* `benches/microbenchmarks/sort.py` (1 hunks)

</details>

<details>
<summary>🧰 Additional context used</summary>

<details>
<summary>📓 Path-based instructions (1)</summary>

<details>
<summary>**/*.py</summary>


**📄 CodeRabbit Inference Engine (.github/copilot-instructions.md)**

> `**/*.py`: Follow PEP 8 style for custom Python code
> Use ruff for linting Python code

Files:
- `benches/microbenchmarks/sort.py`

</details>

</details><details>
<summary>🧠 Learnings (1)</summary>

<details>
<summary>📚 Learning: 2025-07-19T03:16:56.511Z</summary>

Learnt from: CR
PR: RustPython/RustPython#0
File: .github/copilot-instructions.md:0-0
Timestamp: 2025-07-19T03:16:56.511Z
Learning: Generate documentation with cargo doc --no-deps --all


**Applied to files:**
- `benches/README.md`

</details>

</details><details>
<summary>🧬 Code Graph Analysis (1)</summary>

<details>
<summary>benches/microbenchmarks/sort.py (2)</summary><blockquote>

<details>
<summary>stdlib/src/random.rs (1)</summary>

* `random` (42-45)

</details>
<details>
<summary>vm/src/stdlib/builtins.rs (1)</summary>

* `sorted` (815-820)

</details>

</blockquote></details>

</details><details>
<summary>🪛 Flake8 (7.2.0)</summary>

<details>
<summary>benches/microbenchmarks/sort.py</summary>

[error] 3-3: undefined name 'ITERATIONS'

(F821)

</details>

</details>

</details>

<details>
<summary>⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (11)</summary>

* GitHub Check: Run snippets and cpython tests (macos-latest)
* GitHub Check: Run snippets and cpython tests on wasm-wasi
* GitHub Check: Run snippets and cpython tests (windows-latest)
* GitHub Check: Run snippets and cpython tests (ubuntu-latest)
* GitHub Check: Run tests under miri
* GitHub Check: Check the WASM package and demo
* GitHub Check: Check Rust code with rustfmt and clippy
* GitHub Check: Run rust tests (ubuntu-latest)
* GitHub Check: Run rust tests (windows-latest)
* GitHub Check: Run rust tests (macos-latest)
* GitHub Check: Ensure compilation on various targets

</details>

</details>

<!-- This is an auto-generated comment by CodeRabbit for review status -->

Jack O'Connor added 2 commits August 10, 2025 09:09
I chose 5 * Iterations to try better show that RustPython
sort implementation scales noticeably worse CPython's
with respect to the number of elements.
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
benches/microbenchmarks/sort.py (1)

1-1: Ensure Consistent RNG Seeding Across Microbenchmarks

No other benchmarks in benches/microbenchmarks/ currently seed the RNG. If you’d like deterministic inputs for easier cross-run comparisons, consider adding a fixed seed at the top of benches/microbenchmarks/sort.py:

-from random import random
+from random import random, seed
+seed(0)

This change will align sort.py with a reproducible input setup—apply only if it matches the rest of your benchmarking strategy.

📜 Review details

Configuration used: .coderabbit.yml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d96712b and 5531ae2.

📒 Files selected for processing (2)
  • benches/README.md (1 hunks)
  • benches/microbenchmarks/sort.py (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • benches/README.md
🧰 Additional context used
📓 Path-based instructions (1)
**/*.py

📄 CodeRabbit Inference Engine (.github/copilot-instructions.md)

**/*.py: Follow PEP 8 style for custom Python code
Use ruff for linting Python code

Files:

  • benches/microbenchmarks/sort.py
🧠 Learnings (3)
📓 Common learnings
Learnt from: jackoconnordev
PR: RustPython/RustPython#6086
File: benches/microbenchmarks/sort.py:7-7
Timestamp: 2025-08-09T22:55:10.479Z
Learning: In RustPython benchmarks, when testing sorting performance, benchmarking only `sorted()` is sufficient since it internally uses the same sorting logic as `list.sort()`. The only difference is that `sorted()` creates a copy first, so testing both would be redundant for evaluating the sorting algorithm's performance.
📚 Learning: 2025-08-09T22:55:10.479Z
Learnt from: jackoconnordev
PR: RustPython/RustPython#6086
File: benches/microbenchmarks/sort.py:7-7
Timestamp: 2025-08-09T22:55:10.479Z
Learning: In RustPython benchmarks, when testing sorting performance, benchmarking only `sorted()` is sufficient since it internally uses the same sorting logic as `list.sort()`. The only difference is that `sorted()` creates a copy first, so testing both would be redundant for evaluating the sorting algorithm's performance.

Applied to files:

  • benches/microbenchmarks/sort.py
📚 Learning: 2025-08-09T22:56:24.504Z
Learnt from: jackoconnordev
PR: RustPython/RustPython#6086
File: benches/microbenchmarks/sort.py:1-3
Timestamp: 2025-08-09T22:56:24.504Z
Learning: In RustPython's microbenchmarks (benches/microbenchmarks/*.py), the variable `ITERATIONS` is intentionally used without being defined in the Python files. It is injected by the cargo bench harness at runtime. This pattern should be maintained for consistency across all microbenchmarks, and F821 lint warnings for undefined `ITERATIONS` are expected and acceptable in this context.

Applied to files:

  • benches/microbenchmarks/sort.py
🧬 Code Graph Analysis (1)
benches/microbenchmarks/sort.py (2)
stdlib/src/random.rs (1)
  • random (42-45)
vm/src/builtins/list.rs (1)
  • sort (323-337)
🪛 Flake8 (7.2.0)
benches/microbenchmarks/sort.py

[error] 3-3: undefined name 'ITERATIONS'

(F821)

🔇 Additional comments (2)
benches/microbenchmarks/sort.py (2)

3-3: ITERATIONS is intentionally harness-injected; keep as-is

Acknowledging the established pattern in RustPython microbenchmarks: ITERATIONS is provided by the bench harness. F821 from local linters is expected here and acceptable. No change requested.


7-7: No change required: setup is re-executed per iteration
The harness uses Criterion’s iter_batched_ref/iter_batched APIs with the bench_setup closure, which runs the setup block (including unsorted_list = […]) before each sample. That guarantees each sort operates on a fresh, random list.

• In benches/microbenchmarks.rs, see lines 78–86 (b.iter_batched_ref(|| bench_setup(Some(*idx)), …)) and lines 147–155 (b.iter_batched(|| bench_setup(Some(*idx)), …))
• Each invocation of the setup closure re-executes the code before timing, so no iteration ever sorts an already-sorted list.

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