Add hash method to metadata to speed up comparing it in a hash key#2560
Merged
Conversation
https://github.com/ruby/ruby/blob/0623e2b7cc621b1733a760b72af246b06c30cf96/struct.c#L1200-L1203 Signed-off-by: Yuta Iwama <ganmacs@gmail.com>
be4f646 to
079803e
Compare
repeatedly
reviewed
Aug 14, 2019
| # This means that this class is one of the most called object in Fluentd. | ||
| # See https://github.com/fluent/fluentd/pull/2560 | ||
| def hash | ||
| timekey.object_id |
Member
There was a problem hiding this comment.
timekey is sometimes nil, right?
self.object_id is not fit?
Member
There was a problem hiding this comment.
Ah, self.object_id is wong. It doesn't work well.
Member
|
|
Member
Author
|
I think so. the following code returns the same result. A = Struct.new(:l, :c, :r)
B = Struct.new(:l, :c, :r) do
def hash
l.object_id
end
end
a = A.new(nil, 2, 3)
a2 = A.new(nil, 2, 3)
a3 = A.new(nil, 2, 4)
a4 = A.new(1, 2, 4)
a5 = A.new(1, 2, 4)
b = B.new(nil, 2, 3)
b2 = B.new(nil, 2, 3)
b3 = B.new(nil, 2, 4)
b4 = B.new(1, 2, 4)
b5 = B.new(1, 2, 4)
aa = { a => 1, a2 => 2, a3 => 3 }
aa2 = { a => 1 }
bb = { b => 1, b2 => 2, b3 => 3 }
bb2 = { b => 1 }
p (a == a2) == (b == b2)
p (a3 == a2) == (b3 == b2)
p aa[a] == bb[b]
p aa[a2] == bb[b2]
p aa[a3] == bb[b3]
p aa2[a] == bb2[b]
p aa2[a2] == bb2[b2]
p aa2[a3] == bb2[b3]
p (a4 == a5) == (b4 == b5) |
Member
|
If this has a problem with exsiting case, we will revert it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Which issue(s) this PR fixes:
no
What this PR does / why we need it:
Object#hash is called when using an object as the hash key or reference value from a hash object with the object.
Current
Struct's implementation is comparing all inner variables.https://github.com/ruby/ruby/blob/0623e2b7cc621b1733a760b72af246b06c30cf96/struct.c#L1200-L1203
This impl is a bit redundant. Especially in fluentd, it can be overhead.
e.g. Output#handle_stream_simple creates a hash object using an object as a key. this method is called per
emit(one of the most called methods in fluentd).benchmark
Docs Changes:
no need
Release Note:
same as title