-
Notifications
You must be signed in to change notification settings - Fork 52
Identify item by key object instead of key string representation #27
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Conversation
@@ -9,7 +9,7 @@ class ExecutorProxy | |||
def initialize(default_value, key, &block) | |||
@default_value = default_value | |||
@block = block | |||
@block_hash_key = "#{key}#{block.source_location}" | |||
@block_hash_key = [block.source_location, key] |
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.
I decided to reverse key
and block.source_location
, since key
is used to subcategorize.
@exAspArk Please have a look at this contribution! |
@DouweM hey, thank you! I'll take a look tomorrow. |
@DouweM LGTM! Just one thing, could you please write a test which uses instance objects as a |
@exAspArk Test added! |
@DouweM awesome, thanks a lot for your contribution! 🙌 I released your changes in |
Include's Douwe's performance enhancement to identify item by key object instead of key string representation: exAspArk/batch-loader#27
Include's Douwe's performance enhancement to identify item by key object instead of key string representation: exAspArk/batch-loader#27
Imagine I have the following code:
Right now, calling
file_a.name
followed byfile_b.name
will result in two queries being executed, because the string representations ofproject
andsame_project
are different (they contain the actual Rubyobject_id
), which means theblock_hash_key
s will be different as well.With the change in this PR, calling
file_a.name
followed byfile_b.name
will result in only one batch query being executed, sinceproject
andsame_project
have the same#hash
and implement#eql?
to regard each other as identical, which means theblock_hash_key
s will be identical for hash keying purposes.In this specific example, I could also have used
key: project.id
and calledFile.where(project_id: args[:key], name: file_names)
instead ofargs[:key].files.where(name: file_names)
, but that is not an option in some of the batch loaders in the GitLab code base.In general, I think it makes sense for BatchLoader to follow the same equality rules here that Ruby usually would if the user has specifically overwritten
#hash
and#eql?
.