Unverified Commit 0d84c976 authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] Delete `FunctionId` (#80809)

## Delete the `FunctionId` type

This is only used to map to/from global names,  we can just do that directly and remove some indirection.

This is a tiny cleanup following https://github.com/vercel/next.js/pull/80695 which made functionids irrelevant to most  of the runtime.
parent 5802ba81
...@@ -213,9 +213,7 @@ mod ser { ...@@ -213,9 +213,7 @@ mod ser {
unreachable!(); unreachable!();
}; };
let mut state = serializer.serialize_seq(Some(2))?; let mut state = serializer.serialize_seq(Some(2))?;
// It would be a little tricky but we _could_ store the function id on the native_fn so state.serialize_element(&registry::get_function_global_name(native_fn))?;
// that instead of a hashmap lookup this would be a memory access on the NativeFn
state.serialize_element(&registry::get_function_id(native_fn))?;
let arg = *arg; let arg = *arg;
let arg = native_fn.arg_meta.as_serialize(arg); let arg = native_fn.arg_meta.as_serialize(arg);
state.serialize_element(arg)?; state.serialize_element(arg)?;
...@@ -237,10 +235,10 @@ mod ser { ...@@ -237,10 +235,10 @@ mod ser {
where where
A: serde::de::SeqAccess<'de>, A: serde::de::SeqAccess<'de>,
{ {
let fn_id = seq let fn_name = seq
.next_element()? .next_element()?
.ok_or_else(|| serde::de::Error::invalid_length(0, &self))?; .ok_or_else(|| serde::de::Error::invalid_length(0, &self))?;
let native_fn = registry::get_function(fn_id); let native_fn = registry::get_function_by_global_name(fn_name);
let seed = native_fn.arg_meta.deserialization_seed(); let seed = native_fn.arg_meta.deserialization_seed();
let arg = seq let arg = seq
.next_element_seed(seed)? .next_element_seed(seed)?
......
...@@ -113,7 +113,6 @@ macro_rules! define_id { ...@@ -113,7 +113,6 @@ macro_rules! define_id {
} }
define_id!(TaskId: u32, derive(Serialize, Deserialize), serde(transparent)); define_id!(TaskId: u32, derive(Serialize, Deserialize), serde(transparent));
define_id!(FunctionId: u32);
define_id!(ValueTypeId: u32); define_id!(ValueTypeId: u32);
define_id!(TraitTypeId: u32); define_id!(TraitTypeId: u32);
define_id!(BackendJobId: u32); define_id!(BackendJobId: u32);
...@@ -202,12 +201,6 @@ macro_rules! make_serializable { ...@@ -202,12 +201,6 @@ macro_rules! make_serializable {
}; };
} }
make_serializable!(
FunctionId,
registry::get_function_global_name,
registry::get_function_id_by_global_name,
FunctionIdVisitor
);
make_serializable!( make_serializable!(
ValueTypeId, ValueTypeId,
registry::get_value_type_global_name, registry::get_value_type_global_name,
......
...@@ -94,8 +94,7 @@ pub use completion::{Completion, Completions}; ...@@ -94,8 +94,7 @@ pub use completion::{Completion, Completions};
pub use display::ValueToString; pub use display::ValueToString;
pub use effect::{ApplyEffectsContext, Effects, apply_effects, effect, get_effects}; pub use effect::{ApplyEffectsContext, Effects, apply_effects, effect, get_effects};
pub use id::{ pub use id::{
ExecutionId, FunctionId, LocalTaskId, SessionId, TRANSIENT_TASK_BIT, TaskId, TraitTypeId, ExecutionId, LocalTaskId, SessionId, TRANSIENT_TASK_BIT, TaskId, TraitTypeId, ValueTypeId,
ValueTypeId,
}; };
pub use invalidation::{ pub use invalidation::{
DynamicEqHash, InvalidationReason, InvalidationReasonKind, InvalidationReasonSet, Invalidator, DynamicEqHash, InvalidationReason, InvalidationReasonKind, InvalidationReasonSet, Invalidator,
......
use std::{fmt::Debug, hash::Hash, num::NonZeroU64, ops::Deref}; use std::{fmt::Debug, hash::Hash, num::NonZeroU64, ops::Deref, sync::RwLock};
use dashmap::mapref::entry::Entry; use dashmap::mapref::entry::Entry;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use rustc_hash::FxHashMap;
use crate::{ use crate::{
FxDashMap, TraitType, ValueType, FxDashMap, TraitType, ValueType,
id::{FunctionId, TraitTypeId, ValueTypeId}, id::{TraitTypeId, ValueTypeId},
id_factory::IdFactory, id_factory::IdFactory,
native_function::NativeFunction, native_function::NativeFunction,
no_move_vec::NoMoveVec, no_move_vec::NoMoveVec,
}; };
static FUNCTION_ID_FACTORY: IdFactory<FunctionId> = IdFactory::new_const( static NAME_TO_FUNCTION: Lazy<RwLock<FxHashMap<&'static str, &'static NativeFunction>>> =
FunctionId::MIN.to_non_zero_u64(), Lazy::new(RwLock::default);
FunctionId::MAX.to_non_zero_u64(), static FUNCTION_TO_NAME: Lazy<RwLock<FxHashMap<&'static NativeFunction, &'static str>>> =
); Lazy::new(RwLock::default);
static FUNCTIONS_BY_NAME: Lazy<FxDashMap<&'static str, FunctionId>> = Lazy::new(FxDashMap::default);
static FUNCTIONS_BY_VALUE: Lazy<FxDashMap<&'static NativeFunction, FunctionId>> =
Lazy::new(FxDashMap::default);
static FUNCTIONS: Lazy<NoMoveVec<(&'static NativeFunction, &'static str)>> =
Lazy::new(NoMoveVec::new);
static VALUE_TYPE_ID_FACTORY: IdFactory<ValueTypeId> = IdFactory::new_const( static VALUE_TYPE_ID_FACTORY: IdFactory<ValueTypeId> = IdFactory::new_const(
ValueTypeId::MIN.to_non_zero_u64(), ValueTypeId::MIN.to_non_zero_u64(),
...@@ -80,31 +76,23 @@ where ...@@ -80,31 +76,23 @@ where
} }
} }
/// Registers a function so it is available for persistence
pub fn register_function(global_name: &'static str, func: &'static NativeFunction) { pub fn register_function(global_name: &'static str, func: &'static NativeFunction) {
register_thing( let prev = FUNCTION_TO_NAME.write().unwrap().insert(func, global_name);
global_name, debug_assert!(prev.is_none(), "function {global_name} registered twice?");
func, let prev = NAME_TO_FUNCTION.write().unwrap().insert(global_name, func);
&FUNCTION_ID_FACTORY, debug_assert!(
&FUNCTIONS, prev.is_none(),
&FUNCTIONS_BY_NAME, "registration mappings for {global_name} are inconsistent!"
&FUNCTIONS_BY_VALUE,
); );
} }
pub fn get_function_id(func: &'static NativeFunction) -> FunctionId { pub fn get_function_by_global_name(global_name: &str) -> &'static NativeFunction {
get_thing_id(func, &FUNCTIONS_BY_VALUE) NAME_TO_FUNCTION.read().unwrap().get(global_name).unwrap()
}
pub fn get_function_id_by_global_name(global_name: &str) -> Option<FunctionId> {
FUNCTIONS_BY_NAME.get(global_name).map(|x| *x)
}
pub fn get_function(id: FunctionId) -> &'static NativeFunction {
FUNCTIONS.get(*id as usize).unwrap().0
} }
pub fn get_function_global_name(id: FunctionId) -> &'static str { pub fn get_function_global_name(func: &'static NativeFunction) -> &'static str {
FUNCTIONS.get(*id as usize).unwrap().1 FUNCTION_TO_NAME.read().unwrap().get(&func).unwrap()
} }
pub fn register_value_type( pub fn register_value_type(
......
...@@ -73,7 +73,7 @@ impl Serialize for TaskStatistics { ...@@ -73,7 +73,7 @@ impl Serialize for TaskStatistics {
{ {
let mut map = serializer.serialize_map(Some(self.inner.len()))?; let mut map = serializer.serialize_map(Some(self.inner.len()))?;
for entry in &self.inner { for entry in &self.inner {
let key = registry::get_function_global_name(registry::get_function_id(entry.key())); let key = registry::get_function_global_name(entry.key());
map.serialize_entry(key, entry.value())?; map.serialize_entry(key, entry.value())?;
} }
map.end() map.end()
......
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