Unverified Commit c916c80b authored by Matt Mastracci's avatar Matt Mastracci Committed by GitHub

turbo-tasks: drop once_cell, const-init TRAIT_METHOD, ctor WORKER_TASKS (#93090)

## What

Clean up the Lazy-locks we re-export or use internally in the
turbo-tasks crates, migrating to `std::sync::LazyLock` /
`std::cell::LazyCell` where still needed and eliminating a couple of
them where they weren't.

## Why

One less dep, using standard Rust features, a smaller number of Lazy
items.

## Details

- **Re-exports:** drop unused `macro_helpers::OnceCell` and the
now-unused `macro_helpers::phf` (since `TraitType` no longer stores a
`phf::Map`). Replace `once_cell::sync::Lazy` with `std::sync::LazyLock`
in `macro_helpers::TRAIT_CAST_FNS` and in the two macro-generated call
sites.
- **`once_cell::sync::Lazy` → `std::sync::LazyLock`** in `scope.rs`,
`registry/mod.rs`, and `turbo-tasks-backend-fuzz`'s `graph.rs`.
- **`once_cell::unsync::Lazy` → `std::cell::LazyCell`** in
`update_cell.rs`.
- **`TRAIT_METHOD` → plain const-init static:** `TraitType::methods`
changes from `phf::Map<&'static str, TraitMethod>` to `&'static
[TraitMethod]` (parallel to `method_names`), and `TraitType::get`
becomes a `const fn` that does `index_of_name` + array indexing. The
`value_trait` macro now emits a slice literal instead of `phf_map!`, and
the `function` macro emits `static TRAIT_METHOD: &'static TraitMethod =
TRAIT.get(stringify!(..));` — evaluated at const-eval time rather than
behind a `LazyLock<&TraitMethod>`.
- **Dropped `once_cell` and `phf` Cargo deps** from `turbo-tasks`, and
`once_cell` from `turbo-tasks-backend` and its fuzz crate.
parent a430936c
...@@ -9900,9 +9900,7 @@ dependencies = [ ...@@ -9900,9 +9900,7 @@ dependencies = [
"futures", "futures",
"indexmap 2.13.0", "indexmap 2.13.0",
"inventory", "inventory",
"once_cell",
"parking_lot", "parking_lot",
"phf",
"pin-project-lite", "pin-project-lite",
"rayon", "rayon",
"regex", "regex",
...@@ -9942,7 +9940,6 @@ dependencies = [ ...@@ -9942,7 +9940,6 @@ dependencies = [
"indexmap 2.13.0", "indexmap 2.13.0",
"indoc", "indoc",
"lzzzz", "lzzzz",
"once_cell",
"parking_lot", "parking_lot",
"rand 0.10.0", "rand 0.10.0",
"regex", "regex",
...@@ -9975,7 +9972,6 @@ dependencies = [ ...@@ -9975,7 +9972,6 @@ dependencies = [
"arbitrary", "arbitrary",
"bincode 2.0.1", "bincode 2.0.1",
"libfuzzer-sys", "libfuzzer-sys",
"once_cell",
"serde", "serde",
"tokio", "tokio",
"turbo-tasks", "turbo-tasks",
......
...@@ -44,7 +44,6 @@ either = { workspace = true } ...@@ -44,7 +44,6 @@ either = { workspace = true }
hashbrown = { workspace = true, features = ["raw"] } hashbrown = { workspace = true, features = ["raw"] }
indexmap = { workspace = true } indexmap = { workspace = true }
lzzzz = { workspace = true, optional = true } lzzzz = { workspace = true, optional = true }
once_cell = { workspace = true }
parking_lot = { workspace = true } parking_lot = { workspace = true }
rand = { workspace = true } rand = { workspace = true }
ringmap = { workspace = true, features = ["serde"] } ringmap = { workspace = true, features = ["serde"] }
......
...@@ -13,7 +13,6 @@ anyhow = { workspace = true } ...@@ -13,7 +13,6 @@ anyhow = { workspace = true }
arbitrary = { version = "1.4.1", features = ["derive"] } arbitrary = { version = "1.4.1", features = ["derive"] }
bincode = { workspace = true } bincode = { workspace = true }
libfuzzer-sys = "0.4.9" libfuzzer-sys = "0.4.9"
once_cell = { workspace = true }
serde = { workspace = true } serde = { workspace = true }
tokio = { workspace = true, features = ["full"] } tokio = { workspace = true, features = ["full"] }
turbo-tasks = { workspace = true, features = ["non_operation_vc_strongly_consistent"] } turbo-tasks = { workspace = true, features = ["non_operation_vc_strongly_consistent"] }
......
use std::sync::Arc; use std::sync::{Arc, LazyLock};
use anyhow::Result; use anyhow::Result;
use arbitrary::Arbitrary; use arbitrary::Arbitrary;
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use once_cell::sync::Lazy;
use turbo_tasks::{self, NonLocalValue, State, TaskInput, TurboTasks, Vc, trace::TraceRawVcs}; use turbo_tasks::{self, NonLocalValue, State, TaskInput, TurboTasks, Vc, trace::TraceRawVcs};
use turbo_tasks_malloc::TurboMalloc; use turbo_tasks_malloc::TurboMalloc;
...@@ -81,7 +80,7 @@ impl<'a> Iterator for Iter<'a> { ...@@ -81,7 +80,7 @@ impl<'a> Iterator for Iter<'a> {
} }
} }
static RUNTIME: Lazy<tokio::runtime::Runtime> = Lazy::new(|| { static RUNTIME: LazyLock<tokio::runtime::Runtime> = LazyLock::new(|| {
tokio::runtime::Builder::new_multi_thread() tokio::runtime::Builder::new_multi_thread()
.enable_all() .enable_all()
.on_thread_stop(|| { .on_thread_stop(|| {
......
use std::mem::take; use std::{cell::LazyCell, mem::take};
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use once_cell::unsync::Lazy;
use rustc_hash::FxHashSet; use rustc_hash::FxHashSet;
use smallvec::SmallVec; use smallvec::SmallVec;
use turbo_tasks::{ use turbo_tasks::{
...@@ -131,7 +130,7 @@ impl UpdateCellOperation { ...@@ -131,7 +130,7 @@ impl UpdateCellOperation {
#[cfg(feature = "trace_task_dirty")] #[cfg(feature = "trace_task_dirty")]
let has_updated_key_hashes = updated_key_hashes.is_some(); let has_updated_key_hashes = updated_key_hashes.is_some();
let updated_key_hashes_set = updated_key_hashes.map(|updated_key_hashes| { let updated_key_hashes_set = updated_key_hashes.map(|updated_key_hashes| {
Lazy::new(|| updated_key_hashes.into_iter().collect::<FxHashSet<u64>>()) LazyCell::new(|| updated_key_hashes.into_iter().collect::<FxHashSet<u64>>())
}); });
// Collect dependent tasks only when not skipping invalidation. // Collect dependent tasks only when not skipping invalidation.
......
...@@ -559,11 +559,14 @@ impl TurboFn<'_> { ...@@ -559,11 +559,14 @@ impl TurboFn<'_> {
let this = #converted_this; let this = #converted_this;
let persistence = #persistence; let persistence = #persistence;
let mut arg = turbo_tasks::StackDynTaskInputsSlot::new(inputs); let mut arg = turbo_tasks::StackDynTaskInputsSlot::new(inputs);
static TRAIT_METHOD: turbo_tasks::macro_helpers::Lazy<&'static turbo_tasks::TraitMethod> = static TRAIT_METHOD: &'static turbo_tasks::TraitMethod = &#trait_type_ident
turbo_tasks::macro_helpers::Lazy::new(|| #trait_type_ident.get(stringify!(#ident))); .methods[turbo_tasks::macro_helpers::index_of_method_name(
#trait_type_ident.methods,
stringify!(#ident),
)];
<#output as turbo_tasks::task::TaskOutput>::try_from_raw_vc( <#output as turbo_tasks::task::TaskOutput>::try_from_raw_vc(
turbo_tasks::trait_call( turbo_tasks::trait_call(
*TRAIT_METHOD, TRAIT_METHOD,
this, this,
&mut arg, &mut arg,
persistence, persistence,
......
...@@ -69,7 +69,6 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -69,7 +69,6 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
let trait_type_ident = get_trait_type_ident(trait_ident); let trait_type_ident = get_trait_type_ident(trait_ident);
let mut dynamic_trait_fns = Vec::new(); let mut dynamic_trait_fns = Vec::new();
let mut trait_methods: Vec<TokenStream2> = Vec::new(); let mut trait_methods: Vec<TokenStream2> = Vec::new();
let mut method_names: Vec<TokenStream2> = Vec::new();
let mut default_methods: Vec<TokenStream2> = Vec::new(); let mut default_methods: Vec<TokenStream2> = Vec::new();
let mut native_functions = Vec::new(); let mut native_functions = Vec::new();
let mut items: Vec<TokenStream2> = Vec::with_capacity(raw_items.len()); let mut items: Vec<TokenStream2> = Vec::with_capacity(raw_items.len());
...@@ -223,7 +222,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -223,7 +222,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
let method_name_str = syn::LitStr::new(&ident.to_string(), ident.span()); let method_name_str = syn::LitStr::new(&ident.to_string(), ident.span());
let index = trait_methods.len() as u8; let index = trait_methods.len() as u8;
trait_methods.push(quote! { trait_methods.push(quote! {
#method_name_str => turbo_tasks::TraitMethod { turbo_tasks::TraitMethod {
trait_type: &#trait_type_ident, trait_type: &#trait_type_ident,
trait_name: stringify!(#trait_ident), trait_name: stringify!(#trait_ident),
method_name: #method_name_str, method_name: #method_name_str,
...@@ -231,7 +230,6 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -231,7 +230,6 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
index: #index, index: #index,
}, },
}); });
method_names.push(quote! { #method_name_str });
default_methods.push(quote! { Some(&#native_function_ident) }); default_methods.push(quote! { Some(&#native_function_ident) });
native_functions.push(quote! { native_functions.push(quote! {
...@@ -261,7 +259,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -261,7 +259,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
let method_name_str = syn::LitStr::new(&ident.to_string(), ident.span()); let method_name_str = syn::LitStr::new(&ident.to_string(), ident.span());
let index = trait_methods.len() as u8; let index = trait_methods.len() as u8;
trait_methods.push(quote! { trait_methods.push(quote! {
#method_name_str => turbo_tasks::TraitMethod { turbo_tasks::TraitMethod {
trait_type: &#trait_type_ident, trait_type: &#trait_type_ident,
trait_name: stringify!(#trait_ident), trait_name: stringify!(#trait_ident),
method_name: #method_name_str, method_name: #method_name_str,
...@@ -269,7 +267,6 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -269,7 +267,6 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
index: #index, index: #index,
}, },
}); });
method_names.push(quote! { #method_name_str });
default_methods.push(quote! { None }); default_methods.push(quote! { None });
quote! { ; } quote! { ; }
}; };
...@@ -311,7 +308,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -311,7 +308,7 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
extended_supertraits.push(quote!(turbo_tasks::debug::ValueDebug)); extended_supertraits.push(quote!(turbo_tasks::debug::ValueDebug));
} }
let num_methods = method_names.len(); let num_methods = default_methods.len();
let trait_name = global_name_for_type(quote! { dyn #trait_ident }); let trait_name = global_name_for_type(quote! { dyn #trait_ident });
let expanded = quote! { let expanded = quote! {
#[must_use] #[must_use]
...@@ -324,23 +321,17 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -324,23 +321,17 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
#(#native_functions)* #(#native_functions)*
turbo_tasks::macro_helpers::turbo_register!( turbo_tasks::macro_helpers::turbo_register!(
Box<dyn #trait_ident> => #trait_type_ident: turbo_tasks::TraitType = { Box<dyn #trait_ident> => #trait_type_ident: turbo_tasks::TraitType =
use turbo_tasks::macro_helpers::{phf, phf::phf_map};
turbo_tasks::TraitType::new::<&'static dyn #trait_ident>( turbo_tasks::TraitType::new::<&'static dyn #trait_ident>(
stringify!(#trait_ident), stringify!(#trait_ident),
#trait_name, #trait_name,
phf_map! { &[#(#trait_methods)*],
#(#trait_methods)*
},
&[#(#method_names),*],
&[#(#default_methods),*] &[#(#default_methods),*]
) )
}
); );
impl turbo_tasks::macro_helpers::TraitVtablePrototype for Box<dyn #trait_ident> { impl turbo_tasks::macro_helpers::TraitVtablePrototype for Box<dyn #trait_ident> {
const LEN: usize = #num_methods; const LEN: usize = #num_methods;
const NAMES: &[&str] = &[#(#method_names),*];
const DEFAULTS: &[Option<&turbo_tasks::macro_helpers::NativeFunction>] = &[#(#default_methods),*]; const DEFAULTS: &[Option<&turbo_tasks::macro_helpers::NativeFunction>] = &[#(#default_methods),*];
} }
...@@ -354,8 +345,8 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -354,8 +345,8 @@ pub fn value_trait(args: TokenStream, input: TokenStream) -> TokenStream {
// TODO: Remove this Lazy VTableRegistry once trait resolution is fully migrated // TODO: Remove this Lazy VTableRegistry once trait resolution is fully migrated
fn get_impl_vtables() -> &'static turbo_tasks::macro_helpers::VTableRegistry<Self::ValueTrait> { fn get_impl_vtables() -> &'static turbo_tasks::macro_helpers::VTableRegistry<Self::ValueTrait> {
static registry: turbo_tasks::macro_helpers::Lazy<turbo_tasks::macro_helpers::VTableRegistry<dyn # trait_ident>> = static registry: ::std::sync::LazyLock<turbo_tasks::macro_helpers::VTableRegistry<dyn # trait_ident>> =
turbo_tasks::macro_helpers::Lazy::new(|| turbo_tasks::macro_helpers::VTableRegistry::new(turbo_tasks::registry::get_trait_type_id(&#trait_type_ident))); ::std::sync::LazyLock::new(|| turbo_tasks::macro_helpers::VTableRegistry::new(turbo_tasks::registry::get_trait_type_id(&#trait_type_ident)));
&*registry &*registry
} }
......
...@@ -35,9 +35,7 @@ event-listener = "5.4.0" ...@@ -35,9 +35,7 @@ event-listener = "5.4.0"
futures = { workspace = true } futures = { workspace = true }
indexmap = { workspace = true, features = ["serde"] } indexmap = { workspace = true, features = ["serde"] }
inventory = { workspace = true } inventory = { workspace = true }
once_cell = { workspace = true }
parking_lot = { workspace = true, features = ["serde"]} parking_lot = { workspace = true, features = ["serde"]}
phf = { workspace = true }
pin-project-lite = { workspace = true } pin-project-lite = { workspace = true }
rayon = { workspace = true } rayon = { workspace = true }
regex = { workspace = true } regex = { workspace = true }
......
//! Runtime helpers for [turbo-tasks-macro]. //! Runtime helpers for [turbo-tasks-macro].
use std::sync::LazyLock;
pub use async_trait::async_trait; pub use async_trait::async_trait;
pub use bincode; pub use bincode;
pub use inventory; pub use inventory;
pub use once_cell::sync::{Lazy, OnceCell};
pub use phf;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
pub use shrink_to_fit; pub use shrink_to_fit;
pub use tracing; pub use tracing;
...@@ -27,7 +27,7 @@ pub use crate::{ ...@@ -27,7 +27,7 @@ pub use crate::{
registry::RegistryDef, registry::RegistryDef,
task::function::{into_task_fn, into_task_fn_with_this}, task::function::{into_task_fn, into_task_fn_with_this},
turbo_register, turbo_register,
value_type::{TraitVtablePrototype, build_trait_vtable}, value_type::{TraitVtablePrototype, build_trait_vtable, index_of_method_name},
}; };
#[cfg(debug_assertions)] #[cfg(debug_assertions)]
...@@ -209,18 +209,19 @@ unsafe impl Sync for RawPtr {} ...@@ -209,18 +209,19 @@ unsafe impl Sync for RawPtr {}
unsafe impl Send for RawPtr {} unsafe impl Send for RawPtr {}
// Accumulate all trait impls by trait id // Accumulate all trait impls by trait id
static TRAIT_CAST_FNS: Lazy<FxDashMap<TraitTypeId, Vec<(ValueTypeId, RawPtr)>>> = Lazy::new(|| { static TRAIT_CAST_FNS: LazyLock<FxDashMap<TraitTypeId, Vec<(ValueTypeId, RawPtr)>>> =
let map: FxDashMap<TraitTypeId, Vec<(ValueTypeId, RawPtr)>> = FxDashMap::default(); LazyLock::new(|| {
for CollectableTraitCastFunctions(trait_id_fn, value_id_fn, cast_fn) in let map: FxDashMap<TraitTypeId, Vec<(ValueTypeId, RawPtr)>> = FxDashMap::default();
inventory::iter::<CollectableTraitCastFunctions> for CollectableTraitCastFunctions(trait_id_fn, value_id_fn, cast_fn) in
{ inventory::iter::<CollectableTraitCastFunctions>
map.entry(trait_id_fn()) {
.or_default() map.entry(trait_id_fn())
.value_mut() .or_default()
.push((value_id_fn(), RawPtr(*cast_fn))); .value_mut()
} .push((value_id_fn(), RawPtr(*cast_fn)));
map }
}); map
});
// Holds a raw pointer to a function that can perform a fat pointer cast // Holds a raw pointer to a function that can perform a fat pointer cast
pub struct CollectableTraitCastFunctions( pub struct CollectableTraitCastFunctions(
......
use std::{cell::SyncUnsafeCell, num::NonZeroU16}; use std::{cell::SyncUnsafeCell, num::NonZeroU16, sync::LazyLock};
use anyhow::Error; use anyhow::Error;
use once_cell::sync::Lazy;
use crate::{ use crate::{
TraitType, ValueType, TraitType, ValueType,
...@@ -134,16 +133,16 @@ fn init_registry<T: Registerable>(mut items: Vec<&'static T>) -> Box<[&'static T ...@@ -134,16 +133,16 @@ fn init_registry<T: Registerable>(mut items: Vec<&'static T>) -> Box<[&'static T
/// Get an item by its ID from a registry slice /// Get an item by its ID from a registry slice
#[inline] #[inline]
fn get_item<T: Registerable>(registry: &Lazy<Box<[&'static T]>>, id: T::Id) -> &'static T { fn get_item<T: Registerable>(registry: &LazyLock<Box<[&'static T]>>, id: T::Id) -> &'static T {
registry[*id as usize - 1] registry[*id as usize - 1]
} }
/// Get the ID for a registered item. Forces registry init if needed, which /// Get the ID for a registered item. Forces registry init if needed, which
/// assigns IDs to all items as a side effect. /// assigns IDs to all items as a side effect.
#[inline] #[inline]
fn get_id<T: Registerable>(registry: &Lazy<Box<[&'static T]>>, item: &'static T) -> T::Id { fn get_id<T: Registerable>(registry: &LazyLock<Box<[&'static T]>>, item: &'static T) -> T::Id {
Lazy::force(registry); LazyLock::force(registry);
// SAFETY: The ID write happens-before this read thanks to the fence inside of Lazy // SAFETY: The ID write happens-before this read thanks to the fence inside of LazyLock
let n = unsafe { std::ptr::read(item.ty().id.get()) }; let n = unsafe { std::ptr::read(item.ty().id.get()) };
let Some(id) = NonZeroU16::new(n) else { let Some(id) = NonZeroU16::new(n) else {
panic!( panic!(
...@@ -156,7 +155,10 @@ fn get_id<T: Registerable>(registry: &Lazy<Box<[&'static T]>>, item: &'static T) ...@@ -156,7 +155,10 @@ fn get_id<T: Registerable>(registry: &Lazy<Box<[&'static T]>>, item: &'static T)
} }
/// Validate that an ID is within the valid range /// Validate that an ID is within the valid range
fn validate_id<T: Registerable>(registry: &Lazy<Box<[&'static T]>>, id: T::Id) -> Option<Error> { fn validate_id<T: Registerable>(
registry: &LazyLock<Box<[&'static T]>>,
id: T::Id,
) -> Option<Error> {
let len = registry.len(); let len = registry.len();
if *id as usize <= len { if *id as usize <= len {
None None
...@@ -168,7 +170,7 @@ fn validate_id<T: Registerable>(registry: &Lazy<Box<[&'static T]>>, id: T::Id) - ...@@ -168,7 +170,7 @@ fn validate_id<T: Registerable>(registry: &Lazy<Box<[&'static T]>>, id: T::Id) -
} }
} }
static FUNCTIONS: Lazy<Box<[&'static NativeFunction]>> = Lazy::new(|| { static FUNCTIONS: LazyLock<Box<[&'static NativeFunction]>> = LazyLock::new(|| {
init_registry( init_registry(
inventory::iter::<&'static NativeFunction> inventory::iter::<&'static NativeFunction>
.into_iter() .into_iter()
...@@ -191,7 +193,7 @@ pub fn validate_function_id(id: FunctionId) -> Option<Error> { ...@@ -191,7 +193,7 @@ pub fn validate_function_id(id: FunctionId) -> Option<Error> {
validate_id(&FUNCTIONS, id) validate_id(&FUNCTIONS, id)
} }
pub(crate) static VALUES: Lazy<Box<[&'static ValueType]>> = Lazy::new(|| { pub(crate) static VALUES: LazyLock<Box<[&'static ValueType]>> = LazyLock::new(|| {
let items = init_registry( let items = init_registry(
inventory::iter::<&'static ValueType> inventory::iter::<&'static ValueType>
.into_iter() .into_iter()
...@@ -222,7 +224,7 @@ pub(crate) fn trait_type_count() -> usize { ...@@ -222,7 +224,7 @@ pub(crate) fn trait_type_count() -> usize {
TRAITS.len() TRAITS.len()
} }
static TRAITS: Lazy<Box<[&'static TraitType]>> = Lazy::new(|| { static TRAITS: LazyLock<Box<[&'static TraitType]>> = LazyLock::new(|| {
init_registry( init_registry(
inventory::iter::<&'static TraitType> inventory::iter::<&'static TraitType>
.into_iter() .into_iter()
......
...@@ -6,14 +6,13 @@ use std::{ ...@@ -6,14 +6,13 @@ use std::{
marker::PhantomData, marker::PhantomData,
panic::{self, AssertUnwindSafe, catch_unwind}, panic::{self, AssertUnwindSafe, catch_unwind},
sync::{ sync::{
Arc, Arc, LazyLock,
atomic::{AtomicUsize, Ordering}, atomic::{AtomicUsize, Ordering},
}, },
thread::{self, Thread, available_parallelism}, thread::{self, Thread, available_parallelism},
time::{Duration, Instant}, time::{Duration, Instant},
}; };
use once_cell::sync::Lazy;
use parking_lot::{Condvar, Mutex}; use parking_lot::{Condvar, Mutex};
use tokio::{runtime::Handle, task::block_in_place}; use tokio::{runtime::Handle, task::block_in_place};
use tracing::{Span, info_span}; use tracing::{Span, info_span};
...@@ -22,7 +21,8 @@ use crate::{TurboTasksApi, manager::try_turbo_tasks, turbo_tasks_scope}; ...@@ -22,7 +21,8 @@ use crate::{TurboTasksApi, manager::try_turbo_tasks, turbo_tasks_scope};
/// Number of worker tasks to spawn that process jobs. It's 1 less than the number of cpus as we /// Number of worker tasks to spawn that process jobs. It's 1 less than the number of cpus as we
/// also use the current task as worker. /// also use the current task as worker.
static WORKER_TASKS: Lazy<usize> = Lazy::new(|| available_parallelism().map_or(0, |n| n.get() - 1)); static WORKER_TASKS: LazyLock<usize> =
LazyLock::new(|| available_parallelism().map_or(0, |n| n.get() - 1));
enum WorkQueueJob { enum WorkQueueJob {
Job(usize, Box<dyn FnOnce() + Send + 'static>), Job(usize, Box<dyn FnOnce() + Send + 'static>),
......
...@@ -255,8 +255,7 @@ impl TraitMethod { ...@@ -255,8 +255,7 @@ impl TraitMethod {
pub struct TraitType { pub struct TraitType {
pub ty: RegistryType, pub ty: RegistryType,
pub methods: phf::Map<&'static str, TraitMethod>, pub methods: &'static [TraitMethod],
pub method_names: &'static [&'static str],
pub default_methods: &'static [Option<&'static NativeFunction>], pub default_methods: &'static [Option<&'static NativeFunction>],
} }
...@@ -264,8 +263,8 @@ impl Debug for TraitType { ...@@ -264,8 +263,8 @@ impl Debug for TraitType {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
let mut d = f.debug_struct("TraitType"); let mut d = f.debug_struct("TraitType");
d.field("name", &self.ty.name); d.field("name", &self.ty.name);
for (name, method) in self.methods.entries() { for method in self.methods.iter() {
d.field(name, method); d.field(method.method_name, method);
} }
d.finish() d.finish()
} }
...@@ -281,20 +280,22 @@ impl TraitType { ...@@ -281,20 +280,22 @@ impl TraitType {
pub const fn new<T: 'static>( pub const fn new<T: 'static>(
name: &'static str, name: &'static str,
global_name: &'static str, global_name: &'static str,
methods: phf::Map<&'static str, TraitMethod>, methods: &'static [TraitMethod],
method_names: &'static [&'static str],
default_methods: &'static [Option<&'static NativeFunction>], default_methods: &'static [Option<&'static NativeFunction>],
) -> Self { ) -> Self {
Self { Self {
ty: RegistryType::new::<T>(name, global_name), ty: RegistryType::new::<T>(name, global_name),
methods, methods,
method_names,
default_methods, default_methods,
} }
} }
#[cfg(test)]
pub fn get(&self, name: &str) -> &TraitMethod { pub fn get(&self, name: &str) -> &TraitMethod {
self.methods.get(name).unwrap() self.methods
.iter()
.find(|method| method.method_name == name)
.expect("Method not found!")
} }
} }
...@@ -302,17 +303,20 @@ turbo_registry!("Trait", TraitType); ...@@ -302,17 +303,20 @@ turbo_registry!("Trait", TraitType);
pub trait TraitVtablePrototype { pub trait TraitVtablePrototype {
const LEN: usize; const LEN: usize;
const NAMES: &'static [&'static str];
const DEFAULTS: &'static [Option<&'static NativeFunction>]; const DEFAULTS: &'static [Option<&'static NativeFunction>];
} }
pub(crate) const fn index_of_name(array: &'static [&'static str], name: &'static str) -> usize { /// Linear-scan lookup of a [`TraitMethod`] by `method_name` in a `&'static [TraitMethod]`. Const
/// so the `value_trait` macro's per-method dispatch site can resolve to an array index at
/// compile time.
pub const fn index_of_method_name(methods: &'static [TraitMethod], name: &'static str) -> usize {
let mut i = 0; let mut i = 0;
'outer: while i < array.len() { 'outer: while i < methods.len() {
if array[i].len() == name.len() { let entry = methods[i].method_name;
if entry.len() == name.len() {
let mut j = 0; let mut j = 0;
while j < name.len() { while j < name.len() {
if array[i].as_bytes()[j] != name.as_bytes()[j] { if entry.as_bytes()[j] != name.as_bytes()[j] {
i += 1; i += 1;
continue 'outer; continue 'outer;
} }
...@@ -325,7 +329,10 @@ pub(crate) const fn index_of_name(array: &'static [&'static str], name: &'static ...@@ -325,7 +329,10 @@ pub(crate) const fn index_of_name(array: &'static [&'static str], name: &'static
panic!("Method not found!") panic!("Method not found!")
} }
pub const fn build_trait_vtable<B: TraitVtablePrototype, const LEN: usize>( pub const fn build_trait_vtable<
B: TraitVtablePrototype + crate::registry::RegistryDef<TraitType>,
const LEN: usize,
>(
overrides: &[(&'static str, &'static NativeFunction)], overrides: &[(&'static str, &'static NativeFunction)],
) -> [&'static NativeFunction; LEN] { ) -> [&'static NativeFunction; LEN] {
let mut methods = [&crate::native_function::VTABLE_DEFAULT; LEN]; let mut methods = [&crate::native_function::VTABLE_DEFAULT; LEN];
...@@ -340,7 +347,10 @@ pub const fn build_trait_vtable<B: TraitVtablePrototype, const LEN: usize>( ...@@ -340,7 +347,10 @@ pub const fn build_trait_vtable<B: TraitVtablePrototype, const LEN: usize>(
let mut i = 0; let mut i = 0;
while i < overrides.len() { while i < overrides.len() {
let (name, f) = overrides[i]; let (name, f) = overrides[i];
methods[index_of_name(B::NAMES, name)] = f; methods[index_of_method_name(
<B as crate::registry::RegistryDef<TraitType>>::DEF.methods,
name,
)] = f;
i += 1; i += 1;
} }
methods methods
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment