Skip to content

Commit 3e2cf5d

Browse files
[red-knot] Improve property test performance by cloning db instead of holding MutexGuard (#16823)
## 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).
1 parent c9cd0ac commit 3e2cf5d

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

crates/red_knot_python_semantic/src/types/property_tests.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
//! --ignored types::property_tests::stable; do :; done
2525
//! ```
2626
27-
use std::sync::{Arc, Mutex, MutexGuard, OnceLock};
27+
use std::sync::{Arc, Mutex, OnceLock};
2828

2929
use crate::db::tests::{setup_db, TestDb};
3030
use crate::symbol::{builtins_symbol, known_module_symbol};
@@ -327,9 +327,9 @@ impl Arbitrary for Ty {
327327

328328
static CACHED_DB: OnceLock<Arc<Mutex<TestDb>>> = OnceLock::new();
329329

330-
fn get_cached_db() -> MutexGuard<'static, TestDb> {
330+
fn get_cached_db() -> TestDb {
331331
let db = CACHED_DB.get_or_init(|| Arc::new(Mutex::new(setup_db())));
332-
db.lock().unwrap()
332+
db.lock().unwrap().clone()
333333
}
334334

335335
/// A macro to define a property test for types.
@@ -348,8 +348,7 @@ macro_rules! type_property_test {
348348
#[quickcheck_macros::quickcheck]
349349
#[ignore]
350350
fn $test_name($($types: super::Ty),+) -> bool {
351-
let db_cached = super::get_cached_db();
352-
let $db = &*db_cached;
351+
let $db = &super::get_cached_db();
353352
$(let $types = $types.into_type($db);)+
354353

355354
$property

0 commit comments

Comments
 (0)