Skip to content

Commit 2a8fa1a

Browse files
committed
Refactor buffering and add CLI option
1 parent bf0a78f commit 2a8fa1a

5 files changed

Lines changed: 15 additions & 30 deletions

File tree

crates/cli-flags/src/lib.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -506,9 +506,8 @@ wasmtime_option_group! {
506506
/// Include (optional) signatures to facilitate validation checks during replay
507507
/// (see `wasmtime replay` for details).
508508
pub validation_metadata: Option<bool>,
509-
/// Window size of internal buffering for record events (large windows offer more opportunities
510-
/// for coalescing events at the cost of memory usage).
511-
pub event_window_size: Option<usize>,
509+
/// Writer buffer size (in bytes) for internal buffering during recording. Defaults to 8 KiB.
510+
pub buffer_size: Option<usize>,
512511
}
513512

514513
enum Record {

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

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,6 @@ impl From<RRModuleInstanceId> for InstanceId {
110110
///
111111
/// This type can be optimized for [`RREvent`] data configurations.
112112
pub struct RecordBuffer {
113-
/// In-memory event buffer to enable windows for coalescing
114-
buf: Vec<RREvent>,
115113
/// Writer to store data into
116114
writer: Box<dyn RecordWriter>,
117115
/// Settings in record configuration
@@ -121,10 +119,7 @@ pub struct RecordBuffer {
121119
impl RecordBuffer {
122120
/// Push a new record event [`RREvent`] to the buffer
123121
fn push_event(&mut self, event: RREvent) -> Result<()> {
124-
self.buf.push(event);
125-
if self.buf.len() >= self.settings().event_window_size {
126-
self.flush()?;
127-
}
122+
to_record_writer(&event, &mut *self.writer)?;
128123
Ok(())
129124
}
130125

@@ -140,7 +135,6 @@ impl Recorder for RecordBuffer {
140135
fn new_recorder(writer: impl RecordWriter, settings: RecordSettings) -> Result<Self> {
141136
let settings_local = settings.clone();
142137
let mut buf = RecordBuffer {
143-
buf: Vec::new(),
144138
writer: Box::new(writer),
145139
settings,
146140
};
@@ -170,10 +164,8 @@ impl Recorder for RecordBuffer {
170164

171165
fn flush(&mut self) -> Result<()> {
172166
log::debug!("Flushing record buffer...");
173-
for e in self.buf.drain(..) {
174-
to_record_writer(&e, &mut *self.writer)?;
175-
}
176-
return Ok(());
167+
self.writer.flush()?;
168+
Ok(())
177169
}
178170

179171
#[inline]
@@ -273,19 +265,11 @@ impl Replayer for ReplayBuffer {
273265
}
274266

275267
#[inline]
276-
#[allow(
277-
unused,
278-
reason = "method only used for gated validation, but will be extended in the future"
279-
)]
280268
fn settings(&self) -> &ReplaySettings {
281269
&self.settings
282270
}
283271

284272
#[inline]
285-
#[allow(
286-
unused,
287-
reason = "method only used for gated validation, but will be extended in the future"
288-
)]
289273
fn trace_settings(&self) -> &RecordSettings {
290274
&self.trace_settings
291275
}

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,12 @@ mod io;
2121
pub struct RecordSettings {
2222
/// Flag to include additional signatures for replay validation.
2323
pub add_validation: bool,
24-
/// Maximum window size of internal event buffer.
25-
pub event_window_size: usize,
2624
}
2725

2826
impl Default for RecordSettings {
2927
fn default() -> Self {
3028
Self {
3129
add_validation: false,
32-
event_window_size: 16,
3330
}
3431
}
3532
}

src/commands/replay.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ pub struct ReplayOptions {
3636
/// `validate` was enabled for recording.
3737
#[arg(short, long, default_value_t = 64)]
3838
pub deserialize_buffer_size: usize,
39+
40+
/// Size of the read buffer (in bytes) for internal buffering during replay.
41+
#[arg(short, long, default_value_t = 8 * 1024)]
42+
pub read_buffer_size: usize,
3943
}
4044

4145
/// Execute a deterministic, embedding-agnostic replay of a Wasm modules given its associated recorded trace.
@@ -104,7 +108,7 @@ impl ReplayCommand {
104108

105109
let allow_unknown_exports = self.run_cmd.run.common.wasm.unknown_exports_allow;
106110
let mut replay_instance = renv.instantiate_with(
107-
io::BufReader::new(fs::File::open(opts.trace)?),
111+
io::BufReader::with_capacity(opts.read_buffer_size, fs::File::open(opts.trace)?),
108112
|store| {
109113
// If fuel has been configured, we want to add the configured
110114
// fuel amount to this store.

src/commands/run.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,15 @@ pub mod rr_impl {
135135
add_validation: record
136136
.validation_metadata
137137
.unwrap_or(default_settings.add_validation),
138-
event_window_size: record
139-
.event_window_size
140-
.unwrap_or(default_settings.event_window_size),
141138
};
142139
if path.trim().is_empty() {
143140
store.record(io::sink(), settings)?;
144141
} else {
145-
store.record(fs::File::create(&path)?, settings)?;
142+
let file = io::BufWriter::with_capacity(
143+
record.buffer_size.unwrap_or(8 * 1024), // Default to 8 KiB buffer
144+
fs::File::create(&path)?,
145+
);
146+
store.record(file, settings)?;
146147
}
147148
}
148149
Ok(())

0 commit comments

Comments
 (0)