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

Cache Agent type select options in Agent#new #1804

Merged
merged 1 commit into from
Nov 22, 2016

Conversation

dsander
Copy link
Collaborator

@dsander dsander commented Nov 19, 2016

Agent#new was by far the slowest action of all controllers. When using many Agent gems the response time goes up to a point where the user starts to wonder if something is going wrong. By caching the Agent description the response time goes down from about 1 second to 100ms in development.

@@ -82,6 +82,12 @@ def agent_type_icon(agent, agents)
end
end

def agent_type_select_options
Rails.cache.fetch('agent_type_select_options') do
Agent.types.map {|type| [agent_type_to_human(type.name), type, {title: h(Agent.build_for_type(type.name, User.new(id: 0), {}).html_description.lines.first.strip)}] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can there be anything sensitive in html_description? Do we ever inline stuff for the current user? (Caching is a great idea, so we shouldn't inline anything user-specific!)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The descriptions could have sensitive information in them, but they do not at the moment. I am not passing the current_user avoid the risk of leaking and checked the first line of the current agent descriptions to make sure they do not include user specific content.
To completely avoid the problem we would need to cache the data per user (which would increase the memory consumption on multi-user environments).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, that seems fine

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we just make html_description a class method? description is stored in a class, so you are not supposed to include anything specific to the current user in it anyway.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a couple agents that show things like agent.user_id in there to give you example URLs. We could factor these out, but for now it's not really class level.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, of course. Now I recall DataOutputAgent!

@cantino
Copy link
Member

cantino commented Nov 20, 2016

Nice!

@@ -25,7 +25,7 @@
<% if @agent.new_record? %>
<div class="form-group type-select">
<%= f.label :type %>
<%= f.select :type, options_for_select([['Select an Agent Type', 'Agent', {title: ''}]] + Agent.types.map {|type| [agent_type_to_human(type.name), type, {title: h(Agent.build_for_type(type.name,current_user,{}).html_description.lines.first.strip)}] }, @agent.type), {}, class: 'form-control', autofocus: true %>
Copy link
Member

@knu knu Nov 21, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can just wrap this line with <% cache :agent_select do %> and <% end %> instead of creating a helper method.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would also cache the @agent.type.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved [['Select an Agent Type', 'Agent', {title: ''}]] to the helper as well, caching everything with the cache helper does not seem to give significant performance improvements. So I don't think it's worth adding the JavaScript which would be needed to select the Agent type.

Agent#new was by far the slowest action of all controllers. When using
many Agent gems the response time goes up to a point where the user
starts to wonder if something is going wrong. By caching the Agent
description the response time goes down from about 1 second to 100ms
in development.
@dsander
Copy link
Collaborator Author

dsander commented Nov 22, 2016

Thanks!

@dsander dsander merged commit 5d69bd2 into huginn:master Nov 22, 2016
@dsander dsander deleted the cache-agent-type-dropdown branch November 22, 2016 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants