Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Force recycle intermediate collection in Hash#transform_keys! #4329

Merged
merged 2 commits into from
Mar 28, 2021

Conversation

kachick
Copy link
Member

@kachick kachick commented Mar 28, 2021

This PR might be just a question 🙇

ruby/hash.c

Lines 4389 to 4411 in fb6ebe5

static VALUE
rb_hash_compare_by_id(VALUE hash)
{
VALUE tmp;
st_table *identtable;
if (rb_hash_compare_by_id_p(hash)) return hash;
rb_hash_modify_check(hash);
ar_force_convert_table(hash, __FILE__, __LINE__);
HASH_ASSERT(RHASH_ST_TABLE_P(hash));
tmp = hash_alloc(0);
identtable = rb_init_identtable_with_size(RHASH_SIZE(hash));
RHASH_ST_TABLE_SET(tmp, identtable);
rb_hash_foreach(hash, rb_hash_rehash_i, (VALUE)tmp);
st_free_table(RHASH_ST_TABLE(hash));
RHASH_ST_TABLE_SET(hash, identtable);
RHASH_ST_CLEAR(tmp);
rb_gc_force_recycle(tmp);
return hash;
}
looks using this function for the intermediate hash.
So this code needs to same way...? 🤔 (I'm not sure, how to check the changed behaviors in ruby code layer...)

ref: #4294, 5e5fb72

@nobu Could you review? 🙏

@nobu
Copy link
Member

nobu commented Mar 28, 2021

As rb_hash_compare_by_id replaces the ST table with a different type of ST, it is complicated.
An empty Hash would not be a GC pressure so much though, it is better to have.
Ditto for the array pairs.

@kachick kachick changed the title Force recycle intermediate hash [Bug #17735] Force recycle intermediate collection Mar 28, 2021
@kachick kachick changed the title Force recycle intermediate collection Force recycle intermediate collection in Hash#transform_keys! Mar 28, 2021
@kachick
Copy link
Member Author

kachick commented Mar 28, 2021

Thank you for letting me know! 😂
So added for pairs too f435edd

@nobu nobu merged commit 522d4cd into ruby:master Mar 28, 2021
@kachick
Copy link
Member Author

kachick commented Mar 28, 2021

Thank you!

@kachick kachick deleted the tmp-hash-force-gc branch March 28, 2021 05:21
@ko1
Copy link
Contributor

ko1 commented Mar 31, 2021

Please Do not use force_recycle because it is an exceptional operation and slow, now a day . It can violate GC assumptions. I wanted to make it no-op, but I couldn't make it because some code depends on this behavior.

@kachick
Copy link
Member Author

kachick commented Mar 31, 2021

🙇

Please Do not use force_recycle because it is an exceptional operation and slow, now a day . It can violate GC assumptions. I wanted to make it no-op, but I couldn't make it because some code depends on this behavior.

I should note it. Thanks for your alerting 🙏

I have created a PR to just revert this as #4341. Please merge it if it is needed.

@ko1
Copy link
Contributor

ko1 commented Apr 1, 2021

Thank you for your quick patch. It was not your fault, because it is not noted anywhere.
Anyway, thanks again.

matzbot pushed a commit that referenced this pull request Apr 15, 2021
…6ebe55d91187d9635e0183d47dbf38e95b1141,522d4cd32f7727886f4fcbc28ed29c08d361ee20: [Backport #17735]

	Keep non evaluated keys in `Hash#transform_keys!` [Bug #17735]

	---
	 hash.c                                     |  6 +++++-
	 spec/ruby/core/hash/transform_keys_spec.rb | 12 +++++++++++-
	 test/ruby/test_hash.rb                     |  8 ++++++++
	 3 files changed, 24 insertions(+), 2 deletions(-)

	Clear an intermediate hash [Bug #17735]

	---
	 hash.c | 1 +
	 1 file changed, 1 insertion(+)

	Hide an intermediate array

	---
	 hash.c | 6 ++++--
	 1 file changed, 4 insertions(+), 2 deletions(-)

	Force recycle intermediate collection in Hash#transform_keys! [Bug
	 #17735]

	* Force recycle intermediate hash

	* Force recycle intermediate array too

	#4329 (comment)
	---
	 hash.c | 2 ++
	 1 file changed, 2 insertions(+)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
3 participants