-
Notifications
You must be signed in to change notification settings - Fork 1
Optimization: reuse a controller for rendering #28
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
base: main
Are you sure you want to change the base?
Conversation
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.
Pull Request Overview
This PR optimizes prompt rendering by instantiating and configuring a single controller once instead of recreating it on every render call.
- Instantiate
ApplicationController
and configure its view path ininitialize
- Update
render
to reuse the cached controller instance
Comments suppressed due to low confidence (1)
lib/action_prompt/renderer.rb:8
- Add or update tests to cover multiple sequential
render
calls to ensure caching the controller doesn’t introduce unintended side effects.
def render(template_name, locals: {})
def initialize | ||
# NOOP | ||
@controller = ApplicationController.new | ||
@controller.prepend_view_path(Rails.root.join("app", "prompts")) | ||
end | ||
|
||
def render(template_name, locals: {}) | ||
controller = ApplicationController.new | ||
controller.prepend_view_path(Rails.root.join("app", "prompts")) | ||
controller.render_to_string(template: template_name, locals: locals, layout: false) | ||
@controller.render_to_string(template: template_name, locals: locals, layout: false) |
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.
Consider using Rails’ built-in renderer (ApplicationController.renderer
)—it’s thread-safe and avoids manually instantiating controllers.
Copilot uses AI. Check for mistakes.
@controller = ApplicationController.new | ||
@controller.prepend_view_path(Rails.root.join("app", "prompts")) | ||
end | ||
|
||
def render(template_name, locals: {}) | ||
controller = ApplicationController.new | ||
controller.prepend_view_path(Rails.root.join("app", "prompts")) | ||
controller.render_to_string(template: template_name, locals: locals, layout: false) | ||
@controller.render_to_string(template: template_name, locals: locals, layout: false) |
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.
Reusing a single controller instance may introduce state leaks or thread-safety issues under concurrent requests; consider using a fresh renderer per call or a thread-local instance.
Copilot uses AI. Check for mistakes.
A controller used to render a prompt template should be a renderer’s attribute instead of being set up for each rendering.