Skip to content

Commit 2c16553

Browse files
committed
Revert moving things to Ruby
This is slowing down benchmarks on x86, so lets revert it for now.
1 parent acbb8d4 commit 2c16553

File tree

6 files changed

+128
-113
lines changed

6 files changed

+128
-113
lines changed

array.c

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3630,6 +3630,41 @@ rb_ary_sort_by_bang(VALUE ary)
36303630
return ary;
36313631
}
36323632

3633+
3634+
/*
3635+
* call-seq:
3636+
* array.map {|element| ... } -> new_array
3637+
* array.map -> new_enumerator
3638+
*
3639+
* Calls the block, if given, with each element of +self+;
3640+
* returns a new +Array+ whose elements are the return values from the block:
3641+
*
3642+
* a = [:foo, 'bar', 2]
3643+
* a1 = a.map {|element| element.class }
3644+
* a1 # => [Symbol, String, Integer]
3645+
*
3646+
* Returns a new Enumerator if no block given:
3647+
* a = [:foo, 'bar', 2]
3648+
* a1 = a.map
3649+
* a1 # => #<Enumerator: [:foo, "bar", 2]:map>
3650+
*
3651+
*/
3652+
3653+
static VALUE
3654+
rb_ary_collect(VALUE ary)
3655+
{
3656+
long i;
3657+
VALUE collect;
3658+
3659+
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
3660+
collect = rb_ary_new2(RARRAY_LEN(ary));
3661+
for (i = 0; i < RARRAY_LEN(ary); i++) {
3662+
rb_ary_push(collect, rb_yield(RARRAY_AREF(ary, i)));
3663+
}
3664+
return collect;
3665+
}
3666+
3667+
36333668
/*
36343669
* call-seq:
36353670
* array.map! {|element| ... } -> self
@@ -3772,6 +3807,42 @@ rb_ary_values_at(int argc, VALUE *argv, VALUE ary)
37723807
}
37733808

37743809

3810+
/*
3811+
* call-seq:
3812+
* array.select {|element| ... } -> new_array
3813+
* array.select -> new_enumerator
3814+
*
3815+
* Calls the block, if given, with each element of +self+;
3816+
* returns a new +Array+ containing those elements of +self+
3817+
* for which the block returns a truthy value:
3818+
*
3819+
* a = [:foo, 'bar', 2, :bam]
3820+
* a1 = a.select {|element| element.to_s.start_with?('b') }
3821+
* a1 # => ["bar", :bam]
3822+
*
3823+
* Returns a new Enumerator if no block given:
3824+
*
3825+
* a = [:foo, 'bar', 2, :bam]
3826+
* a.select # => #<Enumerator: [:foo, "bar", 2, :bam]:select>
3827+
*
3828+
*/
3829+
3830+
static VALUE
3831+
rb_ary_select(VALUE ary)
3832+
{
3833+
VALUE result;
3834+
long i;
3835+
3836+
RETURN_SIZED_ENUMERATOR(ary, 0, 0, ary_enum_length);
3837+
result = rb_ary_new2(RARRAY_LEN(ary));
3838+
for (i = 0; i < RARRAY_LEN(ary); i++) {
3839+
if (RTEST(rb_yield(RARRAY_AREF(ary, i)))) {
3840+
rb_ary_push(result, rb_ary_elt(ary, i));
3841+
}
3842+
}
3843+
return result;
3844+
}
3845+
37753846
struct select_bang_arg {
37763847
VALUE ary;
37773848
long len[2];
@@ -6625,12 +6696,6 @@ ary_sample(rb_execution_context_t *ec, VALUE ary, VALUE randgen, VALUE nv, VALUE
66256696
return result;
66266697
}
66276698

6628-
static VALUE
6629-
ary_sized_alloc(rb_execution_context_t *ec, VALUE self)
6630-
{
6631-
return rb_ary_new2(RARRAY_LEN(self));
6632-
}
6633-
66346699
static VALUE
66356700
ary_sample0(rb_execution_context_t *ec, VALUE ary)
66366701
{
@@ -8633,9 +8698,13 @@ Init_Array(void)
86338698
rb_define_method(rb_cArray, "sort", rb_ary_sort, 0);
86348699
rb_define_method(rb_cArray, "sort!", rb_ary_sort_bang, 0);
86358700
rb_define_method(rb_cArray, "sort_by!", rb_ary_sort_by_bang, 0);
8701+
rb_define_method(rb_cArray, "collect", rb_ary_collect, 0);
86368702
rb_define_method(rb_cArray, "collect!", rb_ary_collect_bang, 0);
8703+
rb_define_method(rb_cArray, "map", rb_ary_collect, 0);
86378704
rb_define_method(rb_cArray, "map!", rb_ary_collect_bang, 0);
8705+
rb_define_method(rb_cArray, "select", rb_ary_select, 0);
86388706
rb_define_method(rb_cArray, "select!", rb_ary_select_bang, 0);
8707+
rb_define_method(rb_cArray, "filter", rb_ary_select, 0);
86398708
rb_define_method(rb_cArray, "filter!", rb_ary_select_bang, 0);
86408709
rb_define_method(rb_cArray, "keep_if", rb_ary_keep_if, 0);
86418710
rb_define_method(rb_cArray, "values_at", rb_ary_values_at, -1);

array.rb

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -55,73 +55,6 @@ def each
5555
self
5656
end
5757

58-
# call-seq:
59-
# array.map {|element| ... } -> new_array
60-
# array.map -> new_enumerator
61-
#
62-
# Calls the block, if given, with each element of +self+;
63-
# returns a new +Array+ whose elements are the return values from the block:
64-
#
65-
# a = [:foo, 'bar', 2]
66-
# a1 = a.map {|element| element.class }
67-
# a1 # => [Symbol, String, Integer]
68-
#
69-
# Returns a new Enumerator if no block given:
70-
# a = [:foo, 'bar', 2]
71-
# a1 = a.map
72-
# a1 # => #<Enumerator: [:foo, "bar", 2]:map>
73-
def map
74-
Primitive.attr! :inline_block
75-
76-
unless defined?(yield)
77-
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)'
78-
end
79-
80-
_i = 0
81-
value = nil
82-
result = Primitive.ary_sized_alloc
83-
while Primitive.cexpr!(%q{ ary_fetch_next(self, LOCAL_PTR(_i), LOCAL_PTR(value)) })
84-
result << yield(value)
85-
end
86-
result
87-
end
88-
89-
alias collect map
90-
91-
# call-seq:
92-
# array.select {|element| ... } -> new_array
93-
# array.select -> new_enumerator
94-
#
95-
# Calls the block, if given, with each element of +self+;
96-
# returns a new +Array+ containing those elements of +self+
97-
# for which the block returns a truthy value:
98-
#
99-
# a = [:foo, 'bar', 2, :bam]
100-
# a1 = a.select {|element| element.to_s.start_with?('b') }
101-
# a1 # => ["bar", :bam]
102-
#
103-
# Returns a new Enumerator if no block given:
104-
#
105-
# a = [:foo, 'bar', 2, :bam]
106-
# a.select # => #<Enumerator: [:foo, "bar", 2, :bam]:select>
107-
def select
108-
Primitive.attr! :inline_block
109-
110-
unless defined?(yield)
111-
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 0, 0, ary_enum_length)'
112-
end
113-
114-
_i = 0
115-
value = nil
116-
result = Primitive.ary_sized_alloc
117-
while Primitive.cexpr!(%q{ ary_fetch_next(self, LOCAL_PTR(_i), LOCAL_PTR(value)) })
118-
result << value if yield value
119-
end
120-
result
121-
end
122-
123-
alias filter select
124-
12558
# call-seq:
12659
# array.shuffle!(random: Random) -> array
12760
#

