Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce the number of host calls when logging #279

Merged
merged 4 commits into from
Mar 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion crates/cli/benches/benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ impl FunctionCase {
let wasi = WasiCtxBuilder::new()
.stdin(Box::new(ReadPipe::from(&self.payload[..])))
.stdout(Box::new(WritePipe::new_in_memory()))
.inherit_stderr()
.stderr(Box::new(WritePipe::new_in_memory()))
.build();
wasmtime_wasi::add_to_linker(&mut linker, |s| s).unwrap();
let mut store = Store::new(&self.engine, wasi);
Expand Down Expand Up @@ -150,6 +150,15 @@ pub fn criterion_benchmark(c: &mut Criterion) {
)
.unwrap(),
);
function_cases.push(
FunctionCase::new(
Path::new("benches/functions/logging"),
Path::new("index.js"),
&compilation,
linking,
)
.unwrap(),
);
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/cli/benches/functions/logging/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for (let i = 0; i < 100; i++) {
console.error("Here is", "some test", "logs");
}
Empty file.
9 changes: 6 additions & 3 deletions crates/core/src/globals.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,18 @@ where
T: Write + 'static,
{
move |ctx: &Context, _this: &Value, args: &[Value]| {
// Write full string to in-memory destination before writing to stream since each write call to the stream
// will invoke a hostcall.
let mut log_line = String::new();
for (i, arg) in args.iter().enumerate() {
if i != 0 {
write!(stream, " ")?;
log_line.push(' ');
}

stream.write_all(arg.as_str()?.as_bytes())?;
log_line.push_str(arg.as_str()?);
}

writeln!(stream)?;
writeln!(stream, "{log_line}")?;
ctx.undefined_value()
}
}
Expand Down