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#inspect #33

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

Conversation

amomchilov
Copy link

@amomchilov amomchilov commented Dec 18, 2023

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

This PR replaces look-ups into an Array 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.

On quirk here is that the Thread.current[:__inspect_key__] value used by Set#inspect is also shared with OpenStruct#inspect. This PR provides two implementations of #inspect, depending on whether or not the matching version of the ostruct gem is also using an identity set. See ruby/ostruct#58

# Returns a string containing a human-readable representation of the
# set ("#<Set: {element1, element2, ...}>").
def inspect
ids = (Thread.current[InspectKey] ||= Set.new.compare_by_identity)
Copy link
Author

Choose a reason for hiding this comment

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

@@ -812,23 +812,51 @@ def join(separator=nil)
to_a.join(separator)
end

def self.__inspect_supports_identity_set?
Copy link
Author

Choose a reason for hiding this comment

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

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