Граф коммитов

1046 Коммитов

Автор SHA1 Сообщение Дата
Jeremy Evans ffd0820ab3 Deprecate taint/trust and related methods, and make the methods no-ops
This removes the related tests, and puts the related specs behind
version guards.  This affects all code in lib, including some
libraries that may want to support older versions of Ruby.
2019-11-18 01:00:25 +02:00
Ben Woosley bb71a128eb Prefer st_is_member over st_lookup with 0
The st_is_member DEFINE has simpler semantics, for more readable code.
2019-10-09 23:46:50 +09:00
Watson 2d001003e4 Improve performance of Array#sum with float elements (#1555)
The declaration of local variable in loop, it will initialize local variable for each run of the loop with clang generated code.
So, it shouldn't declare the local variable in heavy loop.

Array#sum with float elements will be faster around 30%.

* Before
       user     system      total        real
   3.320000   0.010000   3.330000 (  3.336088)

* After
       user     system      total        real
   2.590000   0.010000   2.600000 (  2.602399)

* Test code
require 'benchmark'

Benchmark.bmbm do |x|
  ary = []
  10000.times { ary << Random.rand }

  x.report do
    50000.times do
      ary.sum
    end
  end

end
2019-10-09 12:25:07 +09:00
Prajjwal Singh c8542ab484 Add: Array#intersection method 2019-10-07 15:59:12 +09:00
Yusuke Endoh 113bef6976 array.c (rb_mem_clear): remove "register" from arguments
to suppress the following warning:

```
compiling cxxanyargs.cpp
In file included from cxxanyargs.cpp:1:
In file included from ../../.././include/ruby/ruby.h:2150:
../../.././include/ruby/intern.h:56:19: warning: 'register' storage class specifier is deprecated and incompatible with C++17 [-Wdeprecated-register]
void rb_mem_clear(register VALUE*, register long);
                  ^~~~~~~~~
../../.././include/ruby/intern.h:56:36: warning: 'register' storage class specifier is deprecated and incompatible with C++17 [-Wdeprecated-register]
void rb_mem_clear(register VALUE*, register long);
                                   ^~~~~~~~~
```
2019-10-04 16:07:46 +09:00
Benoit Daloze ce477089f6 [DOC] Fix typos in Array#{to_s,inspect} doc [ci skip] 2019-09-29 11:04:28 +02:00
Nobuyoshi Nakada e2d97cffe5
[DOC] stated that Array#to_s calls #inspect [ci skip]
[ruby-list:50826]
2019-09-29 17:28:58 +09:00
Dylan Thacker-Smith a1fda16b23 Optimize Array#flatten and flatten! for already flattened arrays (#2495)
* Optimize Array#flatten and flatten! for already flattened arrays
* Add benchmark for Array#flatten and Array#flatten!

[Bug #16119]
2019-09-28 01:24:24 +09:00
Koichi Sasada 6d578164f5 check `ARY_SHARED_ROOT_P()`.
ARY_SHARED_ROOT_P(ary) is true, ARY_HEAP_CAPA(ary) should not
be called.
2019-09-25 17:12:55 +09:00
Koichi Sasada 3deeb3fd91 introduce `obj_ary_extracapa`.
Introduce a new debug counter `obj_ary_extracapa` which counts
arrays which are `len < capa`.
2019-09-25 17:01:54 +09:00
Nobuyoshi Nakada 422ae594d9
Fixed memory leak
* array.c (flatten): fix a memory leak in the case of an exception
  at conversion of an element to Array.
2019-09-20 10:49:49 +09:00
Kenichi Kamiya 9118cb242b Fix typos 2019-09-18 17:29:30 +09:00
Jeremy Evans 1994adf938 Make Array#uniq return subclass instance if called on subclass instance
Previously, Array#uniq would return subclass instance if the
length of the array were 2 or greater, and would return Array
instance if the length of the array were 0 or 1.

Fixes [Bug #7768]
2019-09-02 13:42:31 -07:00
Olivier Lacan 7c46aa6911 Avoid confusion in Array#- and Array#difference docs (#2070)
My previous attempt to correct #2068 apparently failed and the confusing
wording ("instances") was merged into trunk instead.

This should address any potential confusion.
2019-08-16 00:42:17 +09:00
John Hawthorn 9d298b9dab
Allow Array#join to allocate smaller strings
rb_str_buf_new always allocates at least 127 bytes of capacity, even
when less is requested.

    > ObjectSpace.dump(%w[a b c].join)
    {"address":"0x7f935f06ebf0", "type":"STRING", "class":"0x7f935d8b7bb0", "bytesize":3, "capacity":127, "value":"abc", "encoding":"UTF-8", "memsize":168, "flags":{"wb_protected":true}}

Instead, by using rb_str_new and then setting the length to 0, we can
allocate the exact amount of memory needed, without extra capacity.

    > ObjectSpace.dump(%w[a b c].join)
    {"address":"0x7f903fcab530", "type":"STRING", "class":"0x7f903f8b7988", "embedded":true, "bytesize":3, "value":"abc", "encoding":"UTF-8", "memsize":40, "flags":{"wb_protected":true}}
2019-08-09 16:33:53 -07:00
Yusuke Endoh f82633b6a6 array.c: gc.h is not needed 2019-08-07 22:56:52 +09:00
Kenichi Kamiya 0f601df22c
[Doc] Fix Array#to_h call-seq
Closes: https://github.com/ruby/ruby/pull/2254
2019-08-04 09:20:56 +09:00
Kenichi Kamiya 84829392f5
[Doc] Fix Array#difference call-seq
Closes: https://github.com/ruby/ruby/pull/2255
2019-08-04 09:20:29 +09:00
Yusuke Endoh 77bb79b8cf array.c: factor out a complex condition of assert
ARY_SHARED_P and ARY_EMBED_P included:

   assert(!FL_TEST((ary), ELTS_SHARED) || !FL_TEST((ary), RARRAY_EMBED_FLAG)),

The two predicate macros are used in many other assert conditions,
which caused memory bloat during C compilation.
This change factors out the assertion above to a function.
Now gcc consumes 160 MB instead of 250 MB to compile array.c.
2019-07-20 09:08:34 +09:00
Yusuke Endoh 0a16ff9f83 array.c: use assert in macro instead of in a function
The old code lost information of lineno.  Now, an assertion error will
output a correct lineno (but now gcc 8 requires 250 MB, unfortunately).
2019-07-19 23:50:30 +09:00
Yusuke Endoh 3e8d4ff3e5 array.c: factor out `assert(RB_TYPE_P(ary, T_ARRAY))` to a function
The assertion blows up gcc 8 by consuming approx. 1.8 GB memory.
This change reduces the amount of memory required to about 200 MB.

A follow-up of ae750799c1.
2019-07-19 23:41:24 +09:00
Koichi Sasada ae750799c1 Use FL_TEST_RAW() to check flags.
FL_TEST() uses FL_ABLE() which test data types. However,
in array.c we don't need to check it (all of them should be
T_ARRAY), so I changed from FL_TEST() to FL_TEST_RAW() which
does not check FL_ABLE(). Instead of FL_ABLE(), add assertion
to check given object is a T_ARRAY object.

For example, rb_ary_free() becomes slim:

with FL_TEST():
0000000000006a30 <rb_ary_free>:
    6a30:       40 f6 c7 07             test   $0x7,%dil
    6a34:       48 8b 07                mov    (%rdi),%rax
    6a37:       75 09                   jne    6a42 <rb_ary_free+0x12>
    6a39:       48 f7 c7 f7 ff ff ff    test   $0xfffffffffffffff7,%rdi
    6a40:       75 1e                   jne    6a60 <rb_ary_free+0x30>
    6a42:       a9 00 00 00 02          test   $0x2000000,%eax
    6a47:       74 07                   je     6a50 <rb_ary_free+0x20>
    6a49:       f3 c3                   repz retq
    6a4b:       0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)
    6a50:       48 8b 7f 20             mov    0x20(%rdi),%rdi
    6a54:       e9 00 00 00 00          jmpq   6a59 <rb_ary_free+0x29>
    6a59:       0f 1f 80 00 00 00 00    nopl   0x0(%rax)
    6a60:       89 c2                   mov    %eax,%edx
    6a62:       83 e2 1f                and    $0x1f,%edx
    6a65:       83 fa 1b                cmp    $0x1b,%edx
    6a68:       74 d8                   je     6a42 <rb_ary_free+0x12>
    6a6a:       f6 c4 60                test   $0x60,%ah
    6a6d:       74 d3                   je     6a42 <rb_ary_free+0x12>
    6a6f:       eb d8                   jmp    6a49 <rb_ary_free+0x19>```

with FL_TEST_RAW():
0000000000006a30 <rb_ary_free>:
    6a30:       48 f7 07 00 60 00 02    testq  $0x2006000,(%rdi)
    6a37:       74 07                   je     6a40 <rb_ary_free+0x10>
    6a39:       f3 c3                   repz retq
    6a3b:       0f 1f 44 00 00          nopl   0x0(%rax,%rax,1)
    6a40:       48 8b 7f 20             mov    0x20(%rdi),%rdi
    6a44:       e9 00 00 00 00          jmpq   6a49 <rb_ary_free+0x19>
2019-07-19 14:41:36 +09:00
git a44ad9a145 * expand tabs. 2019-07-19 13:10:08 +09:00
Koichi Sasada 182ae1407b fix shared array terminology.
Shared arrays created by Array#dup and so on points
a shared_root object to manage lifetime of Array buffer.
However, sometimes shared_root is called only shared so
it is confusing. So I fixed these wording "shared" to "shared_root".

* RArray::heap::aux::shared -> RArray::heap::aux::shared_root
* ARY_SHARED() -> ARY_SHARED_ROOT()
* ARY_SHARED_NUM() -> ARY_SHARED_ROOT_REFCNT()

Also, add some debug_counters to count shared array objects.

* ary_shared_create: shared ary by Array#dup and so on.
* ary_shared: finished in shard.
* ary_shared_root_occupied: shared_root but has only 1 refcnt.
  The number (ary_shared - ary_shared_root_occupied) is meaningful.
2019-07-19 13:07:59 +09:00
Koichi Sasada f6f09cbc76 introduce RUBY_ASSERT_ALWAYS(expr).
RUBY_ASSERT_ALWAYS(expr) ignores NDEBUG (we cannot remove this
assertion).
2019-07-15 10:21:41 +09:00
Jeremy Evans ced640951b Implement Array#minmax
Array#minmax was previous not implemented, so calling #minmax on
array was actually calling Enumerable#minmax.  This is a simple
implementation of #minmax by just calling rb_ary_min and
rb_ary_max, which improves performance significantly.

Fixes [Bug #15929]
2019-07-02 08:07:06 -07:00
Luke Gruber 97b4fe2ff6
array.c: Wrong heap size given to ruby_sized_xfree when freeing shared roots
Fixes [Bug #15953]

Closes: https://github.com/ruby/ruby/pull/2253
2019-06-24 13:34:30 +09:00
Luke Gruber 5a187e26ad array.c add back shared array optimization to ary_ensure_room_for_unshift
Bug fix in commit ec8e5f5aa6 [Bug #15952] disabled an
optimization in this function.

Closes: https://github.com/ruby/ruby/pull/2252
2019-06-23 13:17:41 +09:00
Luke Gruber ec8e5f5aa6
array.c: always check frozenness in Array#unshift. Fixes [Bug #15952]
Closes: https://github.com/ruby/ruby/pull/2251
2019-06-23 08:05:13 +09:00
Luke Gruber c033dc3073
Fix issue with Array#rindex when rb_equal modifies receiver array
Fixes [Bug #15951]

Closes: https://github.com/ruby/ruby/pull/2250
2019-06-23 00:49:24 +09:00
Martin Dürst 7f79a86d8b add comments to mention sort.reverse!
For array.c (Array#sort) and enum.c (Enumerable#sort_by),
add comments mentioning that sort.reverse! / sort_by { ... }.reverse!
can/should be used to reverse the result. [ci skip]
2019-06-13 15:30:03 +09:00
Koichi Sasada 0b9a7b3528 do not use RARRAY_SET() directly in array.c. 2019-05-21 15:02:54 +09:00
Benoit Daloze 87d2a2df1b Improve documentation of Array.try_convert
* Mostly to try the new git repository.
2019-04-27 16:43:28 +02:00
Kazuhiro NISHIYAMA 8c689e216f
Merge branch 'patch-5' of https://github.com/sos4nt/ruby into trunk
[Fix GH-2084]
2019-04-22 22:45:32 +09:00
nobu 3ee0648dc7 io.c: warn non-nil $,
* array.c (rb_ary_join_m): warn use of non-nil $,.

* io.c (rb_output_fs_setter): warn when set to non-nil value.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67606 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-04-18 21:56:55 +00:00
ktsj 9738f96fcf Introduce pattern matching [EXPERIMENTAL]
[ruby-core:87945] [Feature #14912]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67586 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-04-17 06:48:03 +00:00
nobu b9e52ef8b6 Adjusted styles
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67504 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-04-10 12:43:33 +00:00
kazu 25c1fd3b90 Reverting all commits from r67479 to r67496 because of CI failures
Because hard to specify commits related to r67479 only.
So please commit again.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67499 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-04-10 09:15:21 +00:00
nobu 5ae753dfa3 Adjusted styles
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67493 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-04-10 06:44:41 +00:00
kazu ddc5839005 Fix a typo [ci skip]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67389 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-03-31 14:46:25 +00:00
nobu 638c968582 array.c: [DOC] remove unnecessary markups [ci skip]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@67331 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-03-21 12:01:36 +00:00
Stefan Schüßler e19e5d2409
Fix return value name in docs for Array#union
Throughout the docs, `new_ary` is used to indicate a new array, whereas `ary` refers to the receiver.
2019-02-18 12:44:01 +01:00
nobu 10d85b19da Clarify Array#- and Array#difference documentation
Currently we are not explicit enough regarding the potentially confusing
behavior of `Array#-` and `Array#difference` when it comes to duplicate items
within receiver arrays.

