-
Notifications
You must be signed in to change notification settings - Fork 15
Remove object_id use in Set#inspect
#33
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
base: master
Are you sure you want to change the base?
Conversation
| # 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) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using an identity Set here matches what pp does:
099bd87 to
a045f65
Compare
| to_a.join(separator) | ||
| end | ||
|
|
||
| def self.__inspect_supports_identity_set? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is checked by the corresponding PR in ostruct:
| # 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. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| # 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. |
Found this while auditing uses of
object_idinruby/ruby. Pulled out from ruby/ruby#9276.This PR replaces look-ups into an Array by
object_ids, with an identity Set (seeSet#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 bySet#inspectis also shared withOpenStruct#inspect. This PR provides two implementations of#inspect, depending on whether or not the matching version of theostructgem is also using an identity set. See ruby/ostruct#58