Skip to content

Commit

Permalink
short-circuit the logic in extract_attribute to fix performance regre…
Browse files Browse the repository at this point in the history
…ssion
  • Loading branch information
jamis committed Sep 20, 2024
1 parent fc09714 commit 9927916
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/mongoid/matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ module Matcher
#
# @return [ Object | Array ] Field value or values.
module_function def extract_attribute(document, key)
# 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 @@ -751,6 +751,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

0 comments on commit 9927916

Please sign in to comment.