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

961 Коммитов

Автор SHA1 Сообщение Дата
S-H-GAMELINKS 1f4f6c9832 Using UNDEF_P macro 2022-11-16 18:58:33 +09:00
Nobuyoshi Nakada c3e37f74ae
Adjust indents [ci skip] 2022-10-24 00:22:37 +09:00
S.H c8c136265c
Introduce `hash_iter_status_check` function 2022-10-23 16:17:24 +09:00
Samuel Williams 8dfe3bdf48
Improved formatting of `hash_foreach_iter` functions. (#6615) 2022-10-22 23:34:00 +13:00
Nobuyoshi Nakada ec3f59309e [Bug #17767] Now `ENV.clone` raises `TypeError` as well as `ENV.dup`
One year ago, the former method has been deprecated while the latter
has become an error.  Then the 3.1 released, it is enough time to make
also the former an error.
2022-08-02 16:40:12 +09:00
Peter Zhu efb91ff19b Rename rb_ary_tmp_new to rb_ary_hidden_new
rb_ary_tmp_new suggests that the array is temporary in some way, but
that's not true, it just creates an array that's hidden and not on the
transient heap. This commit renames it to rb_ary_hidden_new.
2022-07-26 09:12:09 -04:00
Takashi Kokubun 5b21e94beb Expand tabs [ci skip]
[Misc #18891]
2022-07-21 09:42:04 -07:00
Nobuyoshi Nakada 278fefb962 ENV.merge! support multile arguments [Feature #18279] 2022-06-17 01:22:24 +09:00
John Hawthorn 17d260a87f Restore rb_exec_recursive_outer
This was a public method, so we should probably keep it.
2022-06-15 16:07:29 -07:00
John Hawthorn 52da90acee Make method id explicit in rb_exec_recursive_outer
Previously, because opt_aref and opt_aset don't push a frame, when they
would call rb_hash to determine the hash value of the key, the initial
level of recursion would incorrectly use the method id at the top of the
stack instead of "hash".

This commit replaces rb_exec_recursive_outer with
rb_exec_recursive_outer_mid, which takes an explicit method id, so that
we can make the hash calculation behave consistently.

rb_exec_recursive_outer was documented as being internal, so I believe
this should be okay to change.
2022-06-10 14:48:21 -07:00
Chris Seaton 3a8d60f503
Document best-practices for writing hash methods (#5805)
* Discussion is as per https://bugs.ruby-lang.org/issues/18611.

Co-authored-by: Sam Bostock <sam.bostock@shopify.com>
2022-04-30 12:57:51 +02:00
Jean Boussier 4a2b5102c1 Expose `rb_hash_new_capa(long)`
[Feature #18683]

This allows parsers and similar libraries to create Hashes of
a certain capacity in advance. It's useful when the key and values
are streamed, hence `bulk_insert()` can't be used.
2022-04-26 16:12:47 +02:00
Nobuyoshi Nakada 300f4677c9
[DOC] Use simple references to operator methods
Method references is not only able to be marked up as code, also
reflects `--show-hash` option.
The bug that prevented the old rdoc from correctly parsing these
methods was fixed last month.
2022-03-26 21:13:16 +09:00
Burdette Lamar 1a002d9ade
Fix formatting errors in What's Here for Array, Hash, ENV (#5718) 2022-03-25 13:48:21 -05:00
Nobuyoshi Nakada 50c972a1ae
[DOC] Simplify operator method references 2022-02-12 12:38:36 +09:00
Steven Nunez 68be9a9ae8 Fix Typo 2022-02-12 10:43:27 +09:00
Nobuyoshi Nakada 16fdc1ff46
[DOC] Fix broken links to literals.rdoc 2022-02-08 01:27:52 +09:00
Nobuyoshi Nakada bc5662d9d8
[DOC] Simplify links to global methods 2022-02-08 01:18:56 +09:00
Peter Zhu a32e5e1b97 [DOC] Use RDoc link style for links in the same class/module
I used this regex:

(?<=\[)#(?:class|module)-([A-Za-z]+)-label-([A-Za-z0-9\-\+]+)

And performed a global find & replace for this:

rdoc-ref:$1@$2
2022-02-07 09:52:06 -05:00
Peter Zhu f9a2802bc5 [DOC] Use RDoc link style for links to other classes/modules
I used this regex:

([A-Za-z]+)\.html#(?:class|module)-[A-Za-z]+-label-([A-Za-z0-9\-\+]+)

And performed a global find & replace for this:

rdoc-ref:$1@$2
2022-02-07 09:52:06 -05:00
Aaron Patterson 2a76440fac [Bug #18501] Fire write barrier after hash has been written
Before this change the write barrier was executed before the key and
value were actually reachable via the Hash.  This could cause
inconsistencies in object coloration which would lead to accidental
collection of dup'd keys.

Example:

1. Object O is grey, Object P is white.
2. Write barrier fires O -> P
3. Write barrier does nothing
4. Malloc happens, which starts GC
5. GC colors O black
6. P is written in to O (now we have O -> P reference)
7. P is now accidentally treated as garbage
2022-02-04 14:36:04 -08:00
Jeremy Evans a93cc3e23b Make Hash#shift return nil for empty hash
Fixes [Bug #16908]
2022-01-14 12:17:57 -08:00
Nobuyoshi Nakada 39bc5de833
Remove tainted and trusted features
Already these had been announced to be removed in 3.2.
2021-12-26 23:28:54 +09:00
Kazuhiro NISHIYAMA 48a0905d25
Add missing '%' in format string 2021-12-21 18:21:03 +09:00
Victor Shepelev a0f10a973f
[DOC] Add documentation for hash value omission syntax 2021-12-20 01:09:52 +09:00
Burdette Lamar 7e0e6f9074
What's Here for ENV (#5292)
[DOC] What's Here for ENV
2021-12-18 12:32:11 -06:00
Koichi Sasada ce6fc20f97 data type should be `static` 2021-12-17 15:46:50 +09:00
Koichi Sasada c8f2766ae2 `RUBY_DEFAULT_FREE` is not needed.
pointed by @nobu.
2021-12-17 15:46:50 +09:00
Koichi Sasada 37bd795cf8 `ENV` ivars should not be accessible from ractors
The `ENV` object can have instance variables like other objects,
but they should be accessed only on the main ractor.

fix https://github.com/ruby/ruby/pull/5263#issuecomment-995585766
2021-12-17 15:46:50 +09:00
Nobuyoshi Nakada 05c9dfe23a
Suppress empty-body warning 2021-12-16 14:19:28 +09:00
Nobuyoshi Nakada 0f99f3fe5e
Adjust indents [ci skip] 2021-12-15 15:31:46 +09:00
Koichi Sasada 0eafba3610 use `RB_VM_LOCK_ENTER()`
We found that we need to make Ruby objects while locking the environ
to ENV operation atomically, so we decided to use `RB_VM_LOCK_ENTER()`
instead of `env_lock`.
2021-12-15 15:04:34 +09:00
Nobuyoshi Nakada a4a3528eb7 Removed no longer used variables 2021-12-15 15:04:34 +09:00
Nobuyoshi Nakada a6ebc10532 Fixed env_pairs array types 2021-12-15 15:04:34 +09:00
Nobuyoshi Nakada db3ab440db Use prototype definition instead of old K&R style 2021-12-15 15:04:34 +09:00
Nobuyoshi Nakada 88e36d8f3c Adjust styles [ci skip]
* --braces-after-func-def-line
* --space-after-for
2021-12-15 15:04:34 +09:00
Nobuyoshi Nakada f471cf295b Symbols closed to env should be static 2021-12-15 15:04:34 +09:00
Rohit Menon 3aab870761 Make ENV shareable 2021-12-15 15:04:34 +09:00
Rohit Menon 2a3e4b6940 Move exception-raising functions out of mutex; Refactor env-copying 2021-12-15 15:04:34 +09:00
Rohit Menon d3d156c21e Add locks for ENV 2021-12-15 15:04:34 +09:00
Burdette Lamar 28fb6d6b9e
Adding links to literals and Kernel (#5192)
* Adding links to literals and Kernel
2021-12-03 07:12:28 -06:00
Peter Zhu aeae6e2842 [Feature #18290] Remove all usages of rb_gc_force_recycle
This commit removes usages of rb_gc_force_recycle since it is a burden
to maintain and makes changes to the GC difficult.
2021-11-08 14:05:54 -05:00
Nobuyoshi Nakada d087214658 Restore Hash#compare_by_identity mode [Bug #18171] 2021-10-02 11:43:35 +09:00
Nobuyoshi Nakada 806e7947fe Add rb_ident_hash_new_with_size 2021-10-02 11:43:35 +09:00
John Hawthorn bb488a1a7f Use faster any_hash logic in rb_hash
From the documentation of rb_obj_hash:

> Certain core classes such as Integer use built-in hash calculations and
> do not call the #hash method when used as a hash key.

So if you override, say, Integer#hash it won't be used from rb_hash_aref
and similar. This avoids method lookups in many common cases.

This commit uses the same optimization in rb_hash, a method used
internally and in the C API to get the hash value of an object. Usually
this is used to build the hash of an object based on its elements.
Previously it would always do a method lookup for 'hash'.

This is primarily intended to speed up hashing of Arrays and Hashes,
which call rb_hash for each element.

    compare-ruby: ruby 3.0.1p64 (2021-04-05 revision 0fb782ee38) [x86_64-linux]
    built-ruby: ruby 3.1.0dev (2021-09-29T02:13:24Z fast_hash d670bf88b2) [x86_64-linux]
    # Iteration per second (i/s)

    |                 |compare-ruby|built-ruby|
    |:----------------|-----------:|---------:|
    |hash_aref_array  |       1.008|     1.769|
    |                 |           -|     1.76x|
2021-09-30 13:06:53 -07:00
Nobuyoshi Nakada a27c274f04
[DOC] Fix broken links [ci skip]
* As the "doc/" prefix is specified by the `--page-dir` option,
  remove from the rdoc references.
* Refer to the original .rdoc instead of the converted .html.
2021-09-15 14:16:14 +09:00
Jeremy Evans 57d315c937 Handle overwriting Object::ENV in spawn
Instead of looking for Object::ENV (which can be overwritten),
directly look for the envtbl variable.  As that is static in hash.c,
and the lookup code is in process.c, add a couple non-static
functions that will return envtbl (or envtbl#to_hash).

Fixes [Bug #18164]
2021-09-14 05:55:14 -09:00
Kenichi Kamiya bb84c75001 Revert "Force recycle intermediate collection in Hash#transform_keys! [Bug #17735]"
This reverts commit 522d4cd32f.
2021-09-14 14:53:42 +09:00
S-H-GAMELINKS 032534dbdf Using RB_BIGNUM_TYPE_P macro 2021-09-11 09:13:24 +09:00
Nobuyoshi Nakada a615885f1e
Free previously used tables [Bug #18134] 2021-08-29 17:18:58 +09:00
Sutou Kouhei a2ad0cb7b4 Make Hash#each family usable in Ractor
We don't need to increment/decrement iteration level for frozen Hash
because frozen Hash can't be modified. We can assume that nobody
changes the target Hash while calling #each family.

How to reproduce:

    a = {}
    100.times do |i|
      a[i] = true
    end
    Ractor.make_shareable(a)

    4.times.collect do
      Ractor.new(a) do |b|
        100.times do
          b.each_value do
          end
        end
      end
    end.each(&:take)

Example output:

    internal:ractor>:267: warning: Ractor is experimental, and the behavior may change in future versions of Ruby! Also there are many implementation issues.
    #<Thread:0x00007fcfb087bb30 run> terminated with exception (report_on_exception is true):
    #<Thread:0x00007fcfb087b8d8 run> terminated with exception (report_on_exception is true):
    #<Thread:0x00007fcfb088d678 run> terminated with exception (report_on_exception is true):
    #<Thread:0x00007fcfb087bd88 run> terminated with exception (report_on_exception is true):
    /tmp/h.rb:10:in `each_value'/tmp/h.rb:10:in `each_value': : /tmp/h.rb:10:in `each_value'no implicit conversion from nil to integer/tmp/h.rb:10:in `each_value'no implicit conversion from nil to integer (: :  (TypeErrorTypeError)no implicit conversion from nil to integer)no implicit conversion from nil to integer (

     (TypeErrorTypeError	from /tmp/h.rb:10:in `block (3 levels) in <main>'
    	from /tmp/h.rb:10:in `block (3 levels) in <main>'
    ))	from /tmp/h.rb:9:in `times'

    	from /tmp/h.rb:9:in `times'

    	from /tmp/h.rb:9:in `block (2 levels) in <main>'
    	from /tmp/h.rb:10:in `block (3 levels) in <main>'
    	from /tmp/h.rb:10:in `block (3 levels) in <main>'
    	from /tmp/h.rb:9:in `block (2 levels) in <main>'
    	from /tmp/h.rb:9:in `times'
    	from /tmp/h.rb:9:in `times'
    	from /tmp/h.rb:9:in `block (2 levels) in <main>'
    	from /tmp/h.rb:9:in `block (2 levels) in <main>'
    <internal:ractor>:694:in `take': thrown by remote Ractor. (Ractor::RemoteError)
    	from /tmp/h.rb:14:in `each'
    	from /tmp/h.rb:14:in `<main>'
    /tmp/h.rb:10:in `each_value': no implicit conversion from nil to integer (TypeError)
    	from /tmp/h.rb:10:in `block (3 levels) in <main>'
    	from /tmp/h.rb:9:in `times'
    	from /tmp/h.rb:9:in `block (2 levels) in <main>'
2021-08-26 13:29:58 +09:00
Nobuyoshi Nakada 6963f8f743
Remove old warning aged nearly 8 years 2021-08-19 17:44:48 +09:00
Nobuyoshi Nakada e5dd40b1f3
Stop force-recycling evacuated array [Bug #18065] 2021-08-07 12:34:45 +09:00
S.H 378e8cdad6
Using RBOOL macro 2021-08-02 12:06:44 +09:00
Nobuyoshi Nakada 71945ad827
Fix a comment [ci skip] 2021-07-28 19:08:05 +09:00
Nobuyoshi Nakada e3cda75436
Remove useless casts 2021-07-22 09:58:23 +09:00
Jeremy Evans 95f8ffa5f6
Copy hash compare_by_identity setting in more cases
This makes the compare_by_identity setting always copied
for the following methods:

* except
* merge
* reject
* select
* slice
* transform_values

Some of these methods did not copy the setting, or only
copied the setting if the receiver was not empty.

Fixes [Bug #17757]

Co-authored-by: Kenichi Kamiya <kachick1@gmail.com>
2021-07-15 10:04:17 -07:00
Nobuyoshi Nakada a73f13c907
[DOC] `Hash.[]` returns a hash with no default value/proc [ci skip] 2021-07-04 14:58:15 +09:00
Nobuyoshi Nakada 94bd3bde81 Specify version to remove as bare numbers 2021-06-30 10:47:01 +09:00
Nobuyoshi Nakada 8118d435d0 rb_warn_deprecated_to_remove_at [Feature #17432]
At compilation time with RUBY_DEBUG enabled, check if the removal
version has been reached.
2021-06-30 10:47:01 +09:00
Nobuyoshi Nakada 47a9b58b2a
Share freeze option handling 2021-06-28 10:53:37 +09:00
Nobuyoshi Nakada 189f154786
[DOC] fixed return value of ENV.clone [ci skip] 2021-06-24 21:53:59 +09:00
Burdette Lamar c1741df1a1 What's Here for Numeric and Comparable 2021-06-21 10:38:16 -07:00
Nobuyoshi Nakada e4f891ce8d
Adjust styles [ci skip]
* --braces-after-func-def-line
* --dont-cuddle-else
* --procnames-start-lines
* --space-after-for
* --space-after-if
* --space-after-while
2021-06-17 10:13:40 +09:00
Jeremy Evans 117310bdc0
Make ENV.clone warn and ENV.dup raise
ENV.dup returned a plain Object, since all of ENV's behavior is
defined in ENV's singleton class.  So using dup makes no sense.

ENV.clone works and is used in some gems, but it doesn't do what
the user expects, since modifying ENV.clone also modifies ENV.
Add a deprecation warning pointing the user to use ENV.to_h
instead.

This also undefines some private initialize* methods in ENV,
since they are not needed.

Fixes [Bug #17767]
2021-06-08 10:19:08 -07:00
S.H c57610dcd5
Add static modifier to C function in hash.c (#3138)
* add static modifier for rb_hash_reject_bang func

* add static modifier for rb_hash_reject func

* add static modifier for rb_hash_values_at func

* add static modifier for rb_hash_assoc func

* add static modifier for rb_hash_rassoc func
2021-06-01 01:00:45 -07:00
Nobuyoshi Nakada 0bbab1e515
Protoized old pre-ANSI K&R style declarations and definitions 2021-05-07 00:04:36 +09:00
Benoit Daloze 68d6bd0873 Fix trivial -Wundef warnings
* See [Feature #17752]

Co-authored-by: xtkoba (Tee KOBAYASHI) <xtkoba+ruby@gmail.com>
2021-05-04 14:56:55 +02:00
Nick Kelley a9824a3113 Correct documentation example on Hash#dig
Fixes [Misc #17842]. The current documentation suggests that:

        {foo: {bar: {baz: 2}}}.dig(:foo, :bar) # => {:bar=>{:baz=>2}}

when it should be:

        {foo: {bar: {baz: 2}}}.dig(:foo, :bar) # => {:baz=>2}
2021-04-30 15:46:19 -07:00
romainsalles 7de7d096e7 Fix wrong documentation
It doesn't return `nil` but raises an exception, as explained a few lines after
2021-04-23 12:25:00 -04:00
Kenichi Kamiya 92545977de
[Doc] Add Hash#value? into call-seq (#4293) 2021-04-15 11:14:43 +09:00
Kenichi Kamiya 522d4cd32f
Force recycle intermediate collection in Hash#transform_keys! [Bug #17735]
* Force recycle intermediate hash

* Force recycle intermediate array too

https://github.com/ruby/ruby/pull/4329#issuecomment-808840718
2021-03-28 14:09:52 +09:00
Nobuyoshi Nakada fb6ebe55d9
Hide an intermediate array 2021-03-28 09:48:45 +09:00
Nobuyoshi Nakada 5e5fb72f99
Clear an intermediate hash [Bug #17735] 2021-03-28 09:42:26 +09:00
Kenichi Kamiya 31e0382723
Keep non evaluated keys in `Hash#transform_keys!` [Bug #17735] 2021-03-28 09:14:57 +09:00
Kenichi Kamiya 5b272a6453 [Doc] Fix a typo around Hash#compare_by_identity 2021-03-22 08:16:19 -04:00
Kenichi Kamiya 7d3fdfb27d
Hash#transform_values! ensures receiver modifiable in block [Bug #17736] 2021-03-22 14:10:52 +09:00
Nobuyoshi Nakada d36ac283d1
Ensure the receiver hash modifiable before updating [Bug #17736]
Close https://github.com/ruby/ruby/pull/4298
2021-03-21 10:07:24 +09:00
Nobuyoshi Nakada 298edfa4a2
Refactor hash aset callback 2021-03-21 00:11:08 +09:00
Nobuyoshi Nakada 0b6554e65b
Refactor hash update callbacks 2021-03-20 23:42:15 +09:00
Kenichi Kamiya 7954bb056b Some Hash destructive methods ensure the receiver modifiable [Bug #17736]
refs:

* https://bugs.ruby-lang.org/issues/17736
* https://github.com/ruby/ruby/pull/4296

This commit aims to cover following methods

* Hash#select!
* Hash#filter!
* Hash#keep_if
* Hash#reject!
* Hash#delete_if

I think these are not all.

---

* Ensure the receiver is modifiable or not
* Assert the receiver is not modified
2021-03-20 20:46:15 +09:00
Marc-Andre Lafortune 0ef2923c2b Avoid rehashing in Hash#replace/dup/initialize_copy [Bug #16996] 2021-03-18 07:34:40 -04:00
Marc-Andre Lafortune d094c3ef04 Avoid rehashing in Hash#select/reject [Bug #16996] 2021-03-18 07:34:40 -04:00
BurdetteLamar 383685b52b Explicit references to Enumerable 2021-01-20 09:33:41 -08:00
Burdette Lamar 0e015f9389
Adds RDoc summary of Hash methods (#4045)
* Adds RDoc summary of Hash methods
2021-01-10 14:13:24 -06:00
Nobuyoshi Nakada 20a8425aa0
Make any hash values fixable [Bug #17488]
As hnum is an unsigned st_index_t, the result of RSHIFT may not be
in the fixable range.

Co-authored-by: NeoCat <neocat@neocat.jp>
2020-12-31 12:11:45 +09:00
Nobuyoshi Nakada 2eea9156eb
Adjusted indents [ci skip] 2020-12-28 19:43:55 +09:00
Marc-André Lafortune db2ebbd71b
Optimize calls to `Kernel#hash` (#3987)
This avoids recursive checks when the `hash` method of an object
isn't specialized.
2020-12-25 02:08:12 +09:00
Marc-Andre Lafortune 0d3dc2ec80 Make `Hash#except` always return a Hash
[Feature #15822]
2020-12-19 03:59:51 -05:00
Marc-Andre Lafortune 8558d5e480 Document Hash#transform_keys with hash. Amend NEWS [DOC] [ci skip] 2020-12-15 17:09:01 -05:00
Lars Kanis ca76337a00
Windows: Read ENV names and values as UTF-8 encoded Strings (#3818)
* Windows: Read ENV names and values as UTF-8 encoded Strings

Implements issue #12650: fix https://bugs.ruby-lang.org/issues/12650

This also removes the special encoding for ENV['PATH'] and some
complexity in the code that is unnecessary now.

* Windows: Improve readablity of getenv() encoding

getenv() did use the expected codepage as an implicit parameter of the macro.
This is mis-leading since include/ruby/win32.h has a different definition.
Using the "cp" variable explicit (like the other function calls) makes it
more readable and consistent.

* Windows: Change external C-API macros getenv() and execv() to use UTF-8

They used to process and return strings with locale encoding,
but since all ruby-internal spawn and environment functions use UTF-8,
it makes sense to change the C-API equally.
2020-12-08 02:00:39 +09:00
卜部昌平 547c71dec4
Hash#index: delete
Has been deprecated since 0c97c8e335.
2020-12-07 18:35:58 +09:00
卜部昌平 12a121cc0f
ENV.index: delete
Has been deprecated since 373282f665.
2020-12-02 20:17:11 +09:00
Cristian Greco ce3c9a3437 Fix USE_TRANSIENT_HEAP macro usage in hash.c
Additionally fix some typos in transient heap.
2020-11-19 07:11:36 +09:00
Jeremy Evans c0aeb98aa9 Make ENV.replace handle multiple environ entries with the same key
While it is expected that all environment keys are unique, that is
not enforced. It is possible by manipulating environ directly you
can call a process with an environment with duplicate keys.  If
ENV.replace was passed a hash with a key where environ had a
duplicate for that key, ENV.replace would end up deleting the key
from environ.

The fix in this case is to not assume that the environment key
list has unique keys, and continue processing the entire key
list in keylist_delete.

Fixes [Bug #17254]
2020-10-29 08:08:42 -07:00
Stefan Stüben 8c2e5bbf58 Don't redefine #rb_intern over and over again 2020-10-21 12:45:18 +09:00
Cristian Greco 6527411f05 [ci skip] Minor documentation fix.
Add missing period.
2020-10-12 15:16:24 +09:00
Kazuhiro NISHIYAMA cece71b467
Add call-seq of [Feature #16274] 2020-09-29 22:49:44 +09:00
bogdanvlviv cdb5258bec Fix `ENV.except`'s docs 2020-09-26 12:02:00 -04:00
Jeremy Evans df14c758fc Make hash returned by Hash#transform_values not have a default
This sets an explicit default of nil.  There is probably a better
approach of removing the default.

Fixes [Bug #17181]
2020-09-21 19:35:08 -07:00