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

903 Коммитов

Автор SHA1 Сообщение Дата
Nobuyoshi Nakada d4e1d4e94e
Moved Array#sample to rbinc 2020-01-26 19:45:58 +09:00
Nobuyoshi Nakada 29eb1b1602
Moved Array#shuffle and Array#shuffle! to rbinc 2020-01-26 19:40:34 +09:00
Nobuyoshi Nakada 0c436bbfbf
Recheck array length after `to_str` conversion
https://hackerone.com/reports/244787
2020-01-25 14:06:38 +09:00
Nobuyoshi Nakada 2b2821acd3
Recheck elements type after `to_str` conversion
https://hackerone.com/reports/244786
2020-01-25 13:57:33 +09:00
Ary Borenszweig e5c441a4a2 Optimize Array#rotate!(n) for n = 1 and n = -1
For the most common cases of `rotate!` one place to the right or to the
left, instead of doing some reversals of the array we just keep a single
value in a temporary value, use memmove and then put the temporary
value where it should be.
2019-12-29 13:12:42 +09:00
卜部昌平 5e22f873ed decouple internal.h headers
Saves comitters' daily life by avoid #include-ing everything from
internal.h to make each file do so instead.  This would significantly
speed up incremental builds.

We take the following inclusion order in this changeset:

1.  "ruby/config.h", where _GNU_SOURCE is defined (must be the very
    first thing among everything).
2.  RUBY_EXTCONF_H if any.
3.  Standard C headers, sorted alphabetically.
4.  Other system headers, maybe guarded by #ifdef
5.  Everything else, sorted alphabetically.

Exceptions are those win32-related headers, which tend not be self-
containing (headers have inclusion order dependencies).
2019-12-26 20:45:12 +09:00
卜部昌平 0e8219f591 make functions static
These functions are used from within a compilation unit so we can
make them static, for better binary size.  This changeset reduces
the size of generated ruby binary from 26,590,128 bytes to
26,584,472 bytes on my macihne.
2019-11-19 12:36:19 +09:00
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