Although the original documentation for these methods does use an array with
multiple instance of the same integers, the explanation for the behavior is
actually imprecise.

> removing any items that also appear in +other_ary+

Not only does `Array#-` remove any items that also appear in `other_ary` but
it also remove any instance of any item in `other_ary`.

One may expect `Array#-` to behave like mathematical subtraction or difference
when it doesn't. One could be forgiven to expect the following behavior:

```ruby
[1,1,2,2,3,3,4,4] - [1,2,3,4]
=> [1,2,3,4]
```

In reality this is the result:

```ruby
[1,1,2,2,3,3,4,4] - [1,2,3,4]
=> []
```

I hope that I've prevented this potential confusion with the clarifications
in this change. I can offer this as evidence of likeliness for confusion:
https://twitter.com/olivierlacan/status/1084930269533085696

I'll freely admit I was surprised by this behavior myself since I needed to
obtain an Array with only one instance of each item in the argument array
removed.

[Fix GH-2068] [ci skip]

From: Olivier Lacan <hi@olivierlacan.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66831 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-01-15 13:52:38 +00:00
nobu 78d6e33702 Fix styles [ci skip]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66762 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-01-09 13:58:49 +00:00
tenderlove 79c150f8a5 Mark array as "going to be modified" in `Array#reject!`
Before this patch, if `reject!` is called on a shared array it can
mutate the shared array rather than a copy.  This patch marks the array
as "going to be modified" so that the shared source array isn't
mutated.

