Skip to content

Commit a7f5106

Browse files
authored
[turbopack] use type_ids to register turbo task traits on value types (#89964)
Tiny optimization, there is no need to use global names for this usecase and we can save a bit of memory and time.
1 parent e6d27d4 commit a7f5106

4 files changed

Lines changed: 26 additions & 22 deletions

File tree

turbopack/crates/turbo-tasks-macros/src/value_impl_macro.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,7 @@ use crate::{
1414
DefinitionContext, FunctionArguments, NativeFn, TurboFn, filter_inline_attributes,
1515
split_function_attributes,
1616
},
17-
global_name::{
18-
global_name_for_method, global_name_for_trait_method_impl, global_name_for_type,
19-
},
17+
global_name::{global_name_for_method, global_name_for_trait_method_impl},
2018
ident::{
2119
get_cast_to_fat_pointer_ident, get_inherent_impl_function_ident, get_path_ident,
2220
get_trait_impl_function_ident, get_type_ident,
@@ -293,7 +291,6 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
293291
});
294292
}
295293
}
296-
let global_value_name = global_name_for_type(ty);
297294
quote! {
298295
// Register all the function impls so the ValueType can find them
299296
// This means objects resolve as
@@ -304,7 +301,7 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
304301
turbo_tasks::macro_helpers::inventory_submit!{
305302
turbo_tasks::macro_helpers::CollectableTraitMethods(
306303
|| (
307-
#global_value_name,
304+
::std::any::TypeId::of::<#ty>(),
308305
<::std::boxed::Box<dyn #trait_path> as turbo_tasks::VcValueTrait>::get_trait_type_id(),
309306
vec![#(#trait_methods)*]
310307
)

turbopack/crates/turbo-tasks-macros/src/value_macro.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,11 +425,13 @@ pub fn value_type_and_register(
425425
};
426426

427427
quote! {
428-
429428
static #value_type_ident: turbo_tasks::macro_helpers::Lazy<turbo_tasks::ValueType> =
430429
turbo_tasks::macro_helpers::Lazy::new(|| {
431430
let mut value_type = #new_value_type;
432-
turbo_tasks::macro_helpers::register_trait_methods(&mut value_type);
431+
turbo_tasks::macro_helpers::register_trait_methods(
432+
::std::any::TypeId::of::<#ty>(),
433+
&mut value_type,
434+
);
433435
value_type
434436
});
435437

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Runtime helpers for [turbo-tasks-macro].
22
3+
use std::any::TypeId;
4+
35
pub use async_trait::async_trait;
46
pub use bincode;
57
pub use once_cell::sync::{Lazy, OnceCell};
@@ -144,29 +146,29 @@ inventory::collect! {CollectableTraitCastFunctions}
144146
#[allow(clippy::type_complexity)]
145147
pub struct CollectableTraitMethods(
146148
pub fn() -> (
147-
&'static str, // A value type name
149+
TypeId,
148150
TraitTypeId,
149151
Vec<(&'static str, &'static NativeFunction)>,
150152
),
151153
);
152154
inventory::collect!(CollectableTraitMethods);
153155

154156
// Called when initializing ValueTypes by value_impl
155-
pub fn register_trait_methods(value_type: &mut ValueType) {
157+
pub fn register_trait_methods(type_id: TypeId, value_type: &mut ValueType) {
156158
#[allow(clippy::type_complexity)]
157159
static TRAIT_METHODS_BY_VALUE: Lazy<
158-
FxDashMap<&'static str, Vec<(TraitTypeId, Vec<(&'static str, &'static NativeFunction)>)>>,
160+
FxDashMap<TypeId, Vec<(TraitTypeId, Vec<(&'static str, &'static NativeFunction)>)>>,
159161
> = Lazy::new(|| {
160-
let map: FxDashMap<&'static str, Vec<_>> = FxDashMap::default();
162+
let map: FxDashMap<TypeId, Vec<_>> = FxDashMap::default();
161163
for CollectableTraitMethods(thunk) in inventory::iter::<CollectableTraitMethods> {
162-
let (value_name, trait_type_id, fn_items) = thunk();
163-
map.entry(value_name)
164+
let (type_id, trait_type_id, fn_items) = thunk();
165+
map.entry(type_id)
164166
.or_default()
165167
.push((trait_type_id, fn_items));
166168
}
167169
map
168170
});
169-
match TRAIT_METHODS_BY_VALUE.remove(value_type.global_name) {
171+
match TRAIT_METHODS_BY_VALUE.remove(&type_id) {
170172
Some((_, traits)) => {
171173
for (trait_type_id, methods) in traits {
172174
let trait_type = crate::registry::get_trait(trait_type_id);

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::num::NonZeroU16;
22

33
use anyhow::Error;
44
use once_cell::sync::Lazy;
5-
use rustc_hash::{FxHashMap, FxHashSet};
5+
use rustc_hash::FxHashMap;
66

77
use crate::{
88
TraitType, ValueType,
@@ -68,17 +68,20 @@ impl<T: RegistryItem> Registry<T> {
6868
items.sort_unstable_by_key(|item| item.global_name());
6969

7070
let mut item_to_id = FxHashMap::with_capacity_and_hasher(items.len(), Default::default());
71-
let mut names = FxHashSet::with_capacity_and_hasher(items.len(), Default::default());
7271

7372
let mut id = NonZeroU16::MIN;
73+
let mut prev_name: Option<&str> = None;
7474
for &item in items.iter() {
75-
item_to_id.insert(item, id.into());
7675
let global_name = item.global_name();
77-
assert!(
78-
names.insert(global_name),
79-
"multiple {ty} items registered with name: {global_name}!",
80-
ty = T::TYPE_NAME
81-
);
76+
if let Some(prev) = prev_name {
77+
assert!(
78+
prev != global_name,
79+
"multiple {ty} items registered with name: {global_name}!",
80+
ty = T::TYPE_NAME
81+
);
82+
}
83+
prev_name = Some(global_name);
84+
item_to_id.insert(item, id.into());
8285
id = id.checked_add(1).expect("overflowing item ids");
8386
}
8487

0 commit comments

Comments
 (0)