Skip to content

Commit 0b2a7f5

Browse files
committed
add function ids back for functions to support persistence
1 parent a1cb4f6 commit 0b2a7f5

File tree

3 files changed

+56
-30
lines changed

3 files changed

+56
-30
lines changed

turbopack/crates/turbo-tasks/src/backend.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ mod ser {
210210
unreachable!();
211211
};
212212
let mut state = serializer.serialize_seq(Some(2))?;
213-
state.serialize_element(native_fn.global_name)?;
213+
state.serialize_element(&registry::get_function_id(native_fn))?;
214214
let arg = *arg;
215215
let arg = native_fn.arg_meta.as_serialize(arg);
216216
state.serialize_element(arg)?;
@@ -232,10 +232,10 @@ mod ser {
232232
where
233233
A: serde::de::SeqAccess<'de>,
234234
{
235-
let fn_name = seq
235+
let fn_id = seq
236236
.next_element()?
237237
.ok_or_else(|| serde::de::Error::invalid_length(0, &self))?;
238-
let native_fn = registry::get_function_by_global_name(fn_name);
238+
let native_fn = registry::get_native_function(fn_id);
239239
let seed = native_fn.arg_meta.deserialization_seed();
240240
let arg = seq
241241
.next_element_seed(seed)?

turbopack/crates/turbo-tasks/src/id.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ macro_rules! define_id {
120120
}
121121

122122
define_id!(TaskId: u32, derive(Serialize, Deserialize), serde(transparent));
123+
define_id!(FunctionId: u32);
123124
define_id!(ValueTypeId: u32);
124125
define_id!(TraitTypeId: u32);
125126
define_id!(SessionId: u32, derive(Debug, Serialize, Deserialize), serde(transparent));
@@ -210,3 +211,8 @@ macro_rules! make_serializable {
210211

211212
make_serializable!(ValueTypeId, registry::get_value_type, ValueTypeVisitor);
212213
make_serializable!(TraitTypeId, registry::get_trait, TraitTypeVisitor);
214+
make_serializable!(
215+
FunctionId,
216+
registry::get_native_function,
217+
FunctionTypeVisitor
218+
);

turbopack/crates/turbo-tasks/src/registry.rs

Lines changed: 47 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,54 @@
11
use once_cell::sync::Lazy;
2-
use rustc_hash::FxHashMap;
2+
use rustc_hash::{FxHashMap, FxHashSet};
33

44
use crate::{
55
TraitType, ValueType,
6-
id::{TraitTypeId, ValueTypeId},
6+
id::{FunctionId, TraitTypeId, ValueTypeId},
77
macro_helpers::CollectableFunction,
88
native_function::NativeFunction,
99
value_type::{CollectableTrait, CollectableValueType},
1010
};
1111

12-
pub fn get_function_by_global_name(global_name: &str) -> &'static NativeFunction {
13-
static NAME_TO_FUNCTION: Lazy<FxHashMap<&'static str, &'static NativeFunction>> =
14-
Lazy::new(|| {
15-
let mut map = FxHashMap::default();
16-
for collected in inventory::iter::<CollectableFunction> {
17-
let native_function = &**collected.0;
18-
let global_name = native_function.global_name;
19-
let prev = map.insert(global_name, native_function);
20-
debug_assert!(
21-
prev.is_none(),
22-
"multiple functions registered with the name {global_name}!"
23-
);
24-
}
25-
map.shrink_to_fit();
26-
map
27-
});
28-
29-
match NAME_TO_FUNCTION.get(global_name) {
30-
Some(f) => f,
31-
None => panic!("unable to find function: {global_name}"),
12+
struct Functions {
13+
id_to_value: Box<[&'static NativeFunction]>,
14+
value_to_id: FxHashMap<&'static NativeFunction, FunctionId>,
15+
}
16+
static FUNCTIONS: Lazy<Functions> = Lazy::new(|| {
17+
let mut functions = inventory::iter::<CollectableFunction>
18+
.into_iter()
19+
.map(|c| &**c.0)
20+
.collect::<Vec<_>>();
21+
functions.sort_by_key(|f| f.global_name);
22+
let mut value_to_id = FxHashMap::default();
23+
let mut names = FxHashSet::default();
24+
names.reserve(functions.len());
25+
for (index, &native_function) in functions.iter().enumerate() {
26+
// SAFETY: as a usize, usize+1 is definitely non-zero.
27+
let id = unsafe { FunctionId::new_unchecked((index + 1).try_into().unwrap()) };
28+
value_to_id.insert(native_function, id);
29+
let global_name = native_function.global_name;
30+
let new = names.insert(global_name);
31+
debug_assert!(
32+
!new,
33+
"multiple functions registered with name: {global_name}!"
34+
);
35+
}
36+
value_to_id.shrink_to_fit();
37+
Functions {
38+
id_to_value: functions.into_boxed_slice(),
39+
value_to_id,
3240
}
41+
});
42+
43+
pub fn get_native_function(id: FunctionId) -> &'static NativeFunction {
44+
FUNCTIONS.id_to_value[*id as usize - 1]
45+
}
46+
47+
pub fn get_function_id(func: &'static NativeFunction) -> FunctionId {
48+
*FUNCTIONS
49+
.value_to_id
50+
.get(&func)
51+
.expect("function isn't registered")
3352
}
3453

3554
struct Values {
@@ -50,17 +69,18 @@ static VALUES: Lazy<Values> = Lazy::new(|| {
5069

5170
let mut value_to_id = FxHashMap::default();
5271
value_to_id.reserve(all_values.len());
53-
let mut global_name_to_value = FxHashMap::default();
54-
global_name_to_value.reserve(all_values.len());
72+
// Our sort above is non-sensical if names are not unique
73+
let mut names = FxHashSet::default();
74+
names.reserve(all_values.len());
5575

5676
for (index, &value_type) in all_values.iter().enumerate() {
5777
// SAFETY: as a usize, usize+1 is definitely non-zero.
5878
let id = unsafe { ValueTypeId::new_unchecked((index + 1).try_into().unwrap()) };
5979
value_to_id.insert(value_type, id);
60-
let prev = global_name_to_value.insert(value_type.global_name, (id, value_type));
80+
let new = names.insert(value_type.global_name);
6181
debug_assert!(
62-
prev.is_none(),
63-
"two traits registered with the same name: {}",
82+
new,
83+
"two values registered with the same name: {}",
6484
value_type.global_name
6585
);
6686
}

0 commit comments

Comments
 (0)