Skip to content

Commit 6e363af

Browse files
committed
Write to a buf before writing to stdout
I noticed that using serde to write to stdout consumes quite a bit of fuel. Not sure why. Here's a result from a test I ran (not using the Functions crate, but doing essentially the same thing): Before: ``` Linear Memory Usage: 1216KB Instructions: 9.243096M Size: 180KB ``` After: ``` Linear Memory Usage: 1280KB Instructions: 6.448534M Size: 181KB ```
1 parent 93ce9a0 commit 6e363af

File tree

1 file changed

+3
-2
lines changed
  • shopify_function_macro/src

1 file changed

+3
-2
lines changed

shopify_function_macro/src/lib.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,9 @@ pub fn shopify_function(
104104
std::io::Read::read_to_string(&mut #input_stream, &mut string)?;
105105
let input: #input_type = serde_json::from_str(&string)?;
106106
let mut out = #output_stream;
107-
let mut serializer = serde_json::Serializer::new(&mut out);
108-
#name(input)?.serialize(&mut serializer)?;
107+
let result = #name(input)?;
108+
let serialized = serde_json::to_vec(&result)?;
109+
std::io::Write::write_all(&mut out, serialized.as_slice())?;
109110
Ok(())
110111
}
111112
#ast

0 commit comments

Comments
 (0)