Skip to content

Replace once_cell with std::sync::OnceLock/core::cell::OnceCell#7077

Merged
youknowone merged 2 commits intoRustPython:mainfrom
youknowone:once-cell
Feb 11, 2026
Merged

Replace once_cell with std::sync::OnceLock/core::cell::OnceCell#7077
youknowone merged 2 commits intoRustPython:mainfrom
youknowone:once-cell

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Feb 11, 2026

  • Replace once_cell::sync::{Lazy, OnceCell} with std::sync::{LazyLock, OnceLock}
  • Replace once_cell::unsync::{Lazy, OnceCell} with core::cell::{LazyCell, OnceCell}
  • Inline get_or_try_init at call sites (unstable in std as of 1.93)
  • Replace OnceCell::with_value() with OnceCell::from() in codecs.rs
  • Remove once_cell direct dependency from common and vm crates

Summary by CodeRabbit

  • Chores
    • Removed an external dependency and refactored internal lazy initialization patterns throughout the codebase.

- Replace `once_cell::sync::{Lazy, OnceCell}` with
  `std::sync::{LazyLock, OnceLock}`
- Replace `once_cell::unsync::{Lazy, OnceCell}` with
  `core::cell::{LazyCell, OnceCell}`
- Inline `get_or_try_init` at call sites (unstable in std as of 1.93)
- Replace `OnceCell::with_value()` with `OnceCell::from()` in codecs.rs
- Remove `once_cell` direct dependency from common and vm crates
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 11, 2026

📝 Walkthrough

Walkthrough

The PR removes the once_cell crate dependency from the project by migrating to standard library equivalents: std::sync::LazyLock/OnceLock for threading contexts and core::cell::LazyCell/OnceCell for non-threaded contexts. Implementation details across multiple VM and common modules are updated to use explicit lazy initialization patterns.

Changes

Cohort / File(s) Summary
Dependency Removals
Cargo.toml, crates/common/Cargo.toml, crates/vm/Cargo.toml
Removed once_cell dependencies from workspace and crate manifests.
Lock Module Reexports
crates/common/src/lock.rs
Updated public re-exports to alias stdlib types: LazyLock as Lazy and OnceLock as OnceCell (threaded), or LazyCell as Lazy and OnceCell (non-threaded), replacing once_cell crate references.
Static Cell Implementation
crates/common/src/static_cell.rs
Replaced get_or_try_init delegation with explicit initialization: check for existing value via get(), compute if absent, store via set(), and return reference.
VM Lazy Initialization
crates/vm/src/builtins/function.rs, crates/vm/src/builtins/memory.rs, crates/vm/src/codecs.rs, crates/vm/src/stdlib/os.rs
Replaced get_or_try_init closures with explicit check-compute-cache patterns using get() and set() calls. Updated initialization of exception cells to use From-based construction instead of with_value.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 The once\_cell is gone, hooray, hooray!
We've swapped the crate for stdlib's way
LazyLock and OnceLock now take the stage
Explicit get and set fill every page
Built-in types, no external cage! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 45.45% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: replacing the once_cell crate with standard library equivalents (std::sync::OnceLock and core::cell::OnceCell).

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

No actionable comments were generated in the recent review. 🎉

🧹 Recent nitpick comments
crates/common/src/static_cell.rs (1)

30-40: Threading variant: f() may be invoked concurrently by multiple threads.

Unlike OnceLock::get_or_try_init (which blocks competing callers), this manual get/set pattern allows multiple threads to evaluate f() simultaneously, discarding all but the winner's result. This is functionally safe but differs from the previous once_cell behavior which serialized initialization. If f() is expensive or has side effects, callers should be aware.

Given this is an accepted trade-off for avoiding unstable APIs, just flagging for awareness.


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 and usage tips.

@github-actions
Copy link
Contributor

Code has been automatically formatted

The code in this PR has been formatted using:

  • cargo fmt --all
    Please pull the latest changes before pushing again:
git pull origin once-cell

@youknowone youknowone marked this pull request as ready for review February 11, 2026 01:58
@youknowone youknowone merged commit c06cf56 into RustPython:main Feb 11, 2026
13 checks passed
@youknowone youknowone deleted the once-cell branch February 11, 2026 02:08
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