Skip to content

Performance: Replace Mutex with RwLock for read-heavy data structures #286

@karthiknadig

Description

@karthiknadig

Summary

Several locators use Mutex for caching environments and managers, but these data structures are read far more often than written. Using RwLock would allow concurrent reads, improving performance during environment discovery.

Affected Files

1. crates/pet-conda/src/lib.rs

// Current:
pub environments: Arc<Mutex<HashMap<PathBuf, PythonEnvironment>>>,
pub managers: Arc<Mutex<HashMap<PathBuf, CondaManager>>>,

// Proposed:
pub environments: Arc<RwLock<HashMap<PathBuf, PythonEnvironment>>>,
pub managers: Arc<RwLock<HashMap<PathBuf, CondaManager>>>,

2. crates/pet-poetry/src/lib.rs

// Current:
pub workspace_directories: Arc<Mutex<Vec<PathBuf>>>,
pub poetry_executable: Arc<Mutex<Option<PathBuf>>>,
search_result: Arc<Mutex<Option<LocatorResult>>>,

3. crates/pet-linux-global-python/src/lib.rs

// Current:
reported_executables: Arc<Mutex<HashMap<PathBuf, PythonEnvironment>>>,

4. crates/pet-windows-store/src/lib.rs

// Current:
environments: Arc<Mutex<Option<Vec<PythonEnvironment>>>>,

5. crates/pet-reporter/src/cache.rs

// Current:
reported_managers: Arc<Mutex<HashMap<PathBuf, EnvManager>>>,
reported_environments: Arc<Mutex<HashMap<PathBuf, PythonEnvironment>>>,

Pattern to Follow

The JSONRPC server in crates/pet/src/jsonrpc.rs already correctly uses RwLock for configuration:

configuration: RwLock<Configuration>,

Migration Notes

  • Replace .lock() with .read() for read operations
  • Replace .lock() with .write() for write operations
  • Consider using try_read() / try_write() where appropriate

Impact

  • Allows multiple threads to read cached environments simultaneously
  • Particularly beneficial during parallel locator execution in find.rs
  • No functional change, purely performance optimization

Priority

Medium - Requires careful review of all lock usages but provides good concurrency improvement.

Metadata

Metadata

Assignees

Labels

debtCode quality issues

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions