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 OpenStruct#inspect #58

Open
wants to merge 2 commits 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 OpenStruct#inspect is also shared with Set#inspect. This PR provides two implementations of #inspect, depending on whether or not the matching version of set gem is also using an identity set. See ruby/set#33.

# Returns a string containing a detailed summary of the keys and values.
#
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.

@@ -380,27 +380,59 @@ def delete_field(name, &block)
end
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.

lib/ostruct.rb Outdated Show resolved Hide resolved
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.

2 participants