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

Support freezing string literals in templates #1883

Open
mitchellhenke opened this issue Oct 25, 2023 · 2 comments
Open

Support freezing string literals in templates #1883

mitchellhenke opened this issue Oct 25, 2023 · 2 comments

Comments

@mitchellhenke
Copy link
Contributor

Feature request

While I was working with config.action_view.frozen_string_literal in Rails (rails/rails#43725), I noticed that in some of our views we were still allocating identical strings repeatedly. I was kind of curious why, and noticed that it was due to our usage of view_component and string literals in view_component templates were not being frozen. As mentioned in the linked PR, the string literals can be significant parts of templates.

It would be neat to have the performance gain available here too!

Motivation

This would strictly be a performance improvement as I understand it.

@camertron
Copy link
Contributor

camertron commented Nov 1, 2023

Hey @mitchellhenke, fantastic idea, thank you! We would welcome a PR that introduces this capability. I would look specifically to inject the # frozen_string_literal: true comment anywhere we're using class_eval in the compiler, for example here.

I verified that the magic comment works with class_eval as well as eval:

class Foo
  class_eval(<<~END)
    # frozen_string_literal: true
    def foo
      "foo"
    end
  END
end

irb> Foo.new.foo.frozen?
=> true

@camertron
Copy link
Contributor

Oh lol I totally missed the PR you submitted for this, carry on!

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

No branches or pull requests

2 participants