-
Notifications
You must be signed in to change notification settings - Fork 896
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
Refactor Api::Initializer #14624
Refactor Api::Initializer #14624
Conversation
It is lazily evaluated, so no reason not to do this here.
Checked commits imtayadeway/manageiq@30bd885~...edbc7c9 with ruby 2.2.6, rubocop 0.47.1, and haml-lint 0.20.0 |
@Fryguy what was the tool you used to keep track of allocations upon instantiating rails/workers ? |
@imtayadeway see ManageIQ/manageiq-gems-pending#123 for info about webtreemap, would be nice to see the numbers for the Api Controller before and after. Thanks |
@@ -1,7 +1,30 @@ | |||
module Api | |||
class Environment | |||
def self.normalized_attributes | |||
@normalized_attributes ||= {:time => {}, :url => {}, :resource => {}, :encrypted => {}} | |||
@normalized_attributes ||= { | |||
:time => time_attributes.each_with_object({}) { |attr, hsh| hsh[attr] = true }, |
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.
Out of curiosity, is the true
value used at all? If not, perhaps it would be better to use a Set over a Hash in all of these cases.
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.
@Fryguy I thought the same thing, but since hash lookup was faster I assumed it was there for that reason. Could we sacrifice a small performance gain for a more appropriate data structure?
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.
A Set is just a Hash under the covers, so you don't sacrifice anything.
https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L84
https://github.com/ruby/ruby/blob/trunk/lib/set.rb#L28-L46
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.
@Fryguy I thought this too, but when I benchmarked it was consistently slower (compared with SortedSet too). Nonetheless, I'll put a PR together so perhaps we can discuss this better there
end | ||
|
||
def self.time_attributes | ||
@time_attributes ||= begin |
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.
The begin/end is redundant here. Also I'm not a fan of the mega indent, personally...I prefer
def self.time_attributes
@time_attributes ||=
ApiConfig.collection... do
end
end
@imtayadeway I'm going to merge this anyway. Let's take my other comments into a separate PR. |
@imtayadeway I assume there is no BZ for this? Not sure if this is one of those changes that any API call will go through this and no specific test is needed (in which case, I don't think BZ is needed), or if there are something QE will need to test to cover the changes made in this PR? |
@simaishi no BZ for this. This PR was really just about code - the discussion about performance only relates to ensuring that there were no regressions in the refactoring. The existing tests should ensure that everything "still works" ;) |
For legacy reasons there were some quite expensive operations that were wrapped in the
Api::Initializer
class so their execution could be delayed until the first request. This code generates some data and uses that data to populate things likeApi::Environment.normalized_attributes
. By moving that code intoApi::Environment
and allowing it to be lazily evaluated, we achieve the same effect. What remains is merely logging which I've moved into the top level lib/api.rb, eliminating the need for this class.To show that we are still delaying population of the
normalized_attributes
data until required, here's what happens when I open the console:As you can see, Rails has successfully booted without calling this expensive operation
This also allowed for a few ✂️ ✂️ ✂️
@miq-bot add-label api, refactoring
@miq-bot assign @abellotti
/cc @Fryguy