Skip to content

Conversation

Alexander-Senko
Copy link
Contributor

A controller used to render a prompt template should be a renderer’s attribute instead of being set up for each rendering.

@evdevdev evdevdev requested a review from Copilot June 10, 2025 14:17
Copy link

@Copilot Copilot AI left a 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 in initialize
  • 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: {})

Comment on lines 3 to +9
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)
Copy link
Preview

Copilot AI Jun 10, 2025

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.

Comment on lines +4 to +9
@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)
Copy link
Preview

Copilot AI Jun 10, 2025

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.

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.

1 participant