Mutex::new() is now const

This commit is contained in:
Folkert 2022-11-09 17:51:43 +01:00
parent 8b53a13f0e
commit e92ceb7282
No known key found for this signature in database
GPG key ID: 1F17F6FFD112B97C
9 changed files with 31 additions and 45 deletions

3
Cargo.lock generated
View file

@ -3356,7 +3356,6 @@ name = "roc_builtins"
version = "0.0.1"
dependencies = [
"dunce",
"lazy_static",
"roc_collections",
"roc_module",
"roc_region",
@ -3826,7 +3825,6 @@ name = "roc_module"
version = "0.0.1"
dependencies = [
"bumpalo",
"lazy_static",
"roc_collections",
"roc_error_macros",
"roc_ident",
@ -4815,7 +4813,6 @@ dependencies = [
"bumpalo",
"indoc",
"insta",
"lazy_static",
"pretty_assertions",
"roc_builtins",
"roc_can",

View file

@ -12,7 +12,6 @@ roc_region = { path = "../region" }
roc_module = { path = "../module" }
roc_target = { path = "../roc_target" }
roc_utils = { path = "../../utils" }
lazy_static = "1.4.0"
[build-dependencies]
# dunce can be removed once ziglang/zig#5109 is fixed

View file

@ -69,6 +69,14 @@ impl std::fmt::Debug for SmallStringInterner {
}
impl SmallStringInterner {
pub const fn new() -> Self {
Self {
buffer: Vec::new(),
lengths: Vec::new(),
offsets: Vec::new(),
}
}
pub fn with_capacity(capacity: usize) -> Self {
Self {
// guess: the average symbol length is 5

View file

@ -6,14 +6,18 @@ pub struct VecMap<K, V> {
impl<K, V> Default for VecMap<K, V> {
fn default() -> Self {
Self::new()
}
}
impl<K, V> VecMap<K, V> {
pub const fn new() -> Self {
Self {
keys: Vec::new(),
values: Vec::new(),
}
}
}
impl<K, V> VecMap<K, V> {
pub fn len(&self) -> usize {
debug_assert_eq!(self.keys.len(), self.values.len());
self.keys.len()

View file

@ -12,7 +12,6 @@ roc_ident = { path = "../ident" }
roc_collections = { path = "../collections" }
roc_error_macros = {path = "../../error_macros"}
bumpalo = { version = "3.11.0", features = ["collections"] }
lazy_static = "1.4.0"
static_assertions = "1.1.0"
snafu = { version = "0.7.1", features = ["backtraces"] }

View file

@ -9,6 +9,3 @@ pub mod ident;
pub mod low_level;
pub mod module_err;
pub mod symbol;
#[macro_use]
extern crate lazy_static;

View file

@ -246,29 +246,13 @@ fn fallback_debug_fmt(symbol: Symbol, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "`{:?}.{:?}`", module_id, ident_id)
}
// TODO this is only here to prevent clippy from complaining about an unused
// #[macro_use] on lazy_statc in --release builds, because as of January 2020,
// we only use lazy_static in the debug configuration. If we ever start using
// lazy_static in release builds, this do-nothing macro invocation will be safe to delete!
//
// There's probably also a way to get clippy to stop complaining about the unused
// #[macro_use] but it didn't seem worth the effort since probably someday we'll
// end up using it in release builds anyway. Right? ...Right?
lazy_static! {}
#[cfg(any(debug_assertions, feature = "debug-symbols"))]
lazy_static! {
/// This is used in Debug builds only, to let us have a Debug instance
/// which displays not only the Module ID, but also the Module Name which
/// corresponds to that ID.
///
static ref DEBUG_MODULE_ID_NAMES: std::sync::Mutex<roc_collections::SmallStringInterner> =
// This stores a u32 key instead of a ModuleId key so that if there's
// a problem with ModuleId's Debug implementation, logging this for diagnostic
// purposes won't recursively trigger ModuleId's Debug instance in the course of printing
// this out.
std::sync::Mutex::new(roc_collections::SmallStringInterner::with_capacity(10));
}
#[cfg(any(debug_assertions, feature = "debug-symbols"))]
static DEBUG_MODULE_ID_NAMES: std::sync::Mutex<roc_collections::SmallStringInterner> =
std::sync::Mutex::new(roc_collections::SmallStringInterner::new());
#[derive(Debug, Default, Clone)]
pub struct Interns {
@ -338,18 +322,16 @@ pub fn get_module_ident_ids_mut<'a>(
})
}
#[cfg(any(debug_assertions, feature = "debug-symbols"))]
lazy_static! {
/// This is used in Debug builds only, to let us have a Debug instance
/// which displays not only the Module ID, but also the Module Name which
/// corresponds to that ID.
static ref DEBUG_IDENT_IDS_BY_MODULE_ID: std::sync::Mutex<roc_collections::VecMap<u32, IdentIds>> =
#[cfg(any(debug_assertions, feature = "debug-symbols"))]
static DEBUG_IDENT_IDS_BY_MODULE_ID: std::sync::Mutex<roc_collections::VecMap<u32, IdentIds>> =
// This stores a u32 key instead of a ModuleId key so that if there's
// a problem with ModuleId's Debug implementation, logging this for diagnostic
// purposes won't recursively trigger ModuleId's Debug instance in the course of printing
// this out.
std::sync::Mutex::new(roc_collections::VecMap::default());
}
std::sync::Mutex::new(roc_collections::VecMap::new());
/// A globally unique ID that gets assigned to each module as it is loaded.
#[derive(Copy, Clone, PartialEq, Eq, Hash)]

View file

@ -26,7 +26,6 @@ roc_region = { path = "../region" }
roc_solve = { path = "../solve" }
roc_debug_flags = { path = "../debug_flags" }
bumpalo = { version = "3.11.0", features = ["collections"] }
lazy_static = "1.4.0"
indoc = "1.0.7"
ven_pretty = { path = "../../vendor/pretty" }
pretty_assertions = "1.3.0"

View file

@ -23,9 +23,10 @@ thread_local! {
// Even if Cargo uses many threads, these tests won't go any faster. But that's fine, they're quick.
lazy_static! {
static ref COMPILER: Instance = init_compiler();
static ref TEST_MUTEX: Mutex<()> = Mutex::new(());
}
static TEST_MUTEX: Mutex<()> = Mutex::new(());
/// Load the compiler .wasm file and get it ready to execute
/// THIS FUNCTION TAKES 4 SECONDS TO RUN
fn init_compiler() -> Instance {