summaryrefslogtreecommitdiff
path: root/hash.c
AgeCommit message (Collapse)Author
2025-07-14merge revision(s) 056497319658cbefe22351c6ec5c9fa6e4df72bd: [Backport #21357]Takashi Kokubun
[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-14merge revision(s) 49b306ecb9e2e9e06e0b1590bacc5f4b38169c3c: [Backport #21333]Takashi Kokubun
[Bug #21333] Prohibit hash modification inside Hash#update block
2025-05-13[Bug #21331] Prohibit hash modification during stlike loopNobuyoshi Nakada
2025-05-13Remove unused retval assignmentsNobuyoshi Nakada
2025-05-13Revert "[Bug #21331] Prohibit modification during stlike loop"Nobuyoshi Nakada
This reverts commit bb180b87b43c45e17ff49735a26d7a188d5c8396, which caused "malloc during GC" error on wasm.
2025-05-13merge revision(s) bb180b87b43c45e17ff49735a26d7a188d5c8396: [Backport #21331]Takashi Kokubun
[Bug #21331] Prohibit modification during stlike loop
2025-03-04[Bug #21144] Win32: Use Windows time zone ID if TZ is not setNobuyoshi Nakada
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-02-13[Bug #20982] Put spaces in `ENV.inspect` results as well as `Hash`Nobuyoshi Nakada
2025-02-13merge revision(s) 19c39e4cfaa467e69b9848c9c5496d7f50d39c7f, ↵Takashi Kokubun
d78ff6a767ca813ac5fa178dd7611f20a993c191: [Backport #20984] [Bug #20984] ENV.inspect should be encoding aware [Bug #20984] Fix test with locale encoding
2024-12-18[DOC] Fix output examples containing old Hash#inspect formattomoya ishida
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. Notes: Merged: https://github.com/ruby/ruby/pull/12384 Merged-By: XrXr
2024-10-03Hash#inspect with colon styletompng
Notes: Merged: https://github.com/ruby/ruby/pull/10924
2024-09-27s/interation/iteration/ydah
Notes: Merged: https://github.com/ruby/ruby/pull/11696
2024-09-26s/useable/usable/ydah
Notes: Merged: https://github.com/ruby/ruby/pull/11694
2024-09-24Windows: Use Unicode aware WinAPI function for ENV[]=Lars Kanis
This only makes a difference when setting an empty value to a Unicode key. Notes: Merged: https://github.com/ruby/ruby/pull/7034
2024-09-24Windows: Remove ENV code targeting Windows before VistaLars Kanis
Windows Vista removed the limit to 32768 bytes environment block. Also MINGW supports _wputenv_s() for years. Notes: Merged: https://github.com/ruby/ruby/pull/7034
2024-09-05Optimized instruction for Hash#freezeÉtienne Barrié
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> Notes: Merged: https://github.com/ruby/ruby/pull/11406
2024-08-30[DOC] Rationalize aliasesBurdetteLamar
Notes: Merged: https://github.com/ruby/ruby/pull/11513
2024-07-30Revert 28a1c4f33e3349a98c04b8e068d9c674eb936064Yusuke Endoh
28a1c4f33e3349a98c04b8e068d9c674eb936064 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. Notes: Merged: https://github.com/ruby/ruby/pull/11254
2024-07-08Implement Hash.new(capacity:)Jean Boussier
[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-03-14[Bug #20307] Fix `Hash#update` to make frozen copy of string keysNobuyoshi Nakada
2024-03-14Extract `RHASH_STRING_KEY_P`Nobuyoshi Nakada
2024-03-14Extract `RHASH_IDENTHASH_P`Nobuyoshi Nakada
2024-03-01Skip a redundant check for the rb_obj_hash caseTakashi Kokubun
Also, refactor the cfunc struct to use a new rb_cfunc_t.
2024-03-01Optimize Hash methods with Kernel#hash (#10160)Takashi Kokubun
2024-02-23Remove unneeded RUBY_FUNC_EXPORTEDPeter Zhu
2024-02-14[DOC] Doc compliance (#9955)Burdette Lamar
2024-02-13[DOC] Doc compliance (#9944)Burdette Lamar
2024-02-08YJIT: Skip pushing a frame for Hash#empty? (#9875)Takashi Kokubun
2024-01-15Explicitly convert between `VALUE` and `st_data_t`Nobuyoshi Nakada
2024-01-12s/SafeStringValue/StringValue/Xavier Noria
The macro SafeStringValue() became just StringValue() in c5c05460ac2, 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-06Tweak for ENV docBurdetteLamar
2024-01-04Memory leak when duplicating identhashPeter Zhu
[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
2023-12-31[DOC] Remove extra colon in Hash#default_procPeter Zhu
2023-12-29[DOC] Fix syntax in Hash#eql?Peter Zhu
2023-12-29[DOC] Remove useless word in Hash#eql?Peter Zhu
2023-12-29[DOC] Add parantheses in call-seq for Hash#eql?Peter Zhu
2023-12-25Move internal ST functions to internal/st.hPeter Zhu
st_replace and st_init_existing_table_with_size are functions used internally in Ruby and should not be publicly visible.
2023-12-18compare_by_identity: remove alloc for non-empty HashAlexander Momchilov
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-18compare_by_identity: remove alloc for empty HashAlexander Momchilov
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[DOC] No document for internal or debug methodsNobuyoshi Nakada
2023-12-15check modifcation whil ar->stKoichi Sasada
* 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-13Fix memory leak in Hash#compare_by_identityAlan Wu
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-07Use xfree in hash_st_freeJohn Hawthorn
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-11-21Do not change hash type in Hash#assocNobuyoshi Nakada
2023-11-21Raise an exception when Hash#compare_by_identity during its iterationYusuke Endoh
2023-11-21Raise an exception if ar_table is converted to st_table during iterationYusuke Endoh
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-12Just check if iteration level is non-zerov3_3_0_preview3Nobuyoshi Nakada
The level in ivar is no longer needed to check if iterating, only used for increment/decrement.
2023-11-12Refactor hash iteration levelNobuyoshi Nakada
- Make it unsigned like as in-flags bits - Make it long since it should be fixable - Reduce it to in-flags bits after decrement
2023-11-11[Bug #19969] Compact st_table after deleted if possibleNobuyoshi Nakada
2023-10-22[DOC] Update documentation for typical implementation of hashYuki Tsujimoto