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

993 Коммитов

Автор SHA1 Сообщение Дата
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 Wu 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
Nobuyoshi Nakada 60e19a0b5f
Just check if iteration level is non-zero
The level in ivar is no longer needed to check if iterating, only used
for increment/decrement.
2023-11-12 13:51:16 +09:00
Nobuyoshi Nakada 9ab64b1d70
Refactor hash iteration level
- 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-12 13:51:15 +09:00
Nobuyoshi Nakada 9eac9d7178
[Bug #19969] Compact st_table after deleted if possible 2023-11-11 18:49:19 +09:00
Yuki Tsujimoto e721a7aec7
[DOC] Update documentation for typical implementation of hash 2023-10-22 09:47:22 +00:00
Nobuyoshi Nakada 262a0cc868
[DOC] Missing comment marker 2023-09-27 15:50:37 +09:00
Peter Zhu f43dac0df2 Add rb_hash_free for the GC to use 2023-09-24 09:07:52 -04:00
Peter Zhu 36cdf163df Add hash_st_free 2023-09-24 09:07:34 -04:00
Peter Zhu 61a2e9450c Fix memory leak in Hash#rehash for ST hashes
We need to free the old ST table in Hash#rehash.

Co-authored-by: Adam Hess <adamhess1991@gmail.com>
2023-09-23 11:24:41 -04:00
Nobuyoshi Nakada 779cab6655
`RHASH_AR_TABLE` never returns NULL now [ci skip] 2023-09-06 17:46:36 +09:00
Dorian Marié (perso) 8cd62fbc17
[DOC] Typo in Hash#key description "so/no such value" 2023-09-03 01:47:50 +09:00
Dorian Marié (perso) 3887b45f47
[DOC] FIx typo in description of Hash#hash (regardless or/of order) 2023-09-03 01:46:33 +09:00
BurdetteLamar b18462c14b [DOC] Don't suppress autolinks 2023-08-12 10:04:39 -04:00
Nobuyoshi Nakada 8f2bbbbcaf
Add assertion in `RHASH_AR_TABLE_BOUND` 2023-08-03 13:17:41 +09:00
Peter Zhu de327ccb63 Remove RARRAY_PTR_USE_TRANSIENT
RARRAY_PTR_USE now does the same things as RARRAY_PTR_USE_TRANSIENT.
2023-07-13 14:48:14 -04:00
Peter Zhu 58386814a7 Don't check for null pointer in calls to free
According to the C99 specification section 7.20.3.2 paragraph 2:

> If ptr is a null pointer, no action occurs.

So we do not need to check that the pointer is a null pointer.
2023-06-30 09:13:31 -04:00
Peter Zhu d3ff43852b Fix memory leak in Hash#replace
Hash#replace can leak memory if the receiver has an ST table.
2023-06-29 19:07:26 -04:00
Peter Zhu f0d08d11dc Fix memory leak when copying ST tables
st_copy allocates a st_table, which is not needed for hashes since it is
allocated by VWA and embedded, so this causes a memory leak.

The following script demonstrates the issue:

```ruby
20.times do
  100_000.times do
    {a: 1, b: 2, c: 3, d: 4, e: 5, f: 6, g: 7, h: 8, i: 9}
  end

  puts `ps -o rss= -p #{$$}`
end
```
2023-06-29 11:16:50 -04:00
Peter Zhu df2b3a2998 Refactor rb_hash_replace to use hash_copy 2023-06-29 11:16:50 -04:00
Nobuyoshi Nakada 73529a12bb
Declare `RHASH_AR_TABLE` and `RHASH_ST_TABLE` return non-null 2023-06-23 14:54:50 +09:00
Peter Zhu 52f8de4f21 Remove dead code in hash.c
RHASH_TABLE_NULL_P and ar_alloc_table are no longer needed since
all Hash will have AR tables.
2023-06-22 14:08:25 -04:00
Peter Zhu 3168f618cb hash.c no longer needs the transient heap 2023-06-12 14:38:56 -04:00