Replies: 5 comments 16 replies
-
Hello, I've created a Jira ticket for this here: https://jira.mongodb.org/browse/MONGOID-5805 Please feel free to follow that ticket. The team will discuss it early next week and decide how to proceed at that point. Thank you very much for the detailed benchmarks, graphs, and analysis! |
Beta Was this translation helpful? Give feedback.
-
@sosolidkk from a cursory look at the diff, a likely culprit is the change to the As far as I can see the other Can you try porting/monkey patching # 7.1.1 version of the method, ported to 7.2.6
module Mongoid
module Matcher
module_function def extract_attribute(document, key)
if (key_string = key.to_s) =~ /.+\..+/
key_string.split('.').inject(document.send(:as_attributes)) do |_attribs, _key|
if _attribs.is_a?(::Array)
if _key =~ /\A\d+\z/ && _attribs.none? {|doc| doc.is_a?(Hash)}
_attribs.try(:[], _key.to_i)
else
_attribs.map { |doc| doc.try(:[], _key) }
end
else
_attribs.try(:[], _key)
end
end
else
if document.is_a?(Hash)
document[key_string]
else
document.attributes[key_string]
end
end
end
end
end |
Beta Was this translation helpful? Give feedback.
-
I took a bit of time to look at this, and it seems like the biggest win comes from re-introducing the short circuit that existed in 7.1.11 -- if the key does not look like it includes any dot notation, don't worry about getting the attributes hash or splitting the key on dot characters, just fetch the corresponding attribute. This will be the most common case anyway, and gets the time back down to pre-7.2 speeds for that common case. That's the good news. The bad news is that the 7.x series is no longer being updated. We'll look at making this change and backporting it to the 8.0 branch, but you'll probably need to resort to monkey-patching to add this to 7.x. Maybe something like: module ShortCircuitExtractAttribute
def self.included(base)
base.alias_method :extract_attribute_original, :extract_attribute
base.alias_method :extract_attribute, :extract_attribute_short_circuited
base.send :module_function, :extract_attribute_original
base.send :module_function, :extract_attribute
end
def extract_attribute_short_circuited(doc, key)
return doc.attributes[key] unless key.include?('.')
extract_attribute_original(doc, key)
end
end
Mongoid::Matcher.include ShortCircuitExtractAttribute In my (unscientific) tests, the above actually performs nearly twice as fast in the common case, versus the original (in 7.1.11), mostly because I'm just checking for the existence of a dot, and not for a syntactically-accurate dot. It'll all fall through to the full version anyway, if the simple case doesn't match. Can you try the above and see how it fares for you? Assuming this helps the situation, we'll see about adding this to Mongoid 8+. |
Beta Was this translation helpful? Give feedback.
-
@johnnyshields -- we've had https://jira.mongodb.org/browse/MONGOID-5741 in the backlog for a while. Other issues have taken priority. @sosolidkk -- that's really odd. I don't understand the patch I proposed would trigger that error; it should (?) be returning the same type of value as the original. I'll update the ticket to take a closer look, but it might be a few weeks until we can clear our plates to dig into it deeper. |
Beta Was this translation helpful? Give feedback.
-
We've committed this change, and it will be released with Mongoid 9.0.3 and Mongoid 8.1.7 (perhaps in the next two or three weeks). The final solution had to take into account a few more considerations in order to satisfy the tests, but the performance improvement ought to still be significant. (See PR #5868.) Thank you for all your amazing research here, @sosolidkk! It was immensely helpful. |
Beta Was this translation helpful? Give feedback.
-
Hello. I've been trying to upgrade the application Mongoid version in my current Job to the latest release available version 7x. We're planning to go through each minor release in order.
When we upgraded from Mongoid
7.1.11
to7.2.6
, it was noticed a huge performance issue with our application response time. Fortunately, it was possible to roll back to the previous version. While investigating, we did not notice any obvious issues. I researched and looked for similar cases as ours, but could not find any. The only discussion I've found about a performance issue was this one: #5783.The response time from our application has gone from a mean of
~3.5s
to~12s
with spikes up to~16s
! You can look in more detail in the graph below.So looking at our profiling tool, it seems that it was a general degradation of performance. Every one of our controller actions that use Mongoid models became at least twice slower, especially the ones with embedded objects related. I've run a few simple benchmarks on the Rails console to confirm this and those are the results:
As you can see, the performance hit is huge. Another thing I did was to run a profiler on the find method to see if there was any difference due to the updates. One interesting to notice about the profiler is that as a single find operation, when it ran from version 7.2.6 it was faster than the one in version 7.1.11. But when I ran a lot of those operations in the benchmark, there was this performance issue.
The main 3 points I did notice looking at the profiling result were:
Mongo::Session::SessionPool#prune!
,Mongo::Server::ConnectionPool#closed?
, and related session methods.Mongo::Operation::Executable#build_message
andMongo::Operation::Context#server_api
in the 7.2.6 profile indicates additional layers of processing, possibly related to changes in how operations are constructed and executed.Mongo::Socket#read_from_socket
and associated operations, suggesting more overhead in network communication.These differences suggest that the increase in CPU time could be due to more complex session and connection management, possibly including additional security or validation steps. Additionally, network-related overhead might have increased, contributing to the slower performance.
But I'm not sure how to proceed further or what to do with this information, to be honest. Is there anything I'm missing or overlooking?
I'll attach the profile text files. I've used the
ruby-prof
gem. In case any other information is needed, please let me know.Things I've also tried
7.5.4
directly, but the performance issue still persists.Ruby version:
3.0.7
Rails version:
6.1.7.8
Mongo engine version:
5.0.28
Beta Was this translation helpful? Give feedback.
All reactions