Skip to content

Commit 4623d24

Browse files
committed
Optimize rb_ary_new_from_values() by using UnwrapCArrayNode
* Using an ArrayBuilderNode would be possible, but seems suboptimal given it remembers the maximum length, and probably most usages do not pass only simple primitives.
1 parent 105e7f9 commit 4623d24

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/main/c/cext/array.c

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,8 @@ VALUE rb_ary_new_from_args(long n, ...) {
5151
}
5252

5353
VALUE rb_ary_new_from_values(long n, const VALUE *values) {
54-
VALUE array = rb_ary_new_capa(n);
55-
for (int i = 0; i < n; i++) {
56-
rb_ary_store(array, i, values[i]);
57-
}
58-
return array;
54+
return rb_tr_wrap(polyglot_invoke(RUBY_CEXT, "rb_ary_new_from_values",
55+
polyglot_from_VALUE_array(values, n)));
5956
}
6057

6158
VALUE rb_assoc_new(VALUE a, VALUE b) {

src/main/java/org/truffleruby/cext/CExtNodes.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1614,4 +1614,14 @@ protected Object checkSymbolCStr(Object string,
16141614
return sym == null ? nil : sym;
16151615
}
16161616
}
1617+
1618+
@CoreMethod(names = "rb_ary_new_from_values", onSingleton = true, required = 1)
1619+
public abstract static class RbAryNewFromValues extends CoreMethodArrayArgumentsNode {
1620+
@Specialization
1621+
protected RubyArray rbAryNewFromValues(Object cArray,
1622+
@Cached UnwrapCArrayNode unwrapCArrayNode) {
1623+
final Object[] values = unwrapCArrayNode.execute(cArray);
1624+
return createArray(values);
1625+
}
1626+
}
16171627
}

0 commit comments

Comments
 (0)