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

Remove object_id use in Set#flatten #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

amomchilov
Copy link

Found this while auditing uses of object_id in ruby/ruby. Pulled out from ruby/ruby#9276.

This PR replaces look-ups into a Set by object_ids, with an identity Set (see Set#compare_by_identity). This has the same semantics but is faster and doesn't trigger allocation of IDs for these objects.

@amomchilov amomchilov requested a review from knu as a code owner December 18, 2023 18:10
set.each { |e|
if e.is_a?(Set)
if seen.include?(e_id = e.object_id)
unless seen.add?(e)
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's 3 nice performance wins here:

  • We don't trigger a look up (and potentially an assignment) of an object ID
  • We can do an identity-based look-up, which is faster
  • We combine the include? and add steps into one, saving one hash lookup/comparison.

@amomchilov amomchilov force-pushed the flatten_merge-without-object_id branch from b129c80 to 9c12c2c Compare December 18, 2023 18:24
@amomchilov amomchilov changed the title Remove object_id use in OpenStruct#inspect Remove object_id use in Set#flatten Dec 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant