Skip to content

Commit 85ed29c

Browse files
committed
Optimize: Make lowerreturn optional and specialize host function return with empty args
1 parent 95591ed commit 85ed29c

3 files changed

Lines changed: 33 additions & 15 deletions

File tree

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -450,14 +450,25 @@ impl<'a, T: 'static> LowerContext<'a, T> {
450450
_ => bail!("HostFuncReturn encountered in invalid phase"),
451451
}
452452
// End of the lowering process (for host calls)
453-
if let Some(e) = lowering_error {
454-
return Err(e.into());
453+
if let Some(err) = lowering_error {
454+
return Err(err.into());
455455
}
456456
if let Some(storage) = result_storage.as_deref_mut() {
457457
e.args.into_raw_slice(storage);
458458
}
459459
complete = true;
460460
}
461+
RREvent::HostFuncReturnEmptyEvent => {
462+
match phase {
463+
ReplayLoweringPhase::HostFuncReturn => {}
464+
_ => bail!("HostFuncReturnEmpty encountered in invalid phase"),
465+
}
466+
// End of the lowering process (for host calls)
467+
if let Some(err) = lowering_error {
468+
return Err(err.into());
469+
}
470+
complete = true;
471+
}
461472
RREvent::ComponentWasmFuncEntry(e) => {
462473
match phase {
463474
ReplayLoweringPhase::WasmFuncEntry => {}
@@ -487,16 +498,16 @@ impl<'a, T: 'static> LowerContext<'a, T> {
487498
RREvent::ComponentLowerFlatReturn(e) => {
488499
if run_validate {
489500
lower_stack.pop().ok_or(ReplayError::InvalidEventPosition)?;
501+
lowering_error = e.0.ret().map_err(Into::into).err();
490502
}
491-
lowering_error = e.0.ret().map_err(Into::into).err();
492503
}
493504
RREvent::ComponentLowerMemoryReturn(e) => {
494505
if run_validate {
495506
lower_store_stack
496507
.pop()
497508
.ok_or(ReplayError::InvalidEventPosition)?;
509+
lowering_error = e.0.ret().map_err(Into::into).err();
498510
}
499-
lowering_error = e.0.ret().map_err(Into::into).err();
500511
}
501512
RREvent::ComponentMemorySliceWrite(e) => {
502513
let offset = e.offset as usize;

crates/wasmtime/src/runtime/rr/backend/crimp.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,9 @@ rr_event! {
120120
/// Nop Event
121121
Nop,
122122
/// Event signalling the end of a trace
123-
Eof
123+
Eof,
124+
/// Specialized variant of `HostFuncReturnEvent` with empty args.
125+
HostFuncReturnEmptyEvent
124126
;
125127
/// The signature of the trace, enabling trace integrity during replay.
126128
///

crates/wasmtime/src/runtime/rr/hooks/component_hooks.rs

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ use crate::ValRaw;
44
use crate::component::{ComponentInstanceId, func::LowerContext};
55
#[cfg(feature = "rr")]
66
use crate::rr::{
7-
RRFuncArgVals, RRFuncArgValsConvertable, RecordBuffer, Recorder, ResultEvent, Validate,
7+
RREvent, RRFuncArgVals, RRFuncArgValsConvertable, RecordBuffer, Recorder, ResultEvent,
8+
Validate,
89
common_events::{HostFuncEntryEvent, HostFuncReturnEvent, WasmFuncReturnEvent},
910
component_events::{
1011
InstantiationEvent, LowerFlatEntryEvent, LowerFlatReturnEvent, LowerMemoryEntryEvent,
@@ -153,9 +154,13 @@ pub fn record_host_func_return(
153154
store: &mut StoreOpaque,
154155
) -> Result<()> {
155156
#[cfg(feature = "rr")]
156-
store.record_event(|| HostFuncReturnEvent {
157-
args: RRFuncArgVals::from_flat_storage(args, flat_results),
158-
})?;
157+
if args.is_empty() {
158+
store.record_event(|| RREvent::HostFuncReturnEmptyEvent)?;
159+
} else {
160+
store.record_event(|| HostFuncReturnEvent {
161+
args: RRFuncArgVals::from_flat_storage(args, flat_results),
162+
})?;
163+
}
159164
#[cfg(not(feature = "rr"))]
160165
let _ = (args, flat_results, store);
161166
Ok(())
@@ -181,9 +186,9 @@ where
181186
})?;
182187
let store_result = lower_store(cx, ty, offset);
183188
#[cfg(feature = "rr")]
184-
cx.store
185-
.0
186-
.record_event(|| LowerMemoryReturnEvent(ResultEvent::from_anyhow_result(&store_result)))?;
189+
cx.store.0.record_event_validation(|| {
190+
LowerMemoryReturnEvent(ResultEvent::from_anyhow_result(&store_result))
191+
})?;
187192
store_result
188193
}
189194

@@ -203,9 +208,9 @@ where
203208
.record_event_validation(|| LowerFlatEntryEvent { ty })?;
204209
let lower_result = lower(cx, ty);
205210
#[cfg(feature = "rr")]
206-
cx.store
207-
.0
208-
.record_event(|| LowerFlatReturnEvent(ResultEvent::from_anyhow_result(&lower_result)))?;
211+
cx.store.0.record_event_validation(|| {
212+
LowerFlatReturnEvent(ResultEvent::from_anyhow_result(&lower_result))
213+
})?;
209214
lower_result
210215
}
211216

0 commit comments

Comments
 (0)