mirror of
https://github.com/astral-sh/ruff.git
synced 2025-10-01 22:31:23 +00:00
[red-knot] Improve property test performance by cloning db instead of holding MutexGuard
(#16823)
Some checks are pending
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
Some checks are pending
CI / cargo fmt (push) Waiting to run
CI / cargo clippy (push) Blocked by required conditions
CI / Determine changes (push) Waiting to run
CI / cargo test (linux) (push) Blocked by required conditions
CI / cargo test (linux, release) (push) Blocked by required conditions
CI / cargo test (windows) (push) Blocked by required conditions
CI / cargo test (wasm) (push) Blocked by required conditions
CI / cargo build (release) (push) Waiting to run
CI / cargo build (msrv) (push) Blocked by required conditions
CI / cargo fuzz build (push) Blocked by required conditions
CI / fuzz parser (push) Blocked by required conditions
CI / test scripts (push) Blocked by required conditions
CI / formatter instabilities and black similarity (push) Blocked by required conditions
CI / ecosystem (push) Blocked by required conditions
CI / cargo shear (push) Blocked by required conditions
CI / python package (push) Waiting to run
CI / pre-commit (push) Waiting to run
CI / mkdocs (push) Waiting to run
CI / test ruff-lsp (push) Blocked by required conditions
CI / check playground (push) Blocked by required conditions
CI / benchmarks (push) Blocked by required conditions
## Summary This PR brings an optimization. - `get_cached_db` no longer returns a `MutexGuard`; instead, it returns a cloned database. ### `get_cached_db` Previously, the `MutexGuard` was held inside the property test function (defined in the macro), which prevented multiple property tests from running in parallel. More specifically, the program could only test one random test case at a time, which likely caused a significant bottleneck. On my local machine, running: ``` QUICKCHECK_TESTS=100000 cargo test --release -p red_knot_python_semantic -- --ignored stable ``` showed about **a 75% speedup** (from \~60s to \~15s).
This commit is contained in:
parent
c9cd0acaeb
commit
3e2cf5d7c4
1 changed files with 4 additions and 5 deletions
|
@ -24,7 +24,7 @@
|
||||||
//! --ignored types::property_tests::stable; do :; done
|
//! --ignored types::property_tests::stable; do :; done
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
use std::sync::{Arc, Mutex, MutexGuard, OnceLock};
|
use std::sync::{Arc, Mutex, OnceLock};
|
||||||
|
|
||||||
use crate::db::tests::{setup_db, TestDb};
|
use crate::db::tests::{setup_db, TestDb};
|
||||||
use crate::symbol::{builtins_symbol, known_module_symbol};
|
use crate::symbol::{builtins_symbol, known_module_symbol};
|
||||||
|
@ -327,9 +327,9 @@ impl Arbitrary for Ty {
|
||||||
|
|
||||||
static CACHED_DB: OnceLock<Arc<Mutex<TestDb>>> = OnceLock::new();
|
static CACHED_DB: OnceLock<Arc<Mutex<TestDb>>> = OnceLock::new();
|
||||||
|
|
||||||
fn get_cached_db() -> MutexGuard<'static, TestDb> {
|
fn get_cached_db() -> TestDb {
|
||||||
let db = CACHED_DB.get_or_init(|| Arc::new(Mutex::new(setup_db())));
|
let db = CACHED_DB.get_or_init(|| Arc::new(Mutex::new(setup_db())));
|
||||||
db.lock().unwrap()
|
db.lock().unwrap().clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A macro to define a property test for types.
|
/// A macro to define a property test for types.
|
||||||
|
@ -348,8 +348,7 @@ macro_rules! type_property_test {
|
||||||
#[quickcheck_macros::quickcheck]
|
#[quickcheck_macros::quickcheck]
|
||||||
#[ignore]
|
#[ignore]
|
||||||
fn $test_name($($types: super::Ty),+) -> bool {
|
fn $test_name($($types: super::Ty),+) -> bool {
|
||||||
let db_cached = super::get_cached_db();
|
let $db = &super::get_cached_db();
|
||||||
let $db = &*db_cached;
|
|
||||||
$(let $types = $types.into_type($db);)+
|
$(let $types = $types.into_type($db);)+
|
||||||
|
|
||||||
$property
|
$property
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue