Skip to content

Replace std::sync::LazyLock with common::lock::LazyLock#7079

Merged
youknowone merged 2 commits intoRustPython:mainfrom
youknowone:lazylock
Feb 11, 2026
Merged

Replace std::sync::LazyLock with common::lock::LazyLock#7079
youknowone merged 2 commits intoRustPython:mainfrom
youknowone:lazylock

Conversation

@youknowone
Copy link
Member

@youknowone youknowone commented Feb 11, 2026

lock.rs now exports both LazyCell and LazyLock:

  • threading: re-exports from std::sync / core::cell
  • non-threading: LazyCell from core::cell, LazyLock as LazyCell wrapper with Sync (safe without threading)

Summary by CodeRabbit

  • Refactor
    • Updated internal lazy initialization mechanisms to use an optimized local implementation instead of standard library dependencies, improving code consistency across the project.

lock.rs now exports both `LazyCell` and `LazyLock`:
  - threading: re-exports from std::sync / core::cell
  - non-threading: `LazyCell` from core::cell, `LazyLock` as
    LazyCell wrapper with Sync (safe without threading)
@youknowone youknowone marked this pull request as ready for review February 11, 2026 04:22
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Feb 11, 2026

📝 Walkthrough

Walkthrough

This change removes the Lazy alias and exposes LazyLock and LazyCell directly in the lock module, adds a LazyLock<T, F> wrapper with a Sync impl and helpers for non-threading builds, and updates many import sites to use rustpython_common::lock::LazyLock (or crate::common::lock::LazyLock).

Changes

Cohort / File(s) Summary
Lock Module Core
crates/common/src/lock.rs
Removed Lazy alias; re-exported LazyLock and LazyCell explicitly. Added public LazyLock<T, F> wrapper around core::cell::LazyCell with unsafe impl Sync, const fn new, force(&Self) -> &T, and Deref impl.
Stdlib imports
crates/stdlib/src/contextvars.rs, crates/stdlib/src/csv.rs, crates/stdlib/src/mmap.rs, crates/stdlib/src/openssl.rs, crates/stdlib/src/ssl/oid.rs, crates/stdlib/src/typing.rs
Replaced imports of std::sync::LazyLock with rustpython_common::lock::LazyLock and updated call sites (e.g., LazyLock::force).
VM builtins imports
crates/vm/src/builtins/...
bytes.rs, dict.rs, genericalias.rs, mappingproxy.rs, memory.rs, range.rs, set.rs, str.rs, template.rs, tuple.rs, union.rs, weakproxy.rs
Replaced std::sync::LazyLock imports with crate::common::lock::LazyLock across builtins.
VM stdlib & types imports
crates/vm/src/stdlib/ctypes/array.rs, crates/vm/src/stdlib/ctypes/pointer.rs, crates/vm/src/stdlib/sre.rs, crates/vm/src/types/structseq.rs
Replaced std::sync::LazyLock with crate::common::lock::LazyLock for static initializers and mapping/sequence helpers.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Suggested reviewers

  • ShaharNaveh
  • coolreader18
  • fanninpm

Poem

🐰 I nudged the locks to show their name,

No alias hiding, no secret game.
A wrapper snug for single-threaded cheer,
Imports changed — the path now clear.
Hooray, my code hops forward, bright and tame!

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.11% 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 pull request title accurately and concisely summarizes the main change: replacing std::sync::LazyLock with common::lock::LazyLock across multiple files, including the new LazyLock implementation in lock.rs.

✏️ 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/stdlib/src/ssl/oid.rs (1)

135-136: Consider importing LazyLock instead of using a fully qualified path.

Other files in this PR import LazyLock with a use statement, but here it's used fully qualified. For consistency:

♻️ Suggested refactor

Add at the top of the file:

use rustpython_common::lock::LazyLock;

Then simplify the declaration:

-static OID_TABLE: rustpython_common::lock::LazyLock<OidTable> =
-    rustpython_common::lock::LazyLock::new(OidTable::build);
+static OID_TABLE: LazyLock<OidTable> = LazyLock::new(OidTable::build);

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 lazylock

@youknowone youknowone enabled auto-merge (squash) February 11, 2026 07:09
@youknowone youknowone disabled auto-merge February 11, 2026 07:09
@youknowone youknowone merged commit ccd3d4f into RustPython:main Feb 11, 2026
13 checks passed
@youknowone youknowone deleted the lazylock branch February 11, 2026 07:09
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