numeric.c

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5713,6 +5713,50 @@ int_downto_size(VALUE from, VALUE args, VALUE eobj)
57135713
return ruby_num_interval_step_size(from, RARRAY_AREF(args, 0), INT2FIX(-1), FALSE);
57145714
}
57155715

5716+
/*
5717+
* call-seq:
5718+
* downto(limit) {|i| ... } -> self
5719+
* downto(limit) -> enumerator
5720+
*
5721+
* Calls the given block with each integer value from +self+ down to +limit+;
5722+
* returns +self+:
5723+
*
5724+
* a = []
5725+
* 10.downto(5) {|i| a << i } # => 10
5726+
* a # => [10, 9, 8, 7, 6, 5]
5727+
* a = []
5728+
* 0.downto(-5) {|i| a << i } # => 0
5729+
* a # => [0, -1, -2, -3, -4, -5]
5730+
* 4.downto(5) {|i| fail 'Cannot happen' } # => 4
5731+
*
5732+
* With no block given, returns an Enumerator.
5733+
*
5734+
*/
5735+
5736+
static VALUE
5737+
int_downto(VALUE from, VALUE to)
5738+
{
5739+
RETURN_SIZED_ENUMERATOR(from, 1, &to, int_downto_size);
5740+
if (FIXNUM_P(from) && FIXNUM_P(to)) {
5741+
long i, end;
5742+
5743+
end = FIX2LONG(to);
5744+
for (i=FIX2LONG(from); i >= end; i--) {
5745+
rb_yield(LONG2FIX(i));
5746+
}
5747+
}
5748+
else {
5749+
VALUE i = from, c;
5750+
5751+
while (!(c = rb_funcall(i, '<', 1, to))) {
5752+
rb_yield(i);
5753+
i = rb_funcall(i, '-', 1, INT2FIX(1));
5754+
}
5755+
if (NIL_P(c)) rb_cmperr(i, to);
5756+
}
5757+
return from;
5758+
}
5759+
57165760
static VALUE
57175761
int_dotimes_size(VALUE num, VALUE args, VALUE eobj)
57185762
{
@@ -6320,6 +6364,7 @@ Init_Numeric(void)
63206364
rb_define_method(rb_cInteger, "anybits?", int_anybits_p, 1);
63216365
rb_define_method(rb_cInteger, "nobits?", int_nobits_p, 1);
63226366
rb_define_method(rb_cInteger, "upto", int_upto, 1);
6367+
rb_define_method(rb_cInteger, "downto", int_downto, 1);
63236368
rb_define_method(rb_cInteger, "succ", int_succ, 0);
63246369
rb_define_method(rb_cInteger, "next", int_succ, 0);
63256370
rb_define_method(rb_cInteger, "pred", int_pred, 0);

numeric.rb

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -241,36 +241,6 @@ def times
241241
self
242242
end
243243

244-
# call-seq:
245-
# downto(limit) {|i| ... } -> self
246-
# downto(limit) -> enumerator
247-
#
248-
# Calls the given block with each integer value from +self+ down to +limit+;
249-
# returns +self+:
250-
#
251-
# a = []
252-
# 10.downto(5) {|i| a << i } # => 10
253-
# a # => [10, 9, 8, 7, 6, 5]
254-
# a = []
255-
# 0.downto(-5) {|i| a << i } # => 0
256-
# a # => [0, -1, -2, -3, -4, -5]
257-
# 4.downto(5) {|i| fail 'Cannot happen' } # => 4
258-
#
259-
# With no block given, returns an Enumerator.
260-
def downto to
261-
Primitive.attr! :inline_block
262-
unless defined?(yield)
263-
return Primitive.cexpr! 'SIZED_ENUMERATOR(self, 1, &to, int_downto_size)'
264-
end
265-
266-
from = self
267-
while from >= to
268-
yield from
269-
from = from.pred
270-
end
271-
self
272-
end
273-
274244
# call-seq:
275245
# to_i -> self
276246
#

test/ruby/test_backtrace.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -223,15 +223,15 @@ def self.foo
223223
@res = caller_locations(2, 1).inspect
224224
end
225225
@line = __LINE__ + 1
226-
[1].map!.map { [1].map!.map { foo } }
227-
assert_equal("[\"#{__FILE__}:#{@line}:in 'Array#map!'\"]", @res)
226+
[1].map.map { [1].map.map { foo } }
227+
assert_equal("[\"#{__FILE__}:#{@line}:in 'Array#map'\"]", @res)
228228
end
229229

230230
def test_caller_location_path_cfunc_iseq_no_pc
231231
def self.foo
232232
@res = caller_locations(2, 1)[0].path
233233
end
234-
[1].map!.map { [1].map!.map { foo } }
234+
[1].map.map { [1].map.map { foo } }
235235
assert_equal(__FILE__, @res)
236236
end
237237

test/ruby/test_settracefunc.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,10 @@ def trace_by_set_trace_func
680680
#
681681
[:c_return, 1, "xyzzy", TracePoint, :trace, TracePoint, nil, nil],
682682
[:line, 4, 'xyzzy', self.class, method, self, :outer, :nothing],
683+
[:c_call, 4, 'xyzzy', Integer, :times, 1, nil, nil],
683684
[:line, 4, 'xyzzy', self.class, method, self, nil, :nothing],
684685
[:line, 5, 'xyzzy', self.class, method, self, :inner, :nothing],
686+
[:c_return, 4, "xyzzy", Integer, :times, 1, nil, nil],
685687
[:line, 7, 'xyzzy', self.class, method, self, :outer, :nothing],
686688
[:c_call, 7, "xyzzy", Class, :inherited, Object, nil, nil],
687689
[:c_return, 7, "xyzzy", Class, :inherited, Object, nil, nil],
@@ -1067,12 +1069,10 @@ def test_tracepoint_block
10671069
# pp events
10681070
# expected_events =
10691071
[[:b_call, :test_tracepoint_block, TestSetTraceFunc, TestSetTraceFunc, nil],
1070-
[:call, :map, Array, Array, nil],
1072+
[:c_call, :map, Array, Array, nil],
10711073
[:b_call, :test_tracepoint_block, TestSetTraceFunc, TestSetTraceFunc, nil],
10721074
[:b_return, :test_tracepoint_block, TestSetTraceFunc, TestSetTraceFunc, 3],
1073-
[:c_call, :<<, Array, Array, nil],
1074-
[:c_return, :<<, Array, Array, [3]],
1075-
[:return, :map, Array, Array, [3]],
1075+
[:c_return, :map, Array, Array, [3]],
10761076
[:call, :method_for_test_tracepoint_block, TestSetTraceFunc, TestSetTraceFunc, nil],
10771077
[:b_call, :test_tracepoint_block, TestSetTraceFunc, TestSetTraceFunc, nil],
10781078
[:b_return, :test_tracepoint_block, TestSetTraceFunc, TestSetTraceFunc, 4],
@@ -1387,10 +1387,9 @@ def test_a_call
13871387
}
13881388
}
13891389
assert_equal([
1390-
:b_call,
1391-
:call,
13921390
:b_call,
13931391
:c_call,
1392+
:b_call,
13941393
:call,
13951394
:b_call,
13961395
], events, "TracePoint log:\n#{ log.join("\n") }\n")
@@ -1414,7 +1413,6 @@ def test_a_return
14141413
assert_equal([
14151414
:b_return,
14161415
:c_return,
1417-
:return,
14181416
:b_return,
14191417
:return,
14201418
:b_return

0 commit comments

Comments
 (0)