Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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.

@amomchilov amomchilov force-pushed the inspect-without-object_id branch from 099bd87 to a045f65 Compare December 18, 2023 18:25
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.

Comment on lines +821 to +822
# We share the same `ids` value as `OpenStruct#inspect`, so we need to use an identity Set
# only we're using a newer version of the `ostruct` gem which is also using an 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.

Suggested change
# We share the same `ids` value as `OpenStruct#inspect`, so we need to use an identity Set
# only we're using a newer version of the `ostruct` gem which is also using an identity Set.
# We share the same thread-local `ids` value as `OpenStruct#inspect`, so we need to use an identity Set
# only when we're using a newer version of the `ostruct` gem which is also using an identity Set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant