Skip to content

Commit

Permalink
Make the DeclarationEngine a lazy_static as a semi-temporary band…
Browse files Browse the repository at this point in the history
…aid (FuelLabs#2675)
  • Loading branch information
emilyaherbert authored Sep 1, 2022
1 parent 889b0ee commit 9076eb2
Show file tree
Hide file tree
Showing 15 changed files with 366 additions and 196 deletions.
21 changes: 9 additions & 12 deletions sway-core/src/concurrent_slab.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,37 @@
use std::{marker::PhantomData, sync::RwLock};
use std::sync::RwLock;

use crate::{type_system::TypeId, TypeInfo};

#[derive(Debug)]
pub(crate) struct ConcurrentSlab<I, T> {
indexer: PhantomData<I>,
pub(crate) struct ConcurrentSlab<T> {
inner: RwLock<Vec<T>>,
}

impl<I, T> Default for ConcurrentSlab<I, T>
impl<T> Default for ConcurrentSlab<T>
where
T: Default,
{
fn default() -> Self {
Self {
indexer: PhantomData,
inner: Default::default(),
}
}
}

impl<I, T> ConcurrentSlab<I, T>
impl<T> ConcurrentSlab<T>
where
T: Clone,
I: From<usize> + std::ops::Deref<Target = usize>,
{
pub fn insert(&self, value: T) -> I {
pub fn insert(&self, value: T) -> usize {
let mut inner = self.inner.write().unwrap();
let ret = inner.len();
inner.push(value);
ret.into()
ret
}

pub fn get(&self, index: I) -> T {
pub fn get(&self, index: usize) -> T {
let inner = self.inner.read().unwrap();
inner[*index].clone()
inner[index].clone()
}

pub fn clear(&self) {
Expand All @@ -48,7 +45,7 @@ where
}
}

impl ConcurrentSlab<TypeId, TypeInfo> {
impl ConcurrentSlab<TypeInfo> {
pub fn replace(
&self,
index: TypeId,
Expand Down
255 changes: 188 additions & 67 deletions sway-core/src/declaration_engine/declaration_engine.rs
Original file line number Diff line number Diff line change
@@ -1,151 +1,272 @@
use std::collections::HashMap;
use lazy_static::lazy_static;
use std::{collections::HashMap, sync::RwLock};
use sway_types::{Span, Spanned};

use crate::{
concurrent_slab::ConcurrentSlab,
semantic_analysis::{
TypedImplTrait, TypedStructDeclaration, TypedTraitDeclaration, TypedTraitFn,
},
TypedFunctionDeclaration,
CompileError, TypedFunctionDeclaration,
};

use super::{declaration_id::DeclarationId, declaration_wrapper::DeclarationWrapper};

lazy_static! {
static ref DECLARATION_ENGINE: DeclarationEngine = DeclarationEngine::default();
}

/// Used inside of type inference to store declarations.
#[derive(Debug)]
pub struct DeclarationEngine {
slab: ConcurrentSlab<DeclarationId, DeclarationWrapper>,
#[derive(Debug, Default)]
pub(crate) struct DeclarationEngine {
slab: ConcurrentSlab<DeclarationWrapper>,
// *declaration_id -> vec of monomorphized copies
// where the declaration_id is the original declaration
monomorphized_copies: HashMap<usize, Vec<DeclarationId>>,
monomorphized_copies: RwLock<HashMap<usize, Vec<DeclarationId>>>,
}

impl DeclarationEngine {
pub(crate) fn new() -> DeclarationEngine {
DeclarationEngine {
slab: ConcurrentSlab::default(),
monomorphized_copies: HashMap::new(),
}
fn clear(&self) {
self.slab.clear();
let mut monomorphized_copies = self.monomorphized_copies.write().unwrap();
monomorphized_copies.clear();
}

pub(crate) fn look_up_decl_id(&self, index: DeclarationId) -> DeclarationWrapper {
self.slab.get(index)
fn de_look_up_decl_id(&self, index: DeclarationId) -> DeclarationWrapper {
self.slab.get(*index)
}

pub(crate) fn add_monomorphized_copy(
&mut self,
original_id: DeclarationId,
new_id: DeclarationId,
) {
match self.monomorphized_copies.get_mut(&*original_id) {
fn de_add_monomorphized_copy(&self, original_id: DeclarationId, new_id: DeclarationId) {
let mut monomorphized_copies = self.monomorphized_copies.write().unwrap();
match monomorphized_copies.get_mut(&*original_id) {
Some(prev) => {
prev.push(new_id);
}
None => {
self.monomorphized_copies.insert(*original_id, vec![new_id]);
monomorphized_copies.insert(*original_id, vec![new_id]);
}
}
}

pub(crate) fn get_monomorphized_copies(
&self,
original_id: DeclarationId,
) -> Vec<DeclarationWrapper> {
match self.monomorphized_copies.get(&*original_id).cloned() {
Some(copies) => copies.into_iter().map(|copy| self.slab.get(copy)).collect(),
fn de_get_monomorphized_copies(&self, original_id: DeclarationId) -> Vec<DeclarationWrapper> {
let monomorphized_copies = self.monomorphized_copies.write().unwrap();
match monomorphized_copies.get(&*original_id).cloned() {
Some(copies) => copies
.into_iter()
.map(|copy| self.slab.get(*copy))
.collect(),
None => vec![],
}
}

pub(crate) fn insert_function(&self, function: TypedFunctionDeclaration) -> DeclarationId {
self.slab.insert(DeclarationWrapper::Function(function))
fn de_insert_function(&self, function: TypedFunctionDeclaration) -> DeclarationId {
let span = function.span();
DeclarationId::new(
self.slab.insert(DeclarationWrapper::Function(function)),
span,
)
}

pub(crate) fn get_function(
fn de_get_function(
&self,
index: DeclarationId,
) -> Result<TypedFunctionDeclaration, DeclarationWrapper> {
self.slab.get(index).expect_function()
span: &Span,
) -> Result<TypedFunctionDeclaration, CompileError> {
self.slab.get(*index).expect_function(span)
}

pub(crate) fn add_monomorphized_function_copy(
&mut self,
fn de_add_monomorphized_function_copy(
&self,
original_id: DeclarationId,
new_copy: TypedFunctionDeclaration,
) {
let new_id = self.slab.insert(DeclarationWrapper::Function(new_copy));
self.add_monomorphized_copy(original_id, new_id)
let span = new_copy.span();
let new_id = DeclarationId::new(
self.slab.insert(DeclarationWrapper::Function(new_copy)),
span,
);
self.de_add_monomorphized_copy(original_id, new_id)
}

pub(crate) fn get_monomorphized_function_copies(
fn de_get_monomorphized_function_copies(
&self,
original_id: DeclarationId,
) -> Result<Vec<TypedFunctionDeclaration>, DeclarationWrapper> {
self.get_monomorphized_copies(original_id)
span: &Span,
) -> Result<Vec<TypedFunctionDeclaration>, CompileError> {
self.de_get_monomorphized_copies(original_id)
.into_iter()
.map(|x| x.expect_function())
.map(|x| x.expect_function(span))
.collect::<Result<_, _>>()
}

pub(crate) fn insert_trait(&self, r#trait: TypedTraitDeclaration) -> DeclarationId {
self.slab.insert(DeclarationWrapper::Trait(r#trait))
fn de_insert_trait(&self, r#trait: TypedTraitDeclaration) -> DeclarationId {
let span = r#trait.name.span();
DeclarationId::new(self.slab.insert(DeclarationWrapper::Trait(r#trait)), span)
}

pub(crate) fn get_trait(
fn de_get_trait(
&self,
index: DeclarationId,
) -> Result<TypedTraitDeclaration, DeclarationWrapper> {
self.slab.get(index).expect_trait()
span: &Span,
) -> Result<TypedTraitDeclaration, CompileError> {
self.slab.get(*index).expect_trait(span)
}

pub(crate) fn insert_trait_fn(&self, trait_fn: TypedTraitFn) -> DeclarationId {
self.slab.insert(DeclarationWrapper::TraitFn(trait_fn))
fn de_insert_trait_fn(&self, trait_fn: TypedTraitFn) -> DeclarationId {
let span = trait_fn.name.span();
DeclarationId::new(
self.slab.insert(DeclarationWrapper::TraitFn(trait_fn)),
span,
)
}

pub(crate) fn get_trait_fn(
fn de_get_trait_fn(
&self,
index: DeclarationId,
) -> Result<TypedTraitFn, DeclarationWrapper> {
self.slab.get(index).expect_trait_fn()
span: &Span,
) -> Result<TypedTraitFn, CompileError> {
self.slab.get(*index).expect_trait_fn(span)
}

pub(crate) fn insert_trait_impl(&self, trait_impl: TypedImplTrait) -> DeclarationId {
self.slab.insert(DeclarationWrapper::TraitImpl(trait_impl))
fn insert_trait_impl(&self, trait_impl: TypedImplTrait) -> DeclarationId {
let span = trait_impl.span.clone();
DeclarationId::new(
self.slab.insert(DeclarationWrapper::TraitImpl(trait_impl)),
span,
)
}

pub(crate) fn get_trait_impl(
fn de_get_trait_impl(
&self,
index: DeclarationId,
) -> Result<TypedImplTrait, DeclarationWrapper> {
self.slab.get(index).expect_trait_impl()
span: &Span,
) -> Result<TypedImplTrait, CompileError> {
self.slab.get(*index).expect_trait_impl(span)
}

pub(crate) fn insert_struct(&self, r#struct: TypedStructDeclaration) -> DeclarationId {
self.slab.insert(DeclarationWrapper::Struct(r#struct))
fn de_insert_struct(&self, r#struct: TypedStructDeclaration) -> DeclarationId {
let span = r#struct.span();
DeclarationId::new(self.slab.insert(DeclarationWrapper::Struct(r#struct)), span)
}

pub(crate) fn get_struct(
fn de_get_struct(
&self,
index: DeclarationId,
) -> Result<TypedStructDeclaration, DeclarationWrapper> {
self.slab.get(index).expect_struct()
span: &Span,
) -> Result<TypedStructDeclaration, CompileError> {
self.slab.get(*index).expect_struct(span)
}

pub(crate) fn add_monomorphized_struct_copy(
&mut self,
fn de_add_monomorphized_struct_copy(
&self,
original_id: DeclarationId,
new_copy: TypedStructDeclaration,
) {
let new_id = self.slab.insert(DeclarationWrapper::Struct(new_copy));
self.add_monomorphized_copy(original_id, new_id)
let span = new_copy.span();
let new_id =
DeclarationId::new(self.slab.insert(DeclarationWrapper::Struct(new_copy)), span);
self.de_add_monomorphized_copy(original_id, new_id)
}

pub(crate) fn get_monomorphized_struct_copies(
fn de_get_monomorphized_struct_copies(
&self,
original_id: DeclarationId,
) -> Result<Vec<TypedStructDeclaration>, DeclarationWrapper> {
self.get_monomorphized_copies(original_id)
span: &Span,
) -> Result<Vec<TypedStructDeclaration>, CompileError> {
self.de_get_monomorphized_copies(original_id)
.into_iter()
.map(|x| x.expect_struct())
.map(|x| x.expect_struct(span))
.collect::<Result<_, _>>()
}
}

pub(crate) fn de_clear() {
DECLARATION_ENGINE.clear()
}

pub(crate) fn de_look_up_decl_id(index: DeclarationId) -> DeclarationWrapper {
DECLARATION_ENGINE.de_look_up_decl_id(index)
}

pub(crate) fn de_insert_function(function: TypedFunctionDeclaration) -> DeclarationId {
DECLARATION_ENGINE.de_insert_function(function)
}

pub(crate) fn de_get_function(
index: DeclarationId,
span: &Span,
) -> Result<TypedFunctionDeclaration, CompileError> {
DECLARATION_ENGINE.de_get_function(index, span)
}

pub(crate) fn de_add_monomorphized_function_copy(
original_id: DeclarationId,
new_copy: TypedFunctionDeclaration,
) {
DECLARATION_ENGINE.de_add_monomorphized_function_copy(original_id, new_copy);
}

pub(crate) fn de_get_monomorphized_function_copies(
original_id: DeclarationId,
span: &Span,
) -> Result<Vec<TypedFunctionDeclaration>, CompileError> {
DECLARATION_ENGINE.de_get_monomorphized_function_copies(original_id, span)
}

pub(crate) fn de_insert_trait(r#trait: TypedTraitDeclaration) -> DeclarationId {
DECLARATION_ENGINE.de_insert_trait(r#trait)
}

pub(crate) fn de_get_trait(
index: DeclarationId,
span: &Span,
) -> Result<TypedTraitDeclaration, CompileError> {
DECLARATION_ENGINE.de_get_trait(index, span)
}

pub(crate) fn de_insert_trait_fn(trait_fn: TypedTraitFn) -> DeclarationId {
DECLARATION_ENGINE.de_insert_trait_fn(trait_fn)
}

pub(crate) fn de_get_trait_fn(
index: DeclarationId,
span: &Span,
) -> Result<TypedTraitFn, CompileError> {
DECLARATION_ENGINE.de_get_trait_fn(index, span)
}

pub(crate) fn insert_trait_impl(trait_impl: TypedImplTrait) -> DeclarationId {
DECLARATION_ENGINE.insert_trait_impl(trait_impl)
}

pub(crate) fn de_get_trait_impl(
index: DeclarationId,
span: &Span,
) -> Result<TypedImplTrait, CompileError> {
DECLARATION_ENGINE.de_get_trait_impl(index, span)
}

pub(crate) fn de_insert_struct(r#struct: TypedStructDeclaration) -> DeclarationId {
DECLARATION_ENGINE.de_insert_struct(r#struct)
}

pub(crate) fn de_get_struct(
index: DeclarationId,
span: &Span,
) -> Result<TypedStructDeclaration, CompileError> {
DECLARATION_ENGINE.de_get_struct(index, span)
}

pub(crate) fn de_add_monomorphized_struct_copy(
original_id: DeclarationId,
new_copy: TypedStructDeclaration,
) {
DECLARATION_ENGINE.de_add_monomorphized_struct_copy(original_id, new_copy);
}

pub(crate) fn de_get_monomorphized_struct_copies(
original_id: DeclarationId,
span: &Span,
) -> Result<Vec<TypedStructDeclaration>, CompileError> {
DECLARATION_ENGINE.de_get_monomorphized_struct_copies(original_id, span)
}
Loading

0 comments on commit 9076eb2

Please sign in to comment.