1014 Commits
Author SHA1 Message Date
Étienne Barrié e068856616 Remove discard qualifiers warning with C23 strchr
C23 has qualifier-preserving standard library functions, so calling
strchr with a `const char *` will return a `const char *`. We can change
the type of the local variables because we don't mutate the strings.
2026-10-16 21:38:29 +09:00
nagachika d784a0b8a5 merge revision(s) 239a472609, d29afc8ab8: [Backport #20958]
[PATCH] fix ENV.keys encoding on windows

	Fix the encoding of ENV.keys elements to UTF-8 on windows.
	They contain UTF-8 byte sequences with locale encoding.

	[PATCH] [Bug #20958] Fix test_keys_encoding on hosts without en_US.UTF-8

	`EnvUtil.invoke_ruby` forces the C locale on the child process, so the
	test set `LC_ALL=en_US.UTF-8` to get a UTF-8 locale back.  That locale
	is not generated on the debian11 chkbuild host, where `setlocale` falls
	back to C and the ENV key comes out as ASCII-8BIT instead of the
	default internal encoding.  Inherit the locale of the parent process
	instead, and omit the test when that locale is not UTF-8.

	https://rubyci.s3.amazonaws.com/debian11/ruby-master/log/20260725T093003Z.fail.html.gz
2026-08-09 15:59:47 +09:00
Benoit Daloze e9ace5404c Fix ar_find_entry_hint() to handle #eql? or another thread changing bound or converting to st_table
* While perusing code in hash.c I found it suspicious that ar_find_entry_hint()
  didn't reread bound in the loop and yet called arbitrary code through #eql?.
* ar_find_entry_hint() before this commit would not check if bound or the storage (AR->ST) changed
  and would return a bin index which is not correct to access.
* Check bound in the loop so it's always up-to-date.
* Return RHASH_AR_TABLE_CONVERTED_TO_ST_TABLE when converted to st_table
  and make callers retry the operation as a st_table.

[Bug #22120]

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
(cherry picked from commit c88430e187)
2026-06-29 19:15:38 +09:00
nagachika c1487868e9 merge revision(s) a05d5ecb12:
[PATCH] Add write barriers on Hash#rehash
2026-04-26 20:30:55 +09:00
Takashi Kokubun 1bdae3404b merge revision(s) 0564973196: [Backport #21357]
[Bug #21357] Fix crash in Hash#merge with block

	Prior to https://github.com/ruby/ruby/commit/49b306ecb9e2e9e06e0b1590bacc5f4b38169c3c
	the `optional_arg` passed from `rb_hash_update_block_i` to `tbl_update`
	was a hash value (i.e. a VALUE). After that commit it changed to an
	`update_call_args`.

	If the block sets or changes the value, `tbl_update_modify` will set the
	`arg.value` back to an actual value and we won't crash. But in the case
	where the block returns the original value we end up calling
	`RB_OBJ_WRITTEN` with the `update_call_args` which is not expected and
	may crash.

	`arg.value` appears to only be used to pass to `RB_OBJ_WRITTEN` (others
	who need the `update_call_args` get it from `arg.arg`), so I don't think
	it needs to be set to anything upfront. And `tbl_update_modify` will set
	the `arg.value` in the cases we need the write barrier.
2025-07-14 13:52:11 -07:00
Takashi Kokubun 247b452af3 merge revision(s) 49b306ecb9: [Backport #21333]
[Bug #21333] Prohibit hash modification inside Hash#update block
2025-07-14 13:49:12 -07:00
Nobuyoshi Nakada a21b88a608 [Bug #21331] Prohibit hash modification during stlike loop 2025-05-13 20:01:10 -07:00
Nobuyoshi Nakada 22f2047a3d Remove unused retval assignments 2025-05-13 20:00:58 -07:00
Nobuyoshi Nakada fbcb327185 Revert "[Bug #21331] Prohibit modification during stlike loop"
This reverts commit bb180b87b4, which
caused "malloc during GC" error on wasm.
2025-05-13 20:00:53 -07:00
Takashi Kokubun 862480a316 merge revision(s) bb180b87b4: [Backport #21331]
[Bug #21331] Prohibit modification during stlike loop
2025-05-13 17:14:49 -07:00
Nobuyoshi Nakada d40c6cf7b6 [Bug #21144] Win32: Use Windows time zone ID if TZ is not set
If the TZ environment variable is not set, the time zone names
retrieved from the system are localized for UI display and may vary
across editions and language packs for the same time zone.
Use the time zone IDs that are invariant across environments instead.
2025-03-04 13:04:27 -08:00
Nobuyoshi Nakada 24f48c83e7 [Bug #20982] Put spaces in ENV.inspect results as well as Hash 2025-02-13 21:43:19 -08:00
Takashi Kokubun 270129fdcb merge revision(s) 19c39e4cfa, d78ff6a767: [Backport #20984]
[Bug #20984] ENV.inspect should be encoding aware

	[Bug #20984] Fix test with locale encoding
2025-02-13 21:29:23 -08:00
tomoya ishida 477c505ac0 [DOC] Fix output examples containing old Hash#inspect format
The inspect format was intentionally changed as an outcome of
[Bug #20433] [ruby-core:118668], but some documentation update
was missing, as [Bug #20962] pointed out. Update some output
examples that clearly use Hash#inspect.
2024-12-18 12:08:15 -05:00
tompng a8a0591253 Hash#inspect with colon style 2024-10-03 18:47:09 +09:00
ydah 0c25eb4ffe s/interation/iteration/ 2024-09-27 09:58:32 +09:00
ydah cc1e1e77d5 s/useable/usable/ 2024-09-26 20:01:20 +09:00
Lars Kanis acf28e835f Windows: Use Unicode aware WinAPI function for ENV[]=
This only makes a difference when setting an empty value to a Unicode key.
2024-09-24 14:06:51 +09:00
Lars Kanis 7627a95e41 Windows: Remove ENV code targeting Windows before Vista
Windows Vista removed the limit to 32768 bytes environment block.
Also MINGW supports _wputenv_s() for years.
2024-09-24 14:06:51 +09:00
Étienne BarriéandJean Boussier bf9879791a Optimized instruction for Hash#freeze
If a Hash which is empty or only using literals is frozen, we detect
this as a peephole optimization and change the instructions to be
`opt_hash_freeze`.

[Feature #20684]

Co-authored-by: Jean Boussier <byroot@ruby-lang.org>
2024-09-05 12:46:02 +02:00
BurdetteLamar a85dc3e972 [DOC] Rationalize aliases 2024-08-30 22:25:47 -04:00
Yusuke Endoh ac5ac48a36 Revert 28a1c4f33e
28a1c4f33e seems to call an improper
ensure clause. [Bug #20655]
Than fixing it properly, I bet it would be much better to simply revert
that commit. It reduces the unneeded complexity. Jumping into a block
called by a C function like Hash#each with callcc is user's fault.
It does not need serious support.
2024-07-30 15:31:24 +09:00
Jean Boussier 9594db0cf2 Implement Hash.new(capacity:)
[Feature #19236]

When building a large hash, pre-allocating it with enough
capacity can save many re-hashes and significantly improve
performance.

```
/opt/rubies/3.3.0/bin/ruby --disable=gems -rrubygems -I./benchmark/lib ./benchmark/benchmark-driver/exe/benchmark-driver \
	            --executables="compare-ruby::../miniruby-master -I.ext/common --disable-gem" \
	            --executables="built-ruby::./miniruby --disable-gem" \
	            --output=markdown --output-compare -v $(find ./benchmark -maxdepth 1 -name 'hash_new' -o -name '*hash_new*.yml' -o -name '*hash_new*.rb' | sort)
compare-ruby: ruby 3.4.0dev (2024-03-25T11:48:11Z master f53209f023) +YJIT dev [arm64-darwin23]
last_commit=[ruby/irb] Cache RDoc::RI::Driver.new (https://github.com/ruby/irb/pull/911)
built-ruby: ruby 3.4.0dev (2024-03-25T15:29:40Z hash-new-rb 77652b08a2) +YJIT dev [arm64-darwin23]
warming up...

|                    |compare-ruby|built-ruby|
|:-------------------|-----------:|---------:|
|new                 |      7.614M|    5.976M|
|                    |       1.27x|         -|
|new_with_capa_1k    |     13.931k|   15.698k|
|                    |           -|     1.13x|
|new_with_capa_100k  |     124.746|   148.283|
|                    |           -|     1.19x|
```
2024-07-08 12:24:33 +02:00
Nobuyoshi Nakada f36a71e269 [Bug #20307] Fix Hash#update to make frozen copy of string keys 2024-03-14 17:49:37 +09:00
Nobuyoshi Nakada cd774f4ab9 Extract RHASH_STRING_KEY_P 2024-03-14 17:49:37 +09:00
Nobuyoshi Nakada c1279a3da5 Extract RHASH_IDENTHASH_P 2024-03-14 17:49:37 +09:00
Takashi Kokubun 61fbd29e14 Skip a redundant check for the rb_obj_hash case
Also, refactor the cfunc struct to use a new rb_cfunc_t.
2024-03-01 15:02:28 -08:00
Takashi Kokubun 70de3b170b Optimize Hash methods with Kernel#hash (#10160) 2024-03-01 11:16:31 -08:00
Peter Zhu df5b8ea4db Remove unneeded RUBY_FUNC_EXPORTED 2024-02-23 10:24:21 -05:00
Burdette Lamar 65f5435540 [DOC] Doc compliance (#9955) 2024-02-14 10:47:42 -05:00
Burdette Lamar 21297293f4 [DOC] Doc compliance (#9944) 2024-02-13 13:49:35 -05:00
Takashi Kokubun b74c8abd11 YJIT: Skip pushing a frame for Hash#empty? (#9875) 2024-02-08 10:22:07 -05:00
Nobuyoshi Nakada dde21a7967 Explicitly convert between VALUE and st_data_t 2024-01-15 09:35:46 +09:00
Xavier Noria aad246feba s/SafeStringValue/StringValue/
The macro SafeStringValue() became just StringValue() in c5c05460ac,
and it is deprecated nowadays.

This patch replaces remaining macro usage. Some occurrences are left in
ext/stringio and ext/win32ole, they should be fixed upstream.

The macro itself is not deleted, because it may be used in extensions.
2024-01-12 12:24:48 -08:00
BurdetteLamar 54e8901f66 Tweak for ENV doc 2024-01-06 13:23:27 -05:00
Peter Zhu 6c252912af Memory leak when duplicating identhash
[Bug #20145]

Before this commit, both copy_compare_by_id and hash_copy will create a
copy of the ST table, so the ST table created in copy_compare_by_id will
be leaked.

    h = { 1 => 2 }.compare_by_identity

    10.times do
      1_000_000.times do
        h.select { false }
      end

      puts `ps -o rss= -p #{$$}`
    end

Before:

    110736
    204352
    300272
    395520
    460704
    476736
    542000
    604704
    682624
    770528

After:

    15504
    16048
    16144
    16256
    16320
    16320
    16752
    16752
    16752
    16752
2024-01-04 11:25:31 -05:00
Peter Zhu 997124fc0b [DOC] Remove extra colon in Hash#default_proc 2023-12-31 21:25:22 -05:00
Peter Zhu 0c923fed7d [DOC] Fix syntax in Hash#eql? 2023-12-29 09:10:58 -05:00
Peter Zhu 4d4da09ae4 [DOC] Remove useless word in Hash#eql? 2023-12-29 09:10:58 -05:00
Peter Zhu 7595c5d4a5 [DOC] Add parantheses in call-seq for Hash#eql? 2023-12-29 09:10:58 -05:00
Peter Zhu 824ff48adc Move internal ST functions to internal/st.h
st_replace and st_init_existing_table_with_size are functions used
internally in Ruby and should not be publicly visible.
2023-12-25 10:41:12 -05:00
Alexander Momchilov 11fa76b1b5 compare_by_identity: remove alloc for non-empty Hash
If a Hash is non-empty, there's no point calling `ar_force_convert_table`. We'll be immediately discarding that new st table, and replacing it with the new `identtable` st table that we're stealing out of `tmp`.
2023-12-18 14:51:16 -05:00
Alexander Momchilov b5c6c0122f compare_by_identity: remove alloc for empty Hash
For non-empty Hashes, this function needs to rehash all the stored values (using the new `compare` and `hash` functions from `identhash`). It does so by writing into a newly allocated `tmp` Hash, and then transferring ownership of its st table into `self`.

For empty Hashes, we can skip allocating this `tmp`, because there's nothing to re-hash. We can just modify our new st table's `type` in-place.
2023-12-18 14:51:16 -05:00
Nobuyoshi Nakada 40fc9b070c [DOC] No document for internal or debug methods 2023-12-18 20:17:45 +09:00
Koichi Sasada 7ba2506232 check modifcation whil ar->st
* delete `ar_try_convert` but use `ar_force_convert_table`
  to make program simple.
* `ar_force_convert_table` checks hash modification while
  calling `#hash` method with the following strategy:

1. copy keys (and vals) of ar_table
2. calc hashes from keys
3. check copied keys and hash's keys. if not matched, repeat from 1

fix [Bug #20050]
2023-12-15 11:58:43 +09:00
Alan WuandPeter Zhu a3b48ac9ad Fix memory leak in Hash#compare_by_identity
We didn't free the old ST before overwriting it which caused a leak.
Found with RUBY_FREE_ON_EXIT.

Co-authored-by: Peter Zhu <peter@peterzhu.ca>
2023-12-13 09:43:09 -08:00
John Hawthorn 5f81f58b26 Use xfree in hash_st_free
st.c redefines malloc and free to be ruby_xmalloc and ruby_xfree, so
when this was copied into hash.c it ended up mismatching an xmalloc with
a regular free, which ended up inflating oldmalloc_increase_bytes when
hashes were freed by minor GC.
2023-12-07 09:22:21 -08:00
Nobuyoshi Nakada f5c3cda7d6 Do not change hash type in Hash#assoc 2023-11-21 15:15:23 +09:00
Yusuke Endoh 1cf2fa3af5 Raise an exception when Hash#compare_by_identity during its iteration 2023-11-21 15:15:23 +09:00
Yusuke Endoh a787e0d649 Raise an exception if ar_table is converted to st_table during iteration
ar_table may be converted to st_table by `ar_force_convert_table`.
If the conversion occurs during the iteration of ar_table, the iteration
may lead to memory corruption.

This change prevents the catastrophy by throwing an exception when the
conversion is detected.

This issue is reported by [SuperS](https://hackerone.com/superss)
2023-11-21 15:15:23 +09:00