[Bug #15479] [ruby-core:90781]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66756 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2019-01-08 19:22:05 +00:00
ko1 4c9f3ce7b1 fix marking T_NONE object bug.
* array.c (rb_ary_splice): do not use RARRAY_PTR() here because it can cause
  GC because of rb_ary_detransient(). Here ary can contain T_NONE object
  because of increasing capacity and not initialized yet.

  error log: http://ci.rvm.jp/results/trunk-test@ruby-sky1/1557174


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66513 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-23 14:58:41 +00:00
ko1 e4c79d0d10 rename li_table->ar_table (and related names).
* internal.h: rename the following names:
  * li_table -> ar_table. "li" means linear (from linear search),
    but we use the word "array" (from data layout).
  * RHASH_ARRAY -> RHASH_AR_TABLE. AR_TABLE is more clear.
  * rb_hash_array_* -> rb_hash_ar_table_*.
  * RHASH_TABLE_P() -> RHASH_ST_TABLE_P(). more clear.
  * RHASH_CLEAR() -> RHASH_ST_CLEAR().

* hash.c: rename "linear_" prefix functions to "ar_" prefix.

* hash.c (linear_init_table): rename to ar_alloc_table.

* debug_counter.h: rename obj_hash_array to obj_hash_ar.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66390 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-14 01:10:15 +00:00
nobu 78be4478d1 implement Array-specific #all?, #none?, #one?
Before this patch Array#all? was not implemented in Array class
and alternatively Enumerable#all? was used, while #any? has its
own method entry in Array class.  Similarly, Array#none? and #one?
also lacks its own implementation.
This patch provides Array-specific implementations for above three
methods to enable faster method lookup.

[Fix GH-2041]

From: Koji Onishi <fursich0@gmail.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66212 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-05 04:25:44 +00:00
nobu 09ef29a78f Prefer rb_check_arity when 0 or 1 arguments
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66205 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-05 01:09:44 +00:00
nobu c28c20ee88 Fix error message
* array.c (ary_take_first_or_last): expected optional argument.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66190 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-04 09:16:49 +00:00
nobu 4b85e88174 Prefer rb_check_arity when 0 or 1 arguments
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66179 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-04 02:24:15 +00:00
svn 888cdf605c * expand tabs.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66166 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-03 12:36:47 +00:00
ko1 5e11de6585 make `RARRAY_PTR_USE` more conservertive.
* include/ruby/ruby.h: de-transient at
  `RARRAY_PTR_USE` and `RARRAY_PTR_USE_START`.
  Introduce `RARRAY_PTR_USE_TRANSIENT` and
  `RARRAY_PTR_USE_START_TRANSIENT` if you don't want to
  de-transient an array. Generally, it is difficult
  so C-extension writers should not use them.

* array.c: use `RARRAY_PTR_USE_TRANSIENT` if possible.

* hash.c: ditto.

* enum.c (enum_sort_by): remove `rb_ary_transient_heap_evacuate()`
  because `RARRAY_PTR_USE` do de-transient.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66165 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-12-03 12:36:39 +00:00
tenderlove d46cd60f3c Use a shared array for the `duparray` instruction
In this example code:

```ruby
def foo
  [1, 2, 3, 4]
end
```

The array literal uses a `duparray` instruction. Before this patch,
`rb_ary_resurrect` would malloc and memcpy a new array buffer.  This
patch changes `rb_ary_resurrect` to use `ary_make_partial` so that the
new array object shares the underlying buffer with the array stored in
the instruction sequences.

Before this patch, the new array object is not shared:

```
$ ruby -r objspace -e'p ObjectSpace.dump([1, 2, 3, 4])'
"{\"address\":\"0x7fa2718372d0\", \"type\":\"ARRAY\", \"class\":\"0x7fa26f8b0010\", \"length\":4, \"memsize\":72, \"flags\":{\"wb_protected\":true}}\n"
```

After this patch:

```
$ ./ruby -r objspace -e'p ObjectSpace.dump([1, 2, 3, 4])'
"{\"address\":\"0x7f9a76883638\", \"type\":\"ARRAY\", \"class\":\"0x7f9a758af900\", \"length\":4, \"shared\":true, \"references\":[\"0x7f9a768837c8\"], \"memsize\":40, \"flags\":{\"wb_protected\":true}}\n"
```

[Feature #15289] [ruby-core:90097]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@66095 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-29 20:39:51 +00:00
svn 31649a24cf * expand tabs.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65924 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-22 09:18:43 +00:00
nobu b309975a91 Revert r63030
* array.c (rb_ary_collect): no longer splat sole array for lambda.
  [ruby-core:89734] [Bug #15285]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65923 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-22 09:18:40 +00:00
shyouhei 6bdca5d85e array.c: avoid (VALUE)--
This args[1]-- overflows when it is zero.  Should do that only
when we can say it is nonzero.




git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65798 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-19 06:48:09 +00:00
stomar 139d9d819c array.c: [DOC] add docs for Array#{filter,filter!}
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65528 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-04 11:40:32 +00:00
stomar bd20f031db array.c: [DOC] improve Array#{select,select!,keep_if} docs
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65527 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-04 11:39:28 +00:00
ko1 c39797e872 introduce USE_TRANSIENT_HEAP to enable/disable theap.
* include/ruby/ruby.h: intrdocue `USE_TRANSIENT_HEAP` macro
  to enable/disable transient heap.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65492 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-11-01 08:53:44 +00:00
svn d4f1187d31 * remove trailing spaces, expand tabs.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65455 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30 22:12:12 +00:00
ko1 8f675cdd00 support theap for T_HASH. [Feature #14989]
* hash.c, internal.h: support theap for small Hash.
  Introduce RHASH_ARRAY (li_table) besides st_table and small Hash
  (<=8 entries) are managed by an array data structure.
  This array data can be managed by theap.
  If st_table is needed, then converting array data to st_table data.

  For st_table using code, we prepare "stlike" APIs which accepts hash value
  and are very similar to st_ APIs.

  This work is based on the GSoC achievement
  by tacinight <tacingiht@gmail.com> and refined by ko1.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65454 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30 22:11:51 +00:00
svn 437392232a * expand tabs.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65450 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30 21:54:13 +00:00
ko1 312b105d0e introduce TransientHeap. [Bug #14858]
* transient_heap.c, transient_heap.h: implement TransientHeap (theap).
  theap is designed for Ruby's object system. theap is like Eden heap
  on generational GC terminology. theap allocation is very fast because
  it only needs to bump up pointer and deallocation is also fast because
  we don't do anything. However we need to evacuate (Copy GC terminology)
  if theap memory is long-lived. Evacuation logic is needed for each type.

  See [Bug #14858] for details.

* array.c: Now, theap for T_ARRAY is supported.

  ary_heap_alloc() tries to allocate memory area from theap. If this trial
  sccesses, this array has theap ptr and RARRAY_TRANSIENT_FLAG is turned on.
  We don't need to free theap ptr.

* ruby.h: RARRAY_CONST_PTR() returns malloc'ed memory area. It menas that
  if ary is allocated at theap, force evacuation to malloc'ed memory.
  It makes programs slow, but very compatible with current code because
  theap memory can be evacuated (theap memory will be recycled).

  If you want to get transient heap ptr, use RARRAY_CONST_PTR_TRANSIENT()
  instead of RARRAY_CONST_PTR(). If you can't understand when evacuation
  will occur, use RARRAY_CONST_PTR().

(re-commit of r65444)


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65449 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30 21:53:56 +00:00
svn 69b8ffcd5b * expand tabs.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65448 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30 21:02:12 +00:00
ko1 7d359f9b69 revert r65444 and r65446 because of commit miss
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65447 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30 21:01:55 +00:00
svn f926f799e9 * expand tabs.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65445 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30 20:46:47 +00:00
ko1 90ac549fa6 introduce TransientHeap. [Bug #14858]
* transient_heap.c, transient_heap.h: implement TransientHeap (theap).
  theap is designed for Ruby's object system. theap is like Eden heap
  on generational GC terminology. theap allocation is very fast because
  it only needs to bump up pointer and deallocation is also fast because
  we don't do anything. However we need to evacuate (Copy GC terminology)
  if theap memory is long-lived. Evacuation logic is needed for each type.

  See [Bug #14858] for details.

* array.c: Now, theap for T_ARRAY is supported.

  ary_heap_alloc() tries to allocate memory area from theap. If this trial
  sccesses, this array has theap ptr and RARRAY_TRANSIENT_FLAG is turned on.
  We don't need to free theap ptr.

* ruby.h: RARRAY_CONST_PTR() returns malloc'ed memory area. It menas that
  if ary is allocated at theap, force evacuation to malloc'ed memory.
  It makes programs slow, but very compatible with current code because
  theap memory can be evacuated (theap memory will be recycled).

  If you want to get transient heap ptr, use RARRAY_CONST_PTR_TRANSIENT()
  instead of RARRAY_CONST_PTR(). If you can't understand when evacuation
  will occur, use RARRAY_CONST_PTR().


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65444 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30 20:46:24 +00:00
ko1 54726befc3 use RARRAY_AREF() instead of RARRAY_CONST_PTR().
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65438 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-30 03:21:56 +00:00
stomar af7f9de4b9 array.c, file.c, string.c: [DOC] fix typos
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65185 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-19 21:35:51 +00:00
stomar 1411b7f934 array.c: improve docs for Array#difference
* array.c: [DOC] small doc fixes for Array#difference and Array#-.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65184 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-19 21:26:15 +00:00
stomar 921dc157d8 array.c: fix rdoc syntax
* array.c: [DOC] use `<code>other_ary</code>s' instead of `+other_ary+s',
  which is not rendered correctly.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@65066 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-13 19:01:22 +00:00
ko1 4ed087b0db revisit `RARRAY_PTR()`.
* array.c (yield_indexed_values): use RARRAY_AREF/ASET instead of
  using RARRAY_PTR().

* enum.c (nmin_filter): ditto.

* proc.c (rb_sym_to_proc): ditto.

* enum.c (rb_nmin_run): use RARRAY_PTR_USE() instead of RARRAY_PTR().
  It is safe because they don't make new referecen from an array.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64986 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-10 04:17:01 +00:00
ko1 165b446166 revisit `RARRAY_PTR()`.
* array.c (ary_memcpy0): remove traditional `RARRAY_PTR()` code.
  It's enough stable.

* array.c (rb_ary_splice): add comment about wb-unprotect.

* array.c (rotate_count): use `RARRAY_PTR_USE()` instead of
  `RARRAY_PTR()` to avoid wb-unprotect.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64985 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-10 03:52:20 +00:00
kazu 1c7d334800 Fix indent of output in doc [ci skip]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64930 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-07 04:21:32 +00:00
svn d7bd45897b * expand tabs.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64922 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-05 06:23:37 +00:00
nobu d65d5533ab Add difference method to Array
I introduce a `difference` method equivalent to the `-` operator, but
which accept more than array as argument. This improved readability, and
it is also coherent with the `+` operator, which has a similar `concat`
method. The method doesn't modify the original object and return a new
object instead. I plan to introduce a `difference!` method as well.

Tests and documentation are included.

It solves partially https://bugs.ruby-lang.org/issues/14097

From: Ana María Martínez Gómez <ammartinez@suse.de>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64921 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-10-05 06:23:34 +00:00
kazu 2617dfa9bd Fix sample code [ci skip]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64805 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-21 08:53:11 +00:00
nobu abe75149d1 Enumerable#to_h with block and so on
[Feature #15143]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64794 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-20 15:06:56 +00:00
nobu ce079f1651 Introduce rb_ary_union_hash method in Array
Avoid repeating code and improve readability in `rb_ary_or` and
`rb_ary_union_multi`. Similaty as done with `rb_ary_union`.

[Fix GH-1747] [Feature #14097]

From: Ana María Martínez Gómez <ammartinez@suse.de>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64790 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-20 03:18:54 +00:00
nobu 4c08223945 Link Array#union from | method
`Array#uniq` is not really related with `Array#|`, so I replaced it by
`Array#union`.

[Fix GH-1747] [Feature #14097]

From: Ana María Martínez Gómez <ammartinez@suse.de>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64789 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-20 03:18:54 +00:00
nobu d0f9184f8b Introduce rb_ary_union method in Array
Avoid repeating code and improve readability in `rb_ary_or` and
`rb_ary_union_multi`.

[Fix GH-1747] [Feature #14097]

From: Ana María Martínez Gómez <ammartinez@suse.de>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64788 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-20 03:18:53 +00:00
nobu 744e816f55 Add union method to Array
I introduce a `union` method equivalent to the `|` operator, but which
accept more than array as argument. This improved readability, and it
is also coherent with the `+` operator, which has a similar `concat`
method. The method doesn't modify the original object and return a new
object instead. It is plan to introduce a `union!` method as well.

Tests and documentation are included.

It solves partially https://bugs.ruby-lang.org/issues/14097

[Fix GH-1747] [Feature #14097]

From: Ana María Martínez Gómez <ammartinez@suse.de>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64787 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-20 03:18:52 +00:00
marcandre 4452e857d4 array.c: Optimize rb_ary_and. Patch by Stefan Schüßler. [Fix GH-1938]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64755 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-16 02:42:26 +00:00
nobu 384fda18b8 warn unused blocks with Enumerable#all? any? one? none?
[Fix GH-1953]

From: Koji Onishi <fursich0@gmail.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64733 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-09-13 11:10:24 +00:00
ko1 1f4efb9aed rest parameter optimization [Feature #15010]
* vm_args.c: rb_ary_dup(args->rest) to be used at most once during
  parameter setup. [Feature #15010]
  A patch by chopraanmol1 (Anmol Chopra) <chopraanmol1@gmail.com>.

* array.c (rb_ary_behead): added to remove first n elements.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64583 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-08-28 07:06:06 +00:00
nobu 67cacdb836 fix sum on infinity
* array.c (rb_ary_sum): consider non-finite floats.
  [ruby-core:88024] [Bug #14926]

* enum.c (sum_iter): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@64014 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-22 10:47:33 +00:00
nobu dae0f4388e Make block spacing consistent
[Fix GH-1910] [ci skip]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63951 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-11 09:56:49 +00:00
kazu cede4ce997 Fix a typo [ci skip]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63814 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-07-01 01:38:18 +00:00
ktsj d62b7e133e [DOC] Add call signature for {Array,Hash}#any?
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63729 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-06-23 04:57:49 +00:00
normal b0200a5e36 array.c: use ruby_sized_free and SIZED_REALLOC_N
Part of the plan to reduce dependencies on malloc_usable_size
which costs us speed: https://bugs.ruby-lang.org/issues/10238

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63481 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-05-21 21:21:23 +00:00
shyouhei 841d5ae80e rb_ary_dig, rb_hash_dig: nobody is using them outside.
mark them static.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63358 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-05-08 09:09:12 +00:00
nobu e42f4ae773 array.c: yield blockarg in collect
* array.c (rb_ary_collect): yield in block argument semantics
  always to splat array elements to lambda, for the backward
  compatibility.  [ruby-core:86362] [Bug #14639]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@63030 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-03-29 00:05:45 +00:00
eregon b1a8c64483 Add a new #filter alias for #select
* In Enumerable, Enumerator::Lazy, Array, Hash and Set
  [Feature #13784] [ruby-core:82285]
* Share specs for the various #select#select! methods and
  reuse them for #filter/#filter!.
* Add corresponding filter tests for select tests.
* Update NEWS.

[Fix GH-1824]

From: Alexander Patrick <adp90@case.edu>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62575 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-25 13:52:07 +00:00
nobu 623160916d Improve Array#sample with random documentation
* array.c (rb_ary_sample): Adds examples for the use of the
  optional parameter random for Array#sample and unifies the style
  with the documentation of Array#shuffle.

[Fix GH-1825]

From: Alberto Almagro <alberto.almagro@rakuten.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62553 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-24 01:12:51 +00:00
nobu 41e9e19bc6 Array#values_at optimization
* array.c (rb_ary_values_at): optimization range argument case.
  bulk concatenation than pushing for each element.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62413 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-15 10:00:57 +00:00
k0kubun 1fd816803b vm_insnhelper.c: inline array aref with integer
internal.h: define inlinable rb_ary_entry_internal.

array.c: use rb_ary_entry_internal.

* Benchmark
ruby --jit mame/optcarrot/bin/optcarrot --benchmark mame/optcarrot/examples/Lan_Master.nes

** Before

checksum: 59662
fps: 58.095175012159686

** After

fps: 59.874751599221526
checksum: 59662

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62388 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-12 15:25:58 +00:00
k0kubun ed935aa5be mjit_compile.c: merge initial JIT compiler
which has been developed by Takashi Kokubun <takashikkbn@gmail> as
YARV-MJIT. Many of its bugs are fixed by wanabe <s.wanabe@gmail.com>.

This JIT compiler is designed to be a safe migration path to introduce
JIT compiler to MRI. So this commit does not include any bytecode
changes or dynamic instruction modifications, which are done in original
MJIT.

This commit even strips off some aggressive optimizations from
YARV-MJIT, and thus it's slower than YARV-MJIT too. But it's still
fairly faster than Ruby 2.5 in some benchmarks (attached below).

Note that this JIT compiler passes `make test`, `make test-all`, `make
test-spec` without JIT, and even with JIT. Not only it's perfectly safe
with JIT disabled because it does not replace VM instructions unlike
MJIT, but also with JIT enabled it stably runs Ruby applications
including Rails applications.

I'm expecting this version as just "initial" JIT compiler. I have many
optimization ideas which are skipped for initial merging, and you may
easily replace this JIT compiler with a faster one by just replacing
mjit_compile.c. `mjit_compile` interface is designed for the purpose.

common.mk: update dependencies for mjit_compile.c.

internal.h: declare `rb_vm_insn_addr2insn` for MJIT.

vm.c: exclude some definitions if `-DMJIT_HEADER` is provided to
compiler. This avoids to include some functions which take a long time
to compile, e.g. vm_exec_core. Some of the purpose is achieved in
transform_mjit_header.rb (see `IGNORED_FUNCTIONS`) but others are
manually resolved for now. Load mjit_helper.h for MJIT header.
mjit_helper.h: New. This is a file used only by JIT-ed code. I'll
refactor `mjit_call_cfunc` later.
vm_eval.c: add some #ifdef switches to skip compiling some functions
like Init_vm_eval.

win32/mkexports.rb: export thread/ec functions, which are used by MJIT.

include/ruby/defines.h: add MJIT_FUNC_EXPORTED macro alis to clarify
that a function is exported only for MJIT.

array.c: export a function used by MJIT.
bignum.c: ditto.
class.c: ditto.
compile.c: ditto.
error.c: ditto.
gc.c: ditto.
hash.c: ditto.
iseq.c: ditto.
numeric.c: ditto.
object.c: ditto.
proc.c: ditto.
re.c: ditto.
st.c: ditto.
string.c: ditto.
thread.c: ditto.
variable.c: ditto.
vm_backtrace.c: ditto.
vm_insnhelper.c: ditto.
vm_method.c: ditto.

I would like to improve maintainability of function exports, but I
believe this way is acceptable as initial merging if we clarify the
new exports are for MJIT (so that we can use them as TODO list to fix)
and add unit tests to detect unresolved symbols.
I'll add unit tests of JIT compilations in succeeding commits.

Author: Takashi Kokubun <takashikkbn@gmail.com>
Contributor: wanabe <s.wanabe@gmail.com>

Part of [Feature #14235]

---

* Known issues
  * Code generated by gcc is faster than clang. The benchmark may be worse
    in macOS. Following benchmark result is provided by gcc w/ Linux.
  * Performance is decreased when Google Chrome is running
  * JIT can work on MinGW, but it doesn't improve performance at least
    in short running benchmark.
  * Currently it doesn't perform well with Rails. We'll try to fix this
    before release.

---

* Benchmark reslts

Benchmarked with:
Intel 4.0GHz i7-4790K with 16GB memory under x86-64 Ubuntu 8 Cores

- 2.0.0-p0: Ruby 2.0.0-p0
- r62186: Ruby trunk (early 2.6.0), before MJIT changes
- JIT off: On this commit, but without `--jit` option
- JIT on: On this commit, and with `--jit` option

** Optcarrot fps

Benchmark: https://github.com/mame/optcarrot

|         |2.0.0-p0 |r62186   |JIT off  |JIT on   |
|:--------|:--------|:--------|:--------|:--------|
|fps      |37.32    |51.46    |51.31    |58.88    |
|vs 2.0.0 |1.00x    |1.38x    |1.37x    |1.58x    |

** MJIT benchmarks

Benchmark: https://github.com/benchmark-driver/mjit-benchmarks
(Original: https://github.com/vnmakarov/ruby/tree/rtl_mjit_branch/MJIT-benchmarks)

|           |2.0.0-p0 |r62186   |JIT off  |JIT on   |
|:----------|:--------|:--------|:--------|:--------|
|aread      |1.00     |1.09     |1.07     |2.19     |
|aref       |1.00     |1.13     |1.11     |2.22     |
|aset       |1.00     |1.50     |1.45     |2.64     |
|awrite     |1.00     |1.17     |1.13     |2.20     |
|call       |1.00     |1.29     |1.26     |2.02     |
|const2     |1.00     |1.10     |1.10     |2.19     |
|const      |1.00     |1.11     |1.10     |2.19     |
|fannk      |1.00     |1.04     |1.02     |1.00     |
|fib        |1.00     |1.32     |1.31     |1.84     |
|ivread     |1.00     |1.13     |1.12     |2.43     |
|ivwrite    |1.00     |1.23     |1.21     |2.40     |
|mandelbrot |1.00     |1.13     |1.16     |1.28     |
|meteor     |1.00     |2.97     |2.92     |3.17     |
|nbody      |1.00     |1.17     |1.15     |1.49     |
|nest-ntimes|1.00     |1.22     |1.20     |1.39     |
|nest-while |1.00     |1.10     |1.10     |1.37     |
|norm       |1.00     |1.18     |1.16     |1.24     |
|nsvb       |1.00     |1.16     |1.16     |1.17     |
|red-black  |1.00     |1.02     |0.99     |1.12     |
|sieve      |1.00     |1.30     |1.28     |1.62     |
|trees      |1.00     |1.14     |1.13     |1.19     |
|while      |1.00     |1.12     |1.11     |2.41     |

** Discourse's script/bench.rb

Benchmark: https://github.com/discourse/discourse/blob/v1.8.7/script/bench.rb

NOTE: Rails performance was somehow a little degraded with JIT for now.
We should fix this.
(At least I know opt_aref is performing badly in JIT and I have an idea
 to fix it. Please wait for the fix.)

*** JIT off
Your Results: (note for timings- percentile is first, duration is second in millisecs)

categories_admin:
  50: 17
  75: 18
  90: 22
  99: 29
home_admin:
  50: 21
  75: 21
  90: 27
  99: 40
topic_admin:
  50: 17
  75: 18
  90: 22
  99: 32
categories:
  50: 35
  75: 41
  90: 43
  99: 77
home:
  50: 39
  75: 46
  90: 49
  99: 95
topic:
  50: 46
  75: 52
  90: 56
  99: 101

*** JIT on
Your Results: (note for timings- percentile is first, duration is second in millisecs)

categories_admin:
  50: 19
  75: 21
  90: 25
  99: 33
home_admin:
  50: 24
  75: 26
  90: 30
  99: 35
topic_admin:
  50: 19
  75: 20
  90: 25
  99: 30
categories:
  50: 40
  75: 44
  90: 48
  99: 76
home:
  50: 42
  75: 48
  90: 51
  99: 89
topic:
  50: 49
  75: 55
  90: 58
  99: 99

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62197 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-04 11:22:28 +00:00
normal 7fdb4099cd array.c: remove rb_ary_frozen_p / Array#frozen?
This is redundant since r15206 /
ffe425ecaa as we no longer lock
the array during sort.  Instead, fall back to Object#frozen?

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62154 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-02-01 09:20:38 +00:00
nobu d0fa578cdc array.c: rb_check_to_array
* array.c (rb_check_to_array): conversion to array by to_a method.
  returns nil if not possible.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@62072 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-27 09:27:47 +00:00
shyouhei 26b08ed144 don't abuse RSTRING_PTR (2nd try)
r61827, r61947 was about to fix this. The proper way to allocate
memory region is called ALLOCV_N.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61949 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-19 08:17:56 +00:00
shyouhei 66bd6ffcbc svn merge -c -61947 .
Previous commit fails in CI.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61948 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-19 07:16:54 +00:00
shyouhei 40b95f0856 don't abuse RSTRING_PTR
r61827 was about to fix this. The proper way to allocate memory
region is called ALLOCV_N.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61947 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-19 07:07:49 +00:00
shyouhei 3ca0948f68 INFINITY is float. That of double is HUGE_VAL.
It seems HUGE_VAL is already used. Why not eliminate INTINITY.
NAN is also float. That of double is called nan(). This is also
fixed.

Signed-off-by: Urabe, Shyouhei <shyouhei@ruby-lang.org>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61938 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-19 01:45:36 +00:00
kazu 646db52f3a [DOC] Add `Array#{append,prepend}` to call-seq [ci skip]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61934 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-18 12:54:18 +00:00
nobu e9cb552ec9 internal.h: remove dependecy on ruby/encoding.h
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61713 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-09 06:24:11 +00:00
shyouhei e5914dd479 fix memory leak (FOUND BY A COMPILER WARNING)
Confusion of argument order ignores this st_free_table.
Results in garbaged table not GCed.
Easily noticable when you read the compiper warnings.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61561 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2018-01-02 06:41:57 +00:00
nobu 0d09ee1e73 Improve Array#- efficiency [Fixes GH-1756]
When doing the difference of a small array with a big one it is not
efficient in both time and memory to convert the second one to a hash.

From: Ana María Martínez Gómez <ammartinez@suse.de>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61330 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-12-19 01:08:52 +00:00
marcandre a9770bac63 Add case equality arity to Enumerable#all?, any?, none? and one?,
and specialized Array#any? and Hash#any?
Based on patch by D.E. Akers [#11286]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@61098 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-12-10 22:36:28 +00:00
nobu 6b818dd961 common conversion functions
* array.c (rb_to_array_type): make public to share common code
  internally.

* hash.c (rb_to_hash_type): make public to share common code
  internally.

* symbol.c (rb_to_symbol_type): make public to share common code
  internally.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60438 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-26 07:23:23 +00:00
kazu 872b9ec896 Fix `shadowing outer local variable` warning [ci skip]
ref https://github.com/rurema/doctree/pull/697

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60426 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-25 12:57:34 +00:00
nobu e480660b85 vm_insnhelper.c: array aref optimization
* vm_insnhelper.c (vm_opt_aref): optimize on other than fixnum
  argument too.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60333 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-10-22 00:19:12 +00:00
glass 8efc41329d hash.c: use rb_hash_new_with_size()
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60078 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-30 13:14:09 +00:00
nobu 9bc73cd81f array.c: improve operations on small arrays
[Feature #13884]

Reduce number of memory allocations for "and", "or" and "diff"
operations on small arrays

Very often, arrays are used to filter parameters and to select
interesting items from 2 collections and very often these
collections are small enough, for example:

```ruby
SAFE_COLUMNS = [:id, :title, :created_at]

def columns
  @all_columns & SAFE_COLUMNS
end
```

In this patch, I got rid of unnecessary memory allocations for
small arrays when "and", "or" and "diff" operations are performed.

name             | HEAD  | PATCH
-----------------+------:+------:
array_small_and  | 0.615 | 0.263
array_small_diff | 0.676 | 0.282
array_small_or   | 0.953 | 0.463

name             | PATCH
-----------------+------:
array_small_and  | 2.343
array_small_diff | 2.392
array_small_or   | 2.056

name             | HEAD  | PATCH
-----------------+------:+------:
array_small_and  | 1.429 | 1.005
array_small_diff | 1.493 | 0.878
array_small_or   | 1.672 | 1.152

name             | PATCH
-----------------+------:
array_small_and  | 1.422
array_small_diff | 1.700
array_small_or   | 1.452

Author:    Dmitry Bochkarev <dimabochkarev@gmail.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60057 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-29 07:43:22 +00:00
duerst dbb6e0a47c improve grammar in documentation of Array#bsearch [ci skip]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60045 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-09-27 07:47:13 +00:00
nobu f0ae63b072 array.c: refine binomial_coefficient
* array.c (binomial_coefficient): get rid of bignums by division
  after each multiplications.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59692 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-08-30 08:26:16 +00:00
nobu 96223329d0 array.c: refine descending_factorial
* array.c (descending_factorial): reduce factorial multipication.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59691 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-08-30 07:55:19 +00:00
nobu 56ce54608c array.c: integer calculations
* array.c (rb_ary_cycle_size, descending_factorial): use
  rb_int_mul instead of rb_funcallv.

* array.c (binomial_coefficient): use rb_int_idiv instead of
  rb_funcallv.

* array.c (rb_ary_repeated_permutation_size): use
  rb_int_positive_pow.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59689 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-08-30 05:36:59 +00:00
nobu 4b9aeef1fa array.c: use rb_check_array_type
* array.c (ary_join_1): simplified by rb_check_array_type.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59688 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-08-29 12:13:37 +00:00
nobu 26a9bf756b array.c: nested encoding
* array.c (ary_join_1): ignore encodings in nested arrays as an
  initial encoding.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59685 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-08-29 11:35:35 +00:00
nobu cb70a92ece array.c: join encoding
* array.c (ary_join_1): copy the encoding of the converted string
  of the first element by to_str too, as an initial encoding.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59684 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-08-29 10:49:40 +00:00
kazu 40dad62c05 Fix warning: shadowing outer local variable - a
[ci skip][Fix GH-1628]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59338 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-07-15 04:35:18 +00:00
normal 940fa3b824 array.c: more predefined IDs
* array.c (id_cmp): change to macro for OPTIMIZED_CMP
  (rb_ary_repeated_permutation_size): s/id_power/idPow/
  (Init_Array): remove id_cmp and id_power

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59210 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-06-29 22:40:36 +00:00
k0kubun a100bf321c array.c: [DOC] Make it clear that #<< modifies receiver
This patch is sent from @selmertsx (morioka shuhei).

[fix GH-1646]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59070 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-06-12 11:09:10 +00:00
stomar 7c8a060b85 array.c: docs for Array#{sort,sort!}
* array.c: [DOC] make example in the docs for Array#{sort,sort!}
  match the call-seq and description by using the same block vars.
  Based on a patch by Roque Pinel (repinel).  [Fix GH-1628]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@59011 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-06-03 20:17:00 +00:00
watson1978 d0015e4ac6 Improve performance of implicit type conversion
To convert the object implicitly, it has had two parts in convert_type() which are
  1. lookink up the method's id
  2. calling the method

Seems that strncmp() and strcmp() in convert_type() are slightly heavy to look up
the method's id for type conversion.

This patch will add and use internal APIs (rb_convert_type_with_id, rb_check_convert_type_with_id)
to call the method without looking up the method's id when convert the object.

Array#flatten -> 19 % up
Array#+       ->  3 % up

[ruby-dev:50024] [Bug #13341] [Fix GH-1537]

### Before
       Array#flatten    104.119k (± 1.1%) i/s -    525.690k in   5.049517s
             Array#+      1.993M (± 1.8%) i/s -     10.010M in   5.024258s

### After
       Array#flatten    124.005k (± 1.0%) i/s -    624.240k in   5.034477s
             Array#+      2.058M (± 4.8%) i/s -     10.302M in   5.019328s

### Test Code
require 'benchmark/ips'

class Foo
  def to_ary
    [1,2,3]
  end
end

Benchmark.ips do |x|

  ary = []
  100.times { |i| ary << i }
  array = [ary]

  x.report "Array#flatten" do |i|
    i.times { array.flatten }
  end

  x.report "Array#+" do |i|
    obj = Foo.new
    i.times { array + obj }
  end

end

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58978 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-31 12:30:57 +00:00
watson1978 6270d59508 Improve Array#concat performance if only one argument is given
* array.c (rb_ary_concat_multi): concatenate the array without generating
    temporary Array object if only one argument is given.
    This is very similar with r58886.

    Array#concat will be faster around 19%.
    [Fix GH-1634]

### Before
        Array#concat      2.187M (± 3.5%) i/s -     10.926M in   5.002829s

### After
        Array#concat      2.598M (± 1.8%) i/s -     13.008M in   5.008201s

### Test code
require 'benchmark/ips'

Benchmark.ips do |x|
  x.report "Array#concat" do |i|
    other = [4]
    i.times { [1, 2, 3].concat(other) }
  end
end

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58909 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-26 17:10:01 +00:00
watson1978 6e0e067de3 Improve performance of rb_equal()
* object.c (rb_equal): add optimized path to compare the objects using
    rb_equal_opt(). Previously, if not same objects were given, rb_equal() would
    call `==' method via rb_funcall() which took a long time.

    rb_equal_opt() has provided faster comparing for Fixnum/Float/String objects.
    Now, Time#eql? uses rb_equal() to compare with argument object and it will
    be faster around 40% on 64-bit environment.

* array.c (rb_ary_index): remove redundant rb_equal_opt() calling.
    Now, rb_equal() was optimized using rb_equal_opt().
    If rb_equal_opt() returns Qundef, it will invoke rb_equal() -> rb_equal_opt(),
    and it will cause the performance regression.

    So, this patch will remove first redundant rb_equal_opt() calling.

* array.c (rb_ary_rindex): ditto.
* array.c (rb_ary_includes): ditto.

    [ruby-core:80360] [Bug #13365] [Fix GH-#1552]

### Before
Time#eql? with other      7.309M (± 1.4%) i/s -     36.647M in   5.014964s
    Array#index(val)      1.433M (± 1.2%) i/s -      7.207M in   5.030942s
   Array#rindex(val)      1.418M (± 1.6%) i/s -      7.103M in   5.009164s
 Array#include?(val)      1.451M (± 0.9%) i/s -      7.295M in   5.026392s

### After
Time#eql? with other     10.321M (± 1.9%) i/s -     51.684M in   5.009203s
    Array#index(val)      1.474M (± 0.9%) i/s -      7.433M in   5.044384s
   Array#rindex(val)      1.449M (± 1.7%) i/s -      7.292M in   5.034436s
 Array#include?(val)      1.466M (± 1.7%) i/s -      7.373M in   5.030047s

### Test code
require 'benchmark/ips'

Benchmark.ips do |x|
  t1 = Time.now
  t2 = Time.now

  x.report "Time#eql? with other" do |i|
    i.times { t1.eql?(t2) }
  end

  # Benchmarks to check whether it didn't introduce the regression
  obj = Object.new
  x.report "Array#index(val)" do |i|
    ary = [1, 2, true, false, obj]
    i.times { ary.index(obj) }
  end

  x.report "Array#rindex(val)" do |i|
    ary = [1, 2, true, false, obj].reverse
    i.times { ary.rindex(obj) }
  end

  x.report "Array#include?(val)" do |i|
    ary = [1, 2, true, false, obj]
    i.times { ary.include?(obj) }
  end
end

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58880 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-25 04:25:37 +00:00
watson1978 044257c046 Improve performance in where push the element into non shared Array object
* array.c (ary_ensure_room_for_push): use rb_ary_modify_check() instead of
    rb_ary_modify() to check whether the object can be modified for non shared
    Array object. rb_ary_modify() has the codes for shared Array object too.
    In here, it has condition branch for shared / non shared Array object and
    it can use rb_ary_modify_check() which is smaller function than
    rb_ary_modify() for non shared object.

    rb_ary_modify_check() will be expand as inline function.
    If it will compile with GCC, Array#<< will be faster around 8%.

    [ruby-core:81082] [Bug #13553] [Fix GH-1609]

## Clang 802.0.42
### Before
            Array#<<      9.353M (± 1.7%) i/s -     46.787M in   5.004123s
          Array#push      7.702M (± 1.1%) i/s -     38.577M in   5.009338s
     Array#values_at      6.133M (± 1.9%) i/s -     30.699M in   5.007772s

### After
            Array#<<      9.458M (± 2.0%) i/s -     47.357M in   5.009069s
          Array#push      7.921M (± 1.8%) i/s -     39.665M in   5.009151s
     Array#values_at      6.377M (± 2.3%) i/s -     31.881M in   5.001888s

### Result
Array#<<        -> 1.2% faster
Array#push      -> 2.8% faster
Array#values_at -> 3.9% faster

## GCC 7.1.0
### Before
            Array#<<     10.497M (± 1.1%) i/s -     52.665M in   5.017601s
          Array#push      8.527M (± 1.6%) i/s -     42.777M in   5.018003s
     Array#values_at      7.621M (± 1.7%) i/s -     38.152M in   5.007910s

### After
            Array#<<     11.403M (± 1.3%) i/s -     57.028M in   5.001849s
          Array#push      8.924M (± 1.3%) i/s -     44.609M in   4.999940s
     Array#values_at      8.291M (± 1.4%) i/s -     41.487M in   5.004727s

### Result
Array#<<        -> 8.3% faster
Array#push      -> 4.3% faster
Array#values_at -> 8.7% faster

## Test code
require 'benchmark/ips'

Benchmark.ips do |x|

  x.report "Array#<<" do |i|
    i.times { [1,2] << 3 }
  end

  x.report "Array#push" do |i|
    i.times { [1,2].push(3) }
  end

  x.report "Array#values_at" do |i|
    ary = [1, 2, 3, 4, 5]
    i.times { ary.values_at(0, 2, 4) }
  end

end

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58867 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-24 06:57:08 +00:00
ko1 9e1624cfe8 Add debug counters.
* debug_counter.h: add the following counters to measure object types.
  obj_free: freed count
  obj_str_ptr: freed count of Strings they have extra buff.
  obj_str_embed: freed count of Strings they don't have extra buff.
  obj_str_shared: freed count of Strings they have shared extra buff.
  obj_str_nofree: freed count of Strings they are marked as nofree.
  obj_str_fstr: freed count of Strings they are marked as fstr.
  obj_ary_ptr: freed count of Arrays they have extra buff.
  obj_ary_embed: freed count of Arrays they don't have extra buff.
  obj_obj_ptr: freed count of Objects (T_OBJECT) they have extra buff.
  obj_obj_embed: freed count of Objects they don't have extra buff.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58865 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-24 06:46:44 +00:00
nobu 34761b7f96 array.c: fix position in message
* array.c (rb_ary_insert): fix the position in error message, when
  it is less than -1.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58694 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-12 17:23:46 +00:00
nobu e95cac18bd array.c: check position to insert
* array.c (rb_ary_insert): check position to insert even if no
  elements to be inserted.  [ruby-core:81125] [Bug #13558]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58693 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-05-12 17:02:48 +00:00
shyouhei 5069122ab6 refactor torexp to use routine in array.c
Found a part where copy&paste can be eliminated.  Reduces vm_exec_core
from 26,228 bytes to 26,176 bytes in size on my machine. I believe it
does not affect any runtime performance.

----

	* array.c (rb_ary_tmp_new_from_values): extend existing
          rb_ary_new_from_values function so that it can take
          additional value for klass.
	* array.c (rb_ary_new_from_values): use the new function.
	* insns.def (toregexp): ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58416 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-04-20 10:32:08 +00:00
mrkn cb8012f5c0 array.c: improve performance of Array#sort with block
* array.c (sort_1): improve performance of Array#sort with block

* benchmark/bm_array_sort_block.rb: added for Array#sort with block

[Bug #13344]
[ruby-dev:50027]
[Fix GH-1544]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58339 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-04-13 09:25:38 +00:00
mrkn 1b5acc876b array.c: Improve performance of Array#sort with float elements
* array.c (sort_2): improve performance of Array#sort with float elements.

* internal.h (cmp_opt_Float, cmp_opt_data): added for checking whether or not
  Float#<=> can be optimizable.

* numeric.c (rb_float_cmp): added for internal use.

* internal.h (rb_float_cmp): ditto.

[Bug #13340]
[ruby-dev:50023]
[Fix GH-1539]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58333 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-04-13 07:22:35 +00:00
rhe 4e85feed33 array.c: report correct memsize for shared root arrays
For a shared array root, struct RArray::as.heap.aux.capa stores the
number of Arrays holding reference to that T_ARRAY instead of the actual
heap-allocated capacity. Use ARY_CAPA() macro which handles this
appropriately.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58332 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-04-13 06:43:17 +00:00
nobu f57d515d69 array.c: Array#append and Array#prepend
* array.c (Init_Array): Add alias "append" to Array#push, and
  "prepend" to Array#unshift.  [Feature #12746] [Fix GH-1574]

Author:    pascbjumper2 <stowers.joshua@live.com>

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58227 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-04-01 07:38:12 +00:00
stomar 56abb1cdd8 docs for creating arrays
* array.c: [DOC] add example for Array.new with block and index.
  Reported by Don Cruickshank.  [ruby-core:68442] [Bug #10944]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@58037 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-03-20 20:30:25 +00:00
stomar b4baeb5af6 documentation for sort methods
* array.c: [DOC] fix grammar in Array#sort, #sort!, #sort_by!,
  move references below the code example, add a missing reference.
* enum.c: [DOC] fix grammar in Enumerable#sort, #sort_by.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57771 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-03-04 13:13:50 +00:00
nobu 3203ae53ff array.c: check if numeric
* array.c (finish_exact_sum): add 0 and the initial value to check
  if the latter is numeric.  [ruby-core:79572] [Bug #13222]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57651 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-02-18 04:23:20 +00:00
nobu 7dccda437b array.c: finish_exact_sum
* array.c (finish_exact_sum): extract duplicate code from
  rb_ary_sum.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57649 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-02-18 03:42:35 +00:00
normal 302ae913e0 array.c (ary_recycle_hash): use rb_gc_force_recycle
Hidden objects (RBASIC_CLASS(hash) == 0) can never become
visible to other threads or signal handlers via
ObjectSpace.each_object or similar means.  Thus it is safe to
forcibly recycle the object slot for future use, here.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57615 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-02-13 02:04:26 +00:00
nobu 2de1dbdffc array.c: improve Array#sample
* array.c (rb_ary_sample): improve performance when many samples
  from a large array.  based on the patch by tomoya ishida
  <tomoyapenguin AT gmail.com> in [ruby-dev:49956].  [Bug #13136]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57380 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-01-20 02:39:27 +00:00
normal eeb36c5c74 mention behavior of Array#join for nested arrays [ci skip]
The current documentation for Array#join does not mention the
special treatment of nested arrays.

It says:
> Returns a string created by converting each element of the
> array to a string, separated by the given separator.

Expected behavior according to the docs would be:

    [ "a", [1, 2, [:x, :y]], "b" ].join("-")  #=> "a-[1, 2, [:x, :y]]-b"
    # because of:
    [1, 2, [:x, :y]].to_s  #=> "[1, 2, [:x, :y]]"

Actual behavior:

    [ "a", [1, 2, [:x, :y]], "b" ].join("-")  #=> "a-1-2-x-y-b"

because join is applied recursively for nested arrays.

The patch clarifies this behavior.

(Also: small markup and grammar fix.)

Patch by Marcus Stollsteimer <sto.mar@web.de>

[ruby-talk:437238] [ruby-core:79079] [Bug #13130]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57329 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2017-01-14 23:09:55 +00:00
rhe b5da45d6d7 array.c: do not resize to less than 0
Shrinking the Array from the block invoked by Array#select! or
Array#reject! causes the Array to be a negative number size. Ensure that
the resulting Array won't be smaller than 0.
[ruby-core:78739] [Bug #13053]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57121 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-12-20 06:53:44 +00:00
rhe 647ba111ea array.c: check array length every time after yielding
Since the Array may be modified during rb_yield(), the length before
invoking the block can't be trusted. Fix possible out-of-bounds read in
Array#combination and Array#repeated_combination.

It may better to make a defensive copy of the Array, but for now let's
follow what Array#permutation does.  [ruby-core:78738] [Bug #13052]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57119 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-12-20 05:26:08 +00:00
mrkn 48f5f5915b array.c, enum.c: change sum algorithm
* array.c (rb_ary_sum): change the algorithm to Kahan-Babuska balancing
  summation to be more precise.
  [Feature #12871] [ruby-core:77771]

* enum.c (sum_iter, enum_sum): ditto.

* test_array.rb, test_enum.rb: add an assertion for the above change.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@57001 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-12-06 13:40:31 +00:00
a_matsuda f12003d372 Update documentation of fetch
The sentence `Negative values of +index+ count from the end of the array.` can be interpreted that it only holds if a block is given. Clarify it.

Patch by: Lukas Elmer <lukas.elmer@gmail.com> (@lukaselmer)
Signed-off-by: Akira Matsuda <ronnie@dio.jp>

closes #1472
[ci skip]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56679 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-08 19:43:58 +00:00
akr 8afd5857f0 [DOC] add explanation for Array#sum
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56566 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-11-05 02:50:15 +00:00
nobu 5b3b8554c9 unstable sort [ci skip]
* array.c (rb_ary_sort_bang, rb_ary_sort, rb_ary_sort_by_bang):
  [DOC] describe that sort may not be stable.
* enum.c (enum_sort, enum_sort_by): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56413 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-10-13 02:35:34 +00:00
nobu dd2ebf4d78 replace Fixnum with Integer in rdoc [ci skip]
* array.c, class.c: Fixed documentation where Fixnum was referred
  directly to use Integer, as Fixnum and Bignum are now unified
  into Integer and direct usage is deprecated.  [Fix GH-1459]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56382 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-10-09 14:05:57 +00:00
usa c2dd2d268e * internal.h (ST2FIX): new macro to convert st_index_t to Fixnum.
a hash value of Object might be Bignum, but it causes many troubles
  expecially the Object is used as a key of a hash.  so I've gave up
  to do so.

* array.c (rb_ary_hash): use above macro.

* bignum.c (rb_big_hash): ditto.

* hash.c (rb_obj_hash, rb_hash_hash): ditto.

* numeric.c (rb_dbl_hash): ditto.

* proc.c (proc_hash): ditto.

* re.c (rb_reg_hash, match_hash): ditto.

* string.c (rb_str_hash_m): ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56340 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-10-04 16:25:01 +00:00
nobu e71e83b021 array.c: update Array#dig doc
* array.c (rb_ary_dig): [DOC] update an example of error message
  by Array#dig, because of Integer Unification.  [Fix GH-1455]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56337 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-10-04 03:59:46 +00:00
nobu 0cc169d1de fid typos [ci skip]
* fix typos, "a" before "Integer" to "an".  [Fix GH-1438]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56225 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-09-24 02:28:25 +00:00
ktsj 1f8765bb2b * array.c (flatten): use rb_obj_class instead of rb_class_of
because rb_class_of may return a singleton class.
  [ruby-dev:49781] [Bug #12738]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56111 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-09-08 08:56:30 +00:00
akr 577de1e93d replace fixnum by integer in documents.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56102 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-09-08 04:57:49 +00:00
nobu 9387ff7315 multiple arguments
* array.c (rb_ary_concat_multi): take multiple arguments.  based
  on the patch by Satoru Horie.  [Feature #12333]
* string.c (rb_str_concat_multi, rb_str_prepend_multi): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56021 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-08-27 01:26:17 +00:00
nobu 2329d8b0de array.c: elements in self
* array.c (rb_ary_splice): consider elements in middle of self.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55983 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-08-22 04:21:54 +00:00
nobu 4831eb86f6 array.c: no temporary array
* array.c (rb_ary_splice): use pointer and length pair instead of
  an array object to replace.

* array.c (rb_ary_insert): get rid of creating temporary array.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55977 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-08-21 04:03:22 +00:00
nobu aa0e51b529 hash.c: rb_hash_add_new_element
* hash.c (rb_hash_add_new_element): add new element or do nothing
  if it is contained already.
* array.c (ary_add_hash, ary_add_hash_by): use
  rb_hash_add_new_element.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55707 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-07-20 08:35:25 +00:00
naruse cb2a99822e * array.c (rb_ary_fill): suppress warnings: 'item' may be used
uninitialized in this function

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55122 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-23 03:23:24 +00:00
naruse 6b4132724a * array.c (rb_ary_entry): extract rb_ary_elt to organize if-conditions
and check whether is is embdeded at once.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@55007 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-15 07:17:46 +00:00
mrkn 78729a59a2 Fix rb_ary_sum for mathn
* array.c (rb_ary_sum): fix for mathn

* test/ruby/test_array.rb (test_sum): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54870 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-05-01 15:02:47 +00:00
akr ef6af0c967 [DOC] Arrah#sum with non-numeric objects.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54705 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-22 11:44:34 +00:00
ktsj 5e08452993 * array.c (rb_ary_sum): [DOC] fix typos.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54619 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-17 11:34:52 +00:00
akr b59a158a18 * array.c (rb_ary_sum): Don't yield same element twice.
Found by nagachika.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54609 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-15 16:26:38 +00:00
akr 72305d2531 * array.c (rb_ary_sum): Fix SEGV by [1/2r, 1].sum.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54606 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-15 16:04:18 +00:00
mrkn d1d95056a3 rename rb_rational_add -> rb_rational_plus
* rational.c (rb_rational_plus): rename from rb_rational_add
  to be aligned with rb_fix_plus.

* array.c (rb_ary_sum): ditto.

* internal.h: ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54603 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-15 14:54:39 +00:00
mrkn 0a267b79f6 array.c (rb_ary_sum): use rb_rational_add directly
* rational.c (rb_rational_add): rename from nurat_add.

* array.c (rb_ary_sum): use rb_rational_add directly.

* test/ruby/test_array.rb (test_sum): add assertions for an array of
  Rational values.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54602 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-15 14:46:35 +00:00
mrkn a945eb9dda array.c: sum for Rational and Float mixed arrays
* array.c (rb_ary_sum): apply the precision compensated algorithm
  for an array in which Rational and Float values are mixed.

* test/ruby/test_array.rb (test_sum): add assertions for the above
  change.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54601 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-15 13:33:05 +00:00
akr df941c91b1 [DOC]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54578 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-14 12:56:38 +00:00
akr 76426c7ea5 * array.c (rb_ary_sum): Support the optional argument, init, and block.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54577 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-14 12:48:44 +00:00
akr 350a740058 [DOC]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54568 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-14 00:00:32 +00:00
akr b359d20352 * array.c (rb_ary_sum): Array#sum is implemented.
Kahan's compensated summation algorithm for precise sum of float
  numbers is moved from ary_inject_op in enum.c.

* enum.c (ary_inject_op): Don't specialize for float numbers.

  [ruby-core:74569] [Feature#12217] proposed by mrkn.



git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54565 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-04-13 13:51:53 +00:00
mame b3a65c883a * array.c, enum.c: make rdoc format consistent.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54155 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-03-17 12:52:47 +00:00
mame 4ca0483a28 * array.c (rb_ary_max, rb_ary_min): implement Array#max and min with
arguments.  replace super call with rb_nmin_run.

* enum.c (nmin_run): exported (as rb_nmin_run).

* internal.h: added a prototype for rb_nmin_run.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54152 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-03-17 12:37:20 +00:00
mame 90ab5beb42 * array.c (rb_ary_max, rb_ary_min): implement a block by itself instead
of delegating Enumerable#max/min.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54151 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-03-17 12:25:40 +00:00
mame 68a6f2e9e0 * array.c (rb_ary_max, rb_ary_min): Array#max and Array#min added.
[Feature #12172]

* internal.h (OPTIMIZED_CMP): moved from enum.c so that array.c can
  use it.

* test/ruby/test_array.rb (test_max, test_min): tests for Array#max
  and Array#min.

* test/ruby/test_enum.rb (test_max, test_min): revised a bit to test
  Enumerable#max and #min explicitly.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54150 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-03-17 12:14:21 +00:00
mame a22455199b * internal.c: struct cmp_opt_data added for refactoring out a data
structure for CMP_OPTIMIZABLE

* array.c (struct ary_sort_data): use struct cmp_opt_data.

* enum.c (struct min_t, max_t, min_max_t): use struct cmp_opt_data.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@54149 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-03-17 12:03:48 +00:00
nobu c31d06eeb7 array.c: [DOC] remove trailing comma [ci skip]
* array.c (rb_ary_push_m): [DOC] Remove trailing comma from
  Array#push example, as other Array examples doesn't put trailing
  comma.  [Fix GH-1279]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53970 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-02-29 07:51:15 +00:00
nobu 491c44e984 Clarify set intersection and union documentation
* array.c (rb_ary_and): clarify that set intersection returns the
  unique elements common to both arrays.

* array.c (rb_ary_or): clarify that union preserves the order from
  the given arrays.

- Most know what intersection means, but saying the operation
  excludes duplicates could be misleading ([1] & [1], duplicates
  excluded, might mean a result of []).
- Instead, saying intersection returns the unique elements common to both
  arrays is more concise and less ambiguous.
- The set union's documentation was incomplete in its describing
  preservation of order. Saying union preserves the order of the
  original array neglects the idea that the order of the elements
  in both arrays, as given, will be preserved.
- Instead, saying set union preserves the order from the given arrays (and
  adding an example) fully demonstrates the idea.

[Fix GH-1273] [ci skip]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53958 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-02-28 01:20:39 +00:00
nobu 4df168074c Fix a Ruby-Doc comment for Array#dig
* array.c (rb_ary_dig): [DOC] fix the exception class to be raised
  when intermediate object does not have dig method.  TypeError
  will be raised now.  [Fix GH-1224]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53666 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-01-27 07:26:53 +00:00
nobu 7cf2a0a1b9 array.c: reword [ci skip]
* array.c (permute0, rpermute0): [DOC] Substitute indexes ->
  indices in documentation for consistency.  [Fix GH-1222]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53660 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-01-26 10:37:12 +00:00
nobu 439224a590 RUBY_ASSERT
* error.c (rb_assert_failure): assertion with stack dump.
* ruby_assert.h (RUBY_ASSERT): new header for the assertion.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53615 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-01-22 08:33:55 +00:00
shugo 9f44b77a18 * enum.c (enum_minmax): optimize object comparison in
Enumerable#minmax.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53454 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2016-01-07 13:06:23 +00:00
marcandre cb3b463a50 * array.c: Improve and fix documentation for Array#dig
[#11776]

* hash.c: ditto

* struct.c: ditto

* test_hash.rb: Add basic test for user defined `dig`.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52941 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-12-08 05:21:11 +00:00
hsbt 65c744fb9c * array.c: clarify docs for take_while/drop_while samples.
[ci skip][fix GH-1028] Patch by @leriksen

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52680 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-20 03:53:25 +00:00
nobu 0701e5ff46 update rdoc of dig methods [ci skip]
* array.c (rb_ary_dig), hash.c (rb_hash_dig): [DOC] Update
  comments describing dig methods.  [Fix GH-1103]
* struct.c (rb_struct_dig): [DOC] add rdoc.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52607 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-17 02:25:28 +00:00
nobu 29862685c0 dig
* array.c (rb_ary_dig): new method Array#dig.
* hash.c (rb_hash_dig): new method Hash#dig.
* object.c (rb_obj_dig): dig in nested arrays/hashes.
  [Feature #11643]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52504 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-09 12:27:26 +00:00
gogotanaka 0765fcf833 * array.c: clarifies Array#reject! documentation.
[fix GH-894][ci skip] Patch by @GxSplinter

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52468 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-11-06 14:01:30 +00:00
nobu 645116ff25 RUBY_DTRACE_CREATE_HOOK
* internal.h (RUBY_DTRACE_CREATE_HOOK): macro to call hook at
  object creation.

* vm.c (rb_source_location, rb_source_loc): retrieve source path
  and line number at once.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52340 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-10-29 05:32:57 +00:00
nobu 77a952d1fb adjust spaces [ci skip]
* array.c (rb_ary_collect): [DOC] Fix space of code example of
  Array#map.  [Fix GH-1062]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52224 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-10-22 15:19:06 +00:00
nobu 4191a6b90d preserve encodings in error messages
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51962 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-09-28 02:40:46 +00:00
nobu ca5c9084dc array.c: [DOC] correct Array#sort rdoc [ci skip]
* array.c (rb_ary_sort_bang, rb_ary_sort): [DOC] correct block
  return values, which may be a negative or positive integer, not
  ony -1 or +1.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51860 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-09-15 04:13:15 +00:00
nobu 5a0dc59a01 array.c: [DOC] correct Array#sort rdoc [ci skip]
* array.c (rb_ary_sort_bang, rb_ary_sort): [DOC] Correct
  description of array sort block return values.  And also fix up
  the grammar a bit.  [Fix GH-1020]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51859 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-09-15 03:49:12 +00:00
nobu 0a8a321558 array.c: fix buffer size
* array.c (rb_ary_repeated_permutation): fix buffer size, ALLOCV_N
  already multiplies element size.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51532 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-08-11 05:57:11 +00:00
nobu f8fb526ad9 remove needless volatile
* array.c (rb_ary_each): remove needless volatile for outdated
  clang 3.0.  revert r32201.

* cont.c (cont_capture): ditto.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51227 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-13 10:52:11 +00:00
nobu 481292190f array.c: fix memory leak
* array.c (rb_ary_sort_bang): the original array may not be
  embedded even if a substitution array is embedded, as it is
  embedded when the original array is short enough but not
  embedded.  [ruby-dev:49166] [Bug #11332]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51147 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-07-05 01:51:51 +00:00
nobu 4a686ebcd3 array.c: use ALLOCV_N
* array.c (rb_ary_permutation): use ALLOCV_N instead of ALLOCV.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50987 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-21 12:28:20 +00:00
nobu c54fb9ec8b array.c: fix for enumerator
* array.c (rb_ary_bsearch_index): fix function typt to return
  enumerator if no block given.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@50844 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2015-06-12 08:19:24 +00:00