Skip to content

Commit fce4afa

Browse files
committed
Fill out MAY_REQUIRE_REALLOC for more types
This is an relatively impactful optimization to the performance of futures/streams when transferring data from the host to the guest, so fill this out for more types get the optimization to fire more. This was found during some improvement of wasi-libc where some code paths weren't getting hit, but they should,, and this is required to have that work out.
1 parent 3250c98 commit fce4afa

File tree

6 files changed

+22
-0
lines changed

6 files changed

+22
-0
lines changed

crates/component-macro/src/component.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,7 @@ fn expand_record_for_component_type(
359359
let mut lower_field_declarations = TokenStream::new();
360360
let mut abi_list = TokenStream::new();
361361
let mut unique_types = HashSet::new();
362+
let mut may_require_realloc = TokenStream::new();
362363

363364
for (index, syn::Field { ident, ty, .. }) in fields.iter().enumerate() {
364365
let generic = format_ident!("T{}", index);
@@ -371,6 +372,9 @@ fn expand_record_for_component_type(
371372
abi_list.extend(quote!(
372373
<#ty as #wt::component::ComponentType>::ABI,
373374
));
375+
may_require_realloc.extend(quote!(
376+
<#ty as #wt::component::ComponentType>::MAY_REQUIRE_REALLOC ||
377+
));
374378

375379
unique_types.insert(ty);
376380
}
@@ -408,6 +412,7 @@ fn expand_record_for_component_type(
408412

409413
const ABI: #internal::CanonicalAbiInfo =
410414
#internal::CanonicalAbiInfo::record_static(&[#abi_list]);
415+
const MAY_REQUIRE_REALLOC: bool = #may_require_realloc false;
411416

412417
#[inline]
413418
fn typecheck(
@@ -961,6 +966,7 @@ impl Expander for ComponentTypeExpander {
961966
let mut lower_generic_args = TokenStream::new();
962967
let mut abi_list = TokenStream::new();
963968
let mut unique_types = HashSet::new();
969+
let mut may_require_realloc = TokenStream::new();
964970

965971
for (index, VariantCase { attrs, ident, ty }) in cases.iter().enumerate() {
966972
let rename = find_rename(attrs)?;
@@ -969,6 +975,9 @@ impl Expander for ComponentTypeExpander {
969975

970976
if let Some(ty) = ty {
971977
abi_list.extend(quote!(Some(<#ty as #wt::component::ComponentType>::ABI),));
978+
may_require_realloc.extend(quote!(
979+
<#ty as #wt::component::ComponentType>::MAY_REQUIRE_REALLOC ||
980+
));
972981

973982
case_names_and_checks.extend(
974983
quote!((#name, Some(<#ty as #wt::component::ComponentType>::typecheck)),),
@@ -1032,6 +1041,7 @@ impl Expander for ComponentTypeExpander {
10321041

10331042
const ABI: #internal::CanonicalAbiInfo =
10341043
#internal::CanonicalAbiInfo::variant_static(&[#abi_list]);
1044+
const MAY_REQUIRE_REALLOC: bool = #may_require_realloc false;
10351045
}
10361046

10371047
unsafe impl #impl_generics #internal::ComponentVariant for #name #ty_generics #where_clause {
@@ -1093,6 +1103,7 @@ impl Expander for ComponentTypeExpander {
10931103

10941104
const ABI: #internal::CanonicalAbiInfo =
10951105
#internal::CanonicalAbiInfo::enum_(#cases_len);
1106+
const MAY_REQUIRE_REALLOC: bool = false;
10961107
}
10971108

10981109
unsafe impl #internal::ComponentVariant for #name {

crates/wasmtime/src/runtime/component/func/typed.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,6 +959,7 @@ macro_rules! forward_type_impls {
959959
type Lower = <$b as ComponentType>::Lower;
960960

961961
const ABI: CanonicalAbiInfo = <$b as ComponentType>::ABI;
962+
const MAY_REQUIRE_REALLOC: bool = <$b as ComponentType>::MAY_REQUIRE_REALLOC;
962963

963964
#[inline]
964965
fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {
@@ -1193,6 +1194,7 @@ macro_rules! floats {
11931194
type Lower = ValRaw;
11941195

11951196
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::$abi;
1197+
const MAY_REQUIRE_REALLOC: bool = false;
11961198

11971199
fn typecheck(ty: &InterfaceType, _types: &InstanceType<'_>) -> Result<()> {
11981200
match ty {
@@ -1310,6 +1312,7 @@ unsafe impl ComponentType for bool {
13101312
type Lower = ValRaw;
13111313

13121314
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::SCALAR1;
1315+
const MAY_REQUIRE_REALLOC: bool = false;
13131316

13141317
fn typecheck(ty: &InterfaceType, _types: &InstanceType<'_>) -> Result<()> {
13151318
match ty {
@@ -1376,6 +1379,7 @@ unsafe impl ComponentType for char {
13761379
type Lower = ValRaw;
13771380

13781381
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::SCALAR4;
1382+
const MAY_REQUIRE_REALLOC: bool = false;
13791383

13801384
fn typecheck(ty: &InterfaceType, _types: &InstanceType<'_>) -> Result<()> {
13811385
match ty {
@@ -2534,6 +2538,7 @@ where
25342538
type Lower = TupleLower<<u32 as ComponentType>::Lower, T::Lower>;
25352539

25362540
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::variant_static(&[None, Some(T::ABI)]);
2541+
const MAY_REQUIRE_REALLOC: bool = T::MAY_REQUIRE_REALLOC;
25372542

25382543
fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {
25392544
match ty {
@@ -2675,6 +2680,7 @@ where
26752680
type Lower = ResultLower<T::Lower, E::Lower>;
26762681

26772682
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::variant_static(&[Some(T::ABI), Some(E::ABI)]);
2683+
const MAY_REQUIRE_REALLOC: bool = T::MAY_REQUIRE_REALLOC || E::MAY_REQUIRE_REALLOC;
26782684

26792685
fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {
26802686
match ty {
@@ -3032,6 +3038,7 @@ macro_rules! impl_component_ty_for_tuples {
30323038
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::record_static(&[
30333039
$($t::ABI),*
30343040
]);
3041+
const MAY_REQUIRE_REALLOC: bool = false $(|| $t::MAY_REQUIRE_REALLOC)*;
30353042

30363043
const IS_RUST_UNIT_TYPE: bool = {
30373044
let mut _is_unit = true;

crates/wasmtime/src/runtime/component/resources/any.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ impl ResourceAny {
272272

273273
unsafe impl ComponentType for ResourceAny {
274274
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::SCALAR4;
275+
const MAY_REQUIRE_REALLOC: bool = false;
275276

276277
type Lower = <u32 as ComponentType>::Lower;
277278

crates/wasmtime/src/runtime/component/resources/host.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ where
312312
D: PartialEq + Send + Sync + Copy + 'static,
313313
{
314314
const ABI: CanonicalAbiInfo = CanonicalAbiInfo::SCALAR4;
315+
const MAY_REQUIRE_REALLOC: bool = false;
315316

316317
type Lower = <u32 as ComponentType>::Lower;
317318

crates/wasmtime/src/runtime/component/resources/host_dynamic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ impl ResourceDynamic {
144144
}
145145

146146
unsafe impl ComponentType for ResourceDynamic {
147+
const MAY_REQUIRE_REALLOC: bool = false;
147148
const ABI: CanonicalAbiInfo = HostResource::<Dynamic, u32>::ABI;
148149
type Lower = crate::ValRaw;
149150

crates/wasmtime/src/runtime/component/resources/host_static.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ where
217217

218218
unsafe impl<T: 'static> ComponentType for Resource<T> {
219219
const ABI: CanonicalAbiInfo = HostResource::<Static<T>, ()>::ABI;
220+
const MAY_REQUIRE_REALLOC: bool = false;
220221
type Lower = crate::ValRaw;
221222

222223
fn typecheck(ty: &InterfaceType, types: &InstanceType<'_>) -> Result<()> {

0 commit comments

Comments
 (0)