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

MONGOID-5805 Short-circuit the logic in extract_attribute to fix performance regression (backport to 8.1-stable) #5869

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion lib/mongoid/matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,24 @@ module Matcher
# from and behaves identically to association traversal for the purposes
# of, for example, subsequent array element retrieval.
#
# @param [ Document | Hash ] document The document to extract from.
# @param [ Document | Hash | String ] document The document to extract from.
# @param [ String ] key The key path to extract.
#
# @return [ Object | Array ] Field value or values.
module_function def extract_attribute(document, key)
# The matcher system will wind up sending atomic values to this as well,
# when attepting to match more complex types. If anything other than a
# Document or a Hash is given, we'll short-circuit the logic and just
# return an empty array.
return [] unless document.is_a?(Hash) || document.is_a?(Document)

# Performance optimization; if the key does not include a '.' character,
# it must reference an immediate attribute of the document.
unless key.include?('.')
hash = document.respond_to?(:attributes) ? document.attributes : document
return [ hash[key] ] if hash.key?(key)
end

if document.respond_to?(:as_attributes, true)
# If a document has hash fields, as_attributes would keep those fields
# as Hash instances which do not offer indifferent access.
Expand Down
4 changes: 4 additions & 0 deletions spec/mongoid/association/referenced/belongs_to/proxy_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,10 @@
person.save!
end

# NOTE: there as a bad interdependency here, with the auto_save_spec.rb
# file. If auto_save_spec.rb runs before this, the following specs fail
# with "undefined method `nullify' for an instance of Person".

context "when parent exists" do

context "when child is destroyed" do
Expand Down
Loading