Skip to content

Commit b5a2c43

Browse files
authored
Merge pull request #430 from bytecodealliance/jb/faster-array-building
2 parents aff2d8f + 92b238e commit b5a2c43

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

ext/src/ruby_api/func.rs

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -222,13 +222,11 @@ impl<'a> Func<'a> {
222222
match results.as_slice() {
223223
[] => Ok(().into_value()),
224224
[result] => result.to_ruby_value(store),
225-
_ => {
226-
let array = RArray::with_capacity(results.len());
227-
for result in results {
228-
array.push(result.to_ruby_value(store)?)?;
229-
}
230-
Ok(array.as_value())
231-
}
225+
_ => results
226+
.into_iter()
227+
.map(|result| result.to_ruby_value(store))
228+
.collect::<Result<RArray, _>>()
229+
.map(|ary| ary.as_value()),
232230
}
233231
}
234232
}
@@ -270,15 +268,15 @@ pub fn make_func_closure(
270268
let wrapped_caller = Obj::wrap(Caller::new(caller_impl));
271269
let store_context = StoreContextValue::from(wrapped_caller);
272270

273-
let rparams = RArray::with_capacity(params.len() + 1);
274-
rparams.push(wrapped_caller.as_value()).unwrap();
275-
276-
for (i, param) in params.iter().enumerate() {
277-
let rparam = param
271+
let params_iter = params.iter().enumerate().map(|(i, param)| {
272+
param
278273
.to_ruby_value(&store_context)
279-
.map_err(|e| wasmtime::Error::msg(format!("invalid argument at index {i}: {e}")))?;
280-
rparams.push(rparam).unwrap();
281-
}
274+
.map_err(|e| wasmtime::Error::msg(format!("invalid argument at index {i}: {e}")))
275+
});
276+
277+
let rparams = std::iter::once(Ok(wrapped_caller.as_value()))
278+
.chain(params_iter)
279+
.collect::<Result<RArray, _>>()?;
282280

283281
let ruby = Ruby::get().unwrap();
284282
let callable = ruby.get_inner(callable);

0 commit comments

Comments
 (0)