Skip to content

Reuse unlocked buffers in os.write - #9107

Merged
youknowone merged 3 commits into
RustPython:mainfrom
1ndahous3:os_write_borrow_bytes
Oct 14, 2026
Merged

youknowone merged 3 commits into
RustPython:mainfrom
1ndahous3:os_write_borrow_bytes

Conversation

@1ndahous3

@1ndahous3 1ndahous3 commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  • Avoid allocating and copying immutable buffers before os.write.
  • Borrow storage owned by bytes, including memoryviews; snapshot other exporters, including readonly views over mutable raw memory, before releasing the interpreter thread.

Performance

Debian 13 amd64

os.write to /dev/null, median of seven samples, release build.

Workload Before After Speedup
64 KiB bytes 2.48 µs 1.14 µs 2.17×
1 MiB bytes 25.04 µs 1.12 µs 22.33×

AI assistance

Written with Codex (GPT-6), reviewed by a human before submission.

Summary by CodeRabbit

  • Performance
    • Reduced copying when writing byte-string data to the operating system, while preserving retry behavior for interrupted writes.
  • Reliability
    • Data from writable or externally managed buffers is copied before use, so changes to the underlying storage do not alter the copied data.

Assisted-by: Codex:gpt-6
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 14, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-14T12:40:23.237465Z 64343fa New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@coderabbitai

coderabbitai Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

Important

Review skipped

Review was skipped as selected files did not have any reviewable changes.

⚙️ Run configuration
  • Configuration used: Repository: RustPython/RustPython/.coderabbit.yml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 0cfd9899-22c2-4b88-b66a-c70bbb8d61fe
📥 Commits

Reviewing files that changed from the base of the PR and between 29338d7 and 64343fa.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: RustPython/RustPython/.coderabbit.yml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: f067c89a-9b76-4aed-9462-159bb6b956f3
📥 Commits

Reviewing files that changed from the base of the PR and between fb9ada1 and 29338d7.

📒 Files selected for processing: 3
  • crates/common/src/borrow.rs
  • crates/vm/src/function/buffer.rs
  • crates/vm/src/stdlib/_ctypes/function.rs

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 4 remain after this review.

📝 Walkthrough

borrow_buf_unlocked now borrows only unlocked buffers sourced from PyBytes; it copies buffers from other exporters. _os::write uses the borrowed buffer, with its existing EINTR signal-check and retry behavior unchanged.

Changes: OS write buffer

Layer / File(s) Summary
Restrict unlocked buffer borrowing
crates/vm/src/function/buffer.rs, crates/common/src/borrow.rs, crates/vm/src/stdlib/_ctypes/function.rs
Unlocked buffers are borrowed only when sourced from PyBytes; other buffers are copied. Documentation describes the borrowing limits, and tests check snapshots of raw-memory views and borrowing of bytes storage.
Borrow argument buffer
crates/vm/src/stdlib/os.rs
write uses an unlocked borrowed buffer instead of first creating an owned copy. Its existing interruption handling remains in place.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Refactor

Suggested reviewers: youknowone

Merge Risk: ⚪ Minimal · up to 29338

The write optimization preserves stable buffer contents during writes and retries. No merge-blocking issue was found.

Pre-merge checks: Passed 5
Passed Passed checks: 5 passed
Check name Status Explanation
Docstring Coverage Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 5 functions across 4 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely describes the primary change: reusing unlocked buffers in os.write to avoid unnecessary copying.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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

Comment @coderabbitai help to get the list of available commands.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fb9ada1053

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

Comment thread crates/vm/src/stdlib/os.rs
Assisted-by: Codex:gpt-6
@codspeed

codspeed Bot commented Oct 14, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 62 untouched benchmarks
⏩ 4 skipped benchmarks1


Comparing 1ndahous3:os_write_borrow_bytes (64343fa) with main (0816af6)

Open in CodSpeed

Footnotes

  1. 4 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@youknowone
youknowone merged commit c85829b into RustPython:main Oct 14, 2026
30 checks passed
@1ndahous3
1ndahous3 deleted the os_write_borrow_bytes branch October 15, 2026 09: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.

2 participants