Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change to use parking_lot::RwLock for better performance #6014

Merged
merged 3 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions sway-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ itertools = "0.10"
lazy_static = "1.4"
miden-core = "0.3.0"
object = { version = "0.32.2", features = ["write"] }
parking_lot = "0.12"
pest = "2.1.3"
pest_derive = "2.1"
petgraph = "0.6"
Expand Down
22 changes: 10 additions & 12 deletions sway-core/src/concurrent_slab.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::{
fmt,
sync::{Arc, RwLock},
};
use parking_lot::RwLock;
use std::{fmt, sync::Arc};

#[derive(Debug, Clone)]
pub struct Inner<T> {
Expand All @@ -28,7 +26,7 @@ where
T: Clone,
{
fn clone(&self) -> Self {
let inner = self.inner.read().unwrap();
let inner = self.inner.read();
Self {
inner: RwLock::new(inner.clone()),
}
Expand Down Expand Up @@ -69,12 +67,12 @@ where
{
#[allow(dead_code)]
pub fn len(&self) -> usize {
let inner = self.inner.read().unwrap();
let inner = self.inner.read();
inner.items.len()
}

pub fn values(&self) -> Vec<Arc<T>> {
let inner = self.inner.read().unwrap();
let inner = self.inner.read();
inner.items.iter().filter_map(|x| x.clone()).collect()
}

Expand All @@ -83,7 +81,7 @@ where
}

pub fn insert_arc(&self, value: Arc<T>) -> usize {
let mut inner = self.inner.write().unwrap();
let mut inner = self.inner.write();

if let Some(free) = inner.free_list.pop() {
assert!(inner.items[free].is_none());
Expand All @@ -96,22 +94,22 @@ where
}

pub fn replace(&self, index: usize, new_value: T) -> Option<T> {
let mut inner = self.inner.write().unwrap();
let mut inner = self.inner.write();
let item = inner.items.get_mut(index)?;
let old = item.replace(Arc::new(new_value))?;
Arc::into_inner(old)
}

pub fn get(&self, index: usize) -> Arc<T> {
let inner = self.inner.read().unwrap();
let inner = self.inner.read();
inner.items[index]
.as_ref()
.expect("invalid slab index for ConcurrentSlab::get")
.clone()
}

pub fn retain(&self, predicate: impl Fn(&usize, &mut Arc<T>) -> bool) {
let mut inner = self.inner.write().unwrap();
let mut inner = self.inner.write();

let Inner { items, free_list } = &mut *inner;
for (idx, item) in items.iter_mut().enumerate() {
Expand All @@ -125,7 +123,7 @@ where
}

pub fn clear(&self) {
let mut inner = self.inner.write().unwrap();
let mut inner = self.inner.write();
inner.items.clear();
inner.items.shrink_to(0);

Expand Down
11 changes: 6 additions & 5 deletions sway-core/src/decl_engine/engine.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use parking_lot::RwLock;
use std::{
collections::{HashMap, HashSet, VecDeque},
fmt::Write,
sync::{Arc, RwLock},
sync::Arc,
};

use sway_types::{ModuleId, Named, Spanned};
Expand Down Expand Up @@ -48,7 +49,7 @@ impl Clone for DeclEngine {
constant_slab: self.constant_slab.clone(),
enum_slab: self.enum_slab.clone(),
type_alias_slab: self.type_alias_slab.clone(),
parents: RwLock::new(self.parents.read().unwrap().clone()),
parents: RwLock::new(self.parents.read().clone()),
}
}
}
Expand Down Expand Up @@ -187,7 +188,7 @@ macro_rules! decl_engine_clear_module {
($($slab:ident, $decl:ty);* $(;)?) => {
impl DeclEngine {
pub fn clear_module(&mut self, module_id: &ModuleId) {
self.parents.write().unwrap().retain(|key, _| {
self.parents.write().retain(|key, _| {
match key {
AssociatedItemDeclId::TraitFn(decl_id) => {
self.get_trait_fn(decl_id).span().source_id().map_or(true, |src_id| &src_id.module_id() != module_id)
Expand Down Expand Up @@ -244,7 +245,7 @@ impl DeclEngine {
AssociatedItemDeclId: From<&'a T>,
{
let index: AssociatedItemDeclId = AssociatedItemDeclId::from(index);
let parents = self.parents.read().unwrap();
let parents = self.parents.read();
let mut acc_parents: HashMap<AssociatedItemDeclId, AssociatedItemDeclId> = HashMap::new();
let mut already_checked: HashSet<AssociatedItemDeclId> = HashSet::new();
let mut left_to_check: VecDeque<AssociatedItemDeclId> = VecDeque::from([index]);
Expand Down Expand Up @@ -289,7 +290,7 @@ impl DeclEngine {
) where
AssociatedItemDeclId: From<DeclId<I>>,
{
let mut parents = self.parents.write().unwrap();
let mut parents = self.parents.write();
parents
.entry(index)
.and_modify(|e| e.push(parent.clone()))
Expand Down
18 changes: 6 additions & 12 deletions sway-core/src/query_engine/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::path::PathBuf;
use std::sync::RwLock;
use std::time::SystemTime;
use std::{collections::HashMap, sync::Arc};

use parking_lot::RwLock;
use std::{collections::HashMap, path::PathBuf, sync::Arc, time::SystemTime};
use sway_error::error::CompileError;
use sway_error::warning::CompileWarning;

Expand Down Expand Up @@ -55,28 +52,25 @@ pub struct QueryEngine {

impl QueryEngine {
pub fn get_parse_module_cache_entry(&self, path: &ModuleCacheKey) -> Option<ModuleCacheEntry> {
let cache = self.parse_module_cache.read().unwrap();
let cache = self.parse_module_cache.read();
cache.get(path).cloned()
}

pub fn insert_parse_module_cache_entry(&self, entry: ModuleCacheEntry) {
let path = entry.path.clone();
let include_tests = entry.include_tests;
let key = ModuleCacheKey::new(path, include_tests);
let mut cache = self.parse_module_cache.write().unwrap();
let mut cache = self.parse_module_cache.write();
cache.insert(key, entry);
}

pub fn get_programs_cache_entry(&self, path: &Arc<PathBuf>) -> Option<ProgramsCacheEntry> {
let cache = self
.programs_cache
.read()
.expect("Failed to read programs cache");
let cache = self.programs_cache.read();
cache.get(path).cloned()
}

pub fn insert_programs_cache_entry(&self, entry: ProgramsCacheEntry) {
let mut cache = self.programs_cache.write().unwrap();
let mut cache = self.programs_cache.write();
cache.insert(entry.path.clone(), entry);
}
}
18 changes: 8 additions & 10 deletions sway-core/src/type_system/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ use crate::{
};
use core::fmt::Write;
use hashbrown::{hash_map::RawEntryMut, HashMap};
use std::sync::{Arc, RwLock};
use parking_lot::RwLock;
use std::sync::Arc;
use sway_error::{
error::CompileError,
handler::{ErrorEmitted, Handler},
Expand All @@ -26,7 +27,7 @@ impl Clone for TypeEngine {
fn clone(&self) -> Self {
TypeEngine {
slab: self.slab.clone(),
id_map: RwLock::new(self.id_map.read().unwrap().clone()),
id_map: RwLock::new(self.id_map.read().clone()),
}
}
}
Expand All @@ -45,7 +46,7 @@ impl TypeEngine {
type_info: ty.clone().into(),
source_id,
};
let mut id_map = self.id_map.write().unwrap();
let mut id_map = self.id_map.write();

let hash_builder = id_map.hasher().clone();
let ty_hash = make_hasher(&hash_builder, engines)(&tsi);
Expand All @@ -72,13 +73,10 @@ impl TypeEngine {
Some(source_id) => &source_id.module_id() != module_id,
None => true,
});
self.id_map
.write()
.unwrap()
.retain(|tsi, _| match tsi.source_id {
Some(source_id) => &source_id.module_id() != module_id,
None => true,
});
self.id_map.write().retain(|tsi, _| match tsi.source_id {
Some(source_id) => &source_id.module_id() != module_id,
None => true,
});
}

pub fn replace(&self, id: TypeId, new_value: TypeSourceInfo) {
Expand Down
1 change: 1 addition & 0 deletions sway-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ indexmap = "2.0.0"
lazy_static = "1.4"
num-bigint = "0.4.3"
num-traits = "0.2.16"
parking_lot = "0.12"
rustc-hash = "1.1.0"
serde = { version = "1.0", features = ["derive"] }
sway-utils = { version = "0.58.0", path = "../sway-utils" }
Expand Down
41 changes: 20 additions & 21 deletions sway-types/src/source_engine.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{ModuleId, SourceId};
use parking_lot::RwLock;
use std::{
collections::{BTreeSet, HashMap},
path::PathBuf,
sync::RwLock,
};

/// The Source Engine manages a relationship between file paths and their corresponding
Expand All @@ -27,12 +27,12 @@ pub struct SourceEngine {
impl Clone for SourceEngine {
fn clone(&self) -> Self {
SourceEngine {
next_source_id: RwLock::new(*self.next_source_id.read().unwrap()),
path_to_source_map: RwLock::new(self.path_to_source_map.read().unwrap().clone()),
source_to_path_map: RwLock::new(self.source_to_path_map.read().unwrap().clone()),
next_module_id: RwLock::new(*self.next_module_id.read().unwrap()),
path_to_module_map: RwLock::new(self.path_to_module_map.read().unwrap().clone()),
module_to_sources_map: RwLock::new(self.module_to_sources_map.read().unwrap().clone()),
next_source_id: RwLock::new(*self.next_source_id.read()),
path_to_source_map: RwLock::new(self.path_to_source_map.read().clone()),
source_to_path_map: RwLock::new(self.source_to_path_map.read().clone()),
next_module_id: RwLock::new(*self.next_module_id.read()),
path_to_module_map: RwLock::new(self.path_to_module_map.read().clone()),
module_to_sources_map: RwLock::new(self.module_to_sources_map.read().clone()),
}
}
}
Expand All @@ -45,16 +45,16 @@ impl SourceEngine {
/// existing ID. If not, a new ID will be created.
pub fn get_source_id(&self, path: &PathBuf) -> SourceId {
{
let source_map = self.path_to_source_map.read().unwrap();
let source_map = self.path_to_source_map.read();
if source_map.contains_key(path) {
return source_map.get(path).copied().unwrap();
}
}
let manifest_path = sway_utils::find_parent_manifest_dir(path).unwrap_or(path.clone());
let module_id = {
let mut module_map = self.path_to_module_map.write().unwrap();
let mut module_map = self.path_to_module_map.write();
*module_map.entry(manifest_path.clone()).or_insert_with(|| {
let mut next_id = self.next_module_id.write().unwrap();
let mut next_id = self.next_module_id.write();
*next_id += 1;
ModuleId::new(*next_id)
})
Expand All @@ -65,25 +65,25 @@ impl SourceEngine {

pub fn get_source_id_with_module_id(&self, path: &PathBuf, module_id: ModuleId) -> SourceId {
{
let source_map = self.path_to_source_map.read().unwrap();
let source_map = self.path_to_source_map.read();
if source_map.contains_key(path) {
return source_map.get(path).copied().unwrap();
}
}

let source_id = SourceId::new(module_id.id, *self.next_source_id.read().unwrap());
let source_id = SourceId::new(module_id.id, *self.next_source_id.read());
{
let mut next_id = self.next_source_id.write().unwrap();
let mut next_id = self.next_source_id.write();
*next_id += 1;

let mut source_map = self.path_to_source_map.write().unwrap();
let mut source_map = self.path_to_source_map.write();
source_map.insert(path.clone(), source_id);

let mut path_map = self.source_to_path_map.write().unwrap();
let mut path_map = self.source_to_path_map.write();
path_map.insert(source_id, path.clone());
}

let mut module_map = self.module_to_sources_map.write().unwrap();
let mut module_map = self.module_to_sources_map.write();
module_map.entry(module_id).or_default().insert(source_id);

source_id
Expand All @@ -97,20 +97,19 @@ impl SourceEngine {
pub fn get_path(&self, source_id: &SourceId) -> PathBuf {
self.source_to_path_map
.read()
.unwrap()
.get(source_id)
.unwrap()
.clone()
}

/// This function provides the module ID corresponding to a specified file path.
pub fn get_module_id(&self, path: &PathBuf) -> Option<ModuleId> {
self.path_to_module_map.read().unwrap().get(path).copied()
self.path_to_module_map.read().get(path).copied()
}

/// Returns the [PathBuf] associated with the provided [ModuleId], if it exists in the path_to_module_map.
pub fn get_path_from_module_id(&self, module_id: &ModuleId) -> Option<PathBuf> {
let path_to_module_map = self.path_to_module_map.read().unwrap();
let path_to_module_map = self.path_to_module_map.read();
path_to_module_map
.iter()
.find(|(_, &id)| id == *module_id)
Expand All @@ -127,14 +126,14 @@ impl SourceEngine {
}

pub fn all_files(&self) -> Vec<PathBuf> {
let s = self.source_to_path_map.read().unwrap();
let s = self.source_to_path_map.read();
let mut v = s.values().cloned().collect::<Vec<_>>();
v.sort();
v
}

pub fn get_source_ids_from_module_id(&self, module_id: ModuleId) -> Option<BTreeSet<SourceId>> {
let s = self.module_to_sources_map.read().unwrap();
let s = self.module_to_sources_map.read();
s.get(&module_id).cloned()
}
}
Loading