{"action":"closed","number":32418,"pull_request":{"url":"https://api.github.com/repos/rails/rails/pulls/32418","id":178823643,"html_url":"https://github.com/rails/rails/pull/32418","diff_url":"https://github.com/rails/rails/pull/32418.diff","patch_url":"https://github.com/rails/rails/pull/32418.patch","issue_url":"https://api.github.com/repos/rails/rails/issues/32418","number":32418,"state":"closed","locked":false,"title":"Disable ActionView::Template finalizers in test environment","user":{"login":"urbanautomaton","id":383376,"avatar_url":"https://avatars2.githubusercontent.com/u/383376?v=4","gravatar_id":"","url":"https://api.github.com/users/urbanautomaton","html_url":"https://github.com/urbanautomaton","followers_url":"https://api.github.com/users/urbanautomaton/followers","following_url":"https://api.github.com/users/urbanautomaton/following{/other_user}","gists_url":"https://api.github.com/users/urbanautomaton/gists{/gist_id}","starred_url":"https://api.github.com/users/urbanautomaton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/urbanautomaton/subscriptions","organizations_url":"https://api.github.com/users/urbanautomaton/orgs","repos_url":"https://api.github.com/users/urbanautomaton/repos","events_url":"https://api.github.com/users/urbanautomaton/events{/privacy}","received_events_url":"https://api.github.com/users/urbanautomaton/received_events","type":"User","site_admin":false},"body":"### Summary\r\n\r\nTo avoid expensive finalization of `ActionView::Template` instances at the end of large test suites, this adds a configuration option to disable the finalizers, and uses it to disable them in the test environment.\r\n\r\n### Description\r\n\r\nActionView::Template instances compile their source to methods on the ActionView::CompiledTemplates module. To prevent leaks in development mode, where templates can frequently change, a finalizer is added that [undefines these methods](https://github.com/rails/rails/blob/09b2348f7fc8d4e7191e70e06608c5909067e2aa/actionview/lib/action_view/template.rb#L118-L126) when the templates are garbage-collected.\r\n\r\nThis is undesirable in the test environment, however, as templates don't change during the life of the test, so there's no leak to be avoided. Moreover, the cost of undefining a method is proportional to the number of descendants a class or module has, since the method cache must be cleared for [all descendant classes](https://github.com/ruby/ruby/blob/753aa75319282297c41a02d9932b5d88ba575112/vm_method.c#L80).\r\n\r\nAs `ActionView::CompiledTemplates` is mixed into `ActionView::TestCase` (or in RSpec suites, every view example group), it can end up with a very large number of descendants, and undefining its methods can become very expensive. In large test suites, this results in a long delay at the end of the test suite as all template finalizers are run, only for the process to then exit.\r\n\r\nTo avoid this unnecessary cost, this change adds a config option, `action_view.finalize_compiled_template_methods`, defaulting to true, and sets it to false in the test environment only.\r\n\r\n### Other Information\r\n\r\nI've built a small [reproduction of this issue](https://github.com/urbanautomaton/stub_template_perf/tree/finalizing-cost-at-end) using RSpec, illustrating a test suite that has 1,000 templates. For this suite the finalization cost at the end of the run is approximately 30 seconds, which is comparable to the delay seen in the app I'm currently working on (~45s).\r\n\r\n```ruby\r\n# spec/views/repro.html.erb_spec.rb\r\nrequire 'rails_helper'\r\n\r\n(1..1000).each do |i|\r\n  RSpec.describe \"many/#{i}.html.erb\" do\r\n    it 'renders the template' do\r\n      render\r\n\r\n      expect(rendered).to include(\"some-contents-#{i}\")\r\n    end\r\n  end\r\nend\r\n```\r\n\r\n```\r\n$ time rspec spec/views/repro.html.erb_spec.rb\r\n..... [many examples snipped] ....\r\n\r\nFinished in 32.51 seconds (files took 2.17 seconds to load)\r\n1000 examples, 0 failures\r\n\r\n\r\nreal    1m7.304s\r\nuser    1m6.304s\r\nsys     0m0.841s\r\n```\r\n\r\n### Feedback\r\n\r\nI've confirmed that this patch addresses the performance issue. However, I'm not entirely sure that exposing a configuration option is the best thing in this instance, since it's inevitably a rather obscure flag that the vast majority of users probably won't want to touch.\r\n\r\nAny and all feedback is obviously welcome, but in particular if anyone has a better idea for making this environment-specific change, I'd be very open to suggestions.","created_at":"2018-04-02T11:36:54Z","updated_at":"2018-04-02T20:27:22Z","closed_at":"2018-04-02T20:27:22Z","merged_at":"2018-04-02T20:27:22Z","merge_commit_sha":"9facd9a9039d9f9697c78ddb57c6685cb27e78ea","assignee":{"login":"eileencodes","id":1080678,"avatar_url":"https://avatars0.githubusercontent.com/u/1080678?v=4","gravatar_id":"","url":"https://api.github.com/users/eileencodes","html_url":"https://github.com/eileencodes","followers_url":"https://api.github.com/users/eileencodes/followers","following_url":"https://api.github.com/users/eileencodes/following{/other_user}","gists_url":"https://api.github.com/users/eileencodes/gists{/gist_id}","starred_url":"https://api.github.com/users/eileencodes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eileencodes/subscriptions","organizations_url":"https://api.github.com/users/eileencodes/orgs","repos_url":"https://api.github.com/users/eileencodes/repos","events_url":"https://api.github.com/users/eileencodes/events{/privacy}","received_events_url":"https://api.github.com/users/eileencodes/received_events","type":"User","site_admin":true},"assignees":[{"login":"eileencodes","id":1080678,"avatar_url":"https://avatars0.githubusercontent.com/u/1080678?v=4","gravatar_id":"","url":"https://api.github.com/users/eileencodes","html_url":"https://github.com/eileencodes","followers_url":"https://api.github.com/users/eileencodes/followers","following_url":"https://api.github.com/users/eileencodes/following{/other_user}","gists_url":"https://api.github.com/users/eileencodes/gists{/gist_id}","starred_url":"https://api.github.com/users/eileencodes/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/eileencodes/subscriptions","organizations_url":"https://api.github.com/users/eileencodes/orgs","repos_url":"https://api.github.com/users/eileencodes/repos","events_url":"https://api.github.com/users/eileencodes/events{/privacy}","received_events_url":"https://api.github.com/users/eileencodes/received_events","type":"User","site_admin":true}],"requested_reviewers":[],"requested_teams":[],"labels":[],"milestone":null,"commits_url":"https://api.github.com/repos/rails/rails/pulls/32418/commits","review_comments_url":"https://api.github.com/repos/rails/rails/pulls/32418/comments","review_comment_url":"https://api.github.com/repos/rails/rails/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rails/rails/issues/32418/comments","statuses_url":"https://api.github.com/repos/rails/rails/statuses/eede8d8130558cb46aba8736bd63e8a28e37eac2","head":{"label":"urbanautomaton:disable-template-finalizer-in-test","ref":"disable-template-finalizer-in-test","sha":"eede8d8130558cb46aba8736bd63e8a28e37eac2","user":{"login":"urbanautomaton","id":383376,"avatar_url":"https://avatars2.githubusercontent.com/u/383376?v=4","gravatar_id":"","url":"https://api.github.com/users/urbanautomaton","html_url":"https://github.com/urbanautomaton","followers_url":"https://api.github.com/users/urbanautomaton/followers","following_url":"https://api.github.com/users/urbanautomaton/following{/other_user}","gists_url":"https://api.github.com/users/urbanautomaton/gists{/gist_id}","starred_url":"https://api.github.com/users/urbanautomaton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/urbanautomaton/subscriptions","organizations_url":"https://api.github.com/users/urbanautomaton/orgs","repos_url":"https://api.github.com/users/urbanautomaton/repos","events_url":"https://api.github.com/users/urbanautomaton/events{/privacy}","received_events_url":"https://api.github.com/users/urbanautomaton/received_events","type":"User","site_admin":false},"repo":{"id":11690261,"name":"rails","full_name":"urbanautomaton/rails","owner":{"login":"urbanautomaton","id":383376,"avatar_url":"https://avatars2.githubusercontent.com/u/383376?v=4","gravatar_id":"","url":"https://api.github.com/users/urbanautomaton","html_url":"https://github.com/urbanautomaton","followers_url":"https://api.github.com/users/urbanautomaton/followers","following_url":"https://api.github.com/users/urbanautomaton/following{/other_user}","gists_url":"https://api.github.com/users/urbanautomaton/gists{/gist_id}","starred_url":"https://api.github.com/users/urbanautomaton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/urbanautomaton/subscriptions","organizations_url":"https://api.github.com/users/urbanautomaton/orgs","repos_url":"https://api.github.com/users/urbanautomaton/repos","events_url":"https://api.github.com/users/urbanautomaton/events{/privacy}","received_events_url":"https://api.github.com/users/urbanautomaton/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/urbanautomaton/rails","description":"Ruby on Rails","fork":true,"url":"https://api.github.com/repos/urbanautomaton/rails","forks_url":"https://api.github.com/repos/urbanautomaton/rails/forks","keys_url":"https://api.github.com/repos/urbanautomaton/rails/keys{/key_id}","collaborators_url":"https://api.github.com/repos/urbanautomaton/rails/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/urbanautomaton/rails/teams","hooks_url":"https://api.github.com/repos/urbanautomaton/rails/hooks","issue_events_url":"https://api.github.com/repos/urbanautomaton/rails/issues/events{/number}","events_url":"https://api.github.com/repos/urbanautomaton/rails/events","assignees_url":"https://api.github.com/repos/urbanautomaton/rails/assignees{/user}","branches_url":"https://api.github.com/repos/urbanautomaton/rails/branches{/branch}","tags_url":"https://api.github.com/repos/urbanautomaton/rails/tags","blobs_url":"https://api.github.com/repos/urbanautomaton/rails/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/urbanautomaton/rails/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/urbanautomaton/rails/git/refs{/sha}","trees_url":"https://api.github.com/repos/urbanautomaton/rails/git/trees{/sha}","statuses_url":"https://api.github.com/repos/urbanautomaton/rails/statuses/{sha}","languages_url":"https://api.github.com/repos/urbanautomaton/rails/languages","stargazers_url":"https://api.github.com/repos/urbanautomaton/rails/stargazers","contributors_url":"https://api.github.com/repos/urbanautomaton/rails/contributors","subscribers_url":"https://api.github.com/repos/urbanautomaton/rails/subscribers","subscription_url":"https://api.github.com/repos/urbanautomaton/rails/subscription","commits_url":"https://api.github.com/repos/urbanautomaton/rails/commits{/sha}","git_commits_url":"https://api.github.com/repos/urbanautomaton/rails/git/commits{/sha}","comments_url":"https://api.github.com/repos/urbanautomaton/rails/comments{/number}","issue_comment_url":"https://api.github.com/repos/urbanautomaton/rails/issues/comments{/number}","contents_url":"https://api.github.com/repos/urbanautomaton/rails/contents/{+path}","compare_url":"https://api.github.com/repos/urbanautomaton/rails/compare/{base}...{head}","merges_url":"https://api.github.com/repos/urbanautomaton/rails/merges","archive_url":"https://api.github.com/repos/urbanautomaton/rails/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/urbanautomaton/rails/downloads","issues_url":"https://api.github.com/repos/urbanautomaton/rails/issues{/number}","pulls_url":"https://api.github.com/repos/urbanautomaton/rails/pulls{/number}","milestones_url":"https://api.github.com/repos/urbanautomaton/rails/milestones{/number}","notifications_url":"https://api.github.com/repos/urbanautomaton/rails/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/urbanautomaton/rails/labels{/name}","releases_url":"https://api.github.com/repos/urbanautomaton/rails/releases{/id}","deployments_url":"https://api.github.com/repos/urbanautomaton/rails/deployments","created_at":"2013-07-26T16:50:07Z","updated_at":"2018-03-30T22:18:01Z","pushed_at":"2018-04-02T19:50:48Z","git_url":"git://github.com/urbanautomaton/rails.git","ssh_url":"git@github.com:urbanautomaton/rails.git","clone_url":"https://github.com/urbanautomaton/rails.git","svn_url":"https://github.com/urbanautomaton/rails","homepage":"http://rubyonrails.org","size":160338,"stargazers_count":0,"watchers_count":0,"language":"Ruby","has_issues":false,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":0,"mirror_url":null,"archived":false,"open_issues_count":0,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit"},"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"rails:master","ref":"master","sha":"073168358740b33831f0963242ba1de1d0ddb0cc","user":{"login":"rails","id":4223,"avatar_url":"https://avatars1.githubusercontent.com/u/4223?v=4","gravatar_id":"","url":"https://api.github.com/users/rails","html_url":"https://github.com/rails","followers_url":"https://api.github.com/users/rails/followers","following_url":"https://api.github.com/users/rails/following{/other_user}","gists_url":"https://api.github.com/users/rails/gists{/gist_id}","starred_url":"https://api.github.com/users/rails/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rails/subscriptions","organizations_url":"https://api.github.com/users/rails/orgs","repos_url":"https://api.github.com/users/rails/repos","events_url":"https://api.github.com/users/rails/events{/privacy}","received_events_url":"https://api.github.com/users/rails/received_events","type":"Organization","site_admin":false},"repo":{"id":8514,"name":"rails","full_name":"rails/rails","owner":{"login":"rails","id":4223,"avatar_url":"https://avatars1.githubusercontent.com/u/4223?v=4","gravatar_id":"","url":"https://api.github.com/users/rails","html_url":"https://github.com/rails","followers_url":"https://api.github.com/users/rails/followers","following_url":"https://api.github.com/users/rails/following{/other_user}","gists_url":"https://api.github.com/users/rails/gists{/gist_id}","starred_url":"https://api.github.com/users/rails/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rails/subscriptions","organizations_url":"https://api.github.com/users/rails/orgs","repos_url":"https://api.github.com/users/rails/repos","events_url":"https://api.github.com/users/rails/events{/privacy}","received_events_url":"https://api.github.com/users/rails/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rails/rails","description":"Ruby on Rails","fork":false,"url":"https://api.github.com/repos/rails/rails","forks_url":"https://api.github.com/repos/rails/rails/forks","keys_url":"https://api.github.com/repos/rails/rails/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rails/rails/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rails/rails/teams","hooks_url":"https://api.github.com/repos/rails/rails/hooks","issue_events_url":"https://api.github.com/repos/rails/rails/issues/events{/number}","events_url":"https://api.github.com/repos/rails/rails/events","assignees_url":"https://api.github.com/repos/rails/rails/assignees{/user}","branches_url":"https://api.github.com/repos/rails/rails/branches{/branch}","tags_url":"https://api.github.com/repos/rails/rails/tags","blobs_url":"https://api.github.com/repos/rails/rails/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rails/rails/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rails/rails/git/refs{/sha}","trees_url":"https://api.github.com/repos/rails/rails/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rails/rails/statuses/{sha}","languages_url":"https://api.github.com/repos/rails/rails/languages","stargazers_url":"https://api.github.com/repos/rails/rails/stargazers","contributors_url":"https://api.github.com/repos/rails/rails/contributors","subscribers_url":"https://api.github.com/repos/rails/rails/subscribers","subscription_url":"https://api.github.com/repos/rails/rails/subscription","commits_url":"https://api.github.com/repos/rails/rails/commits{/sha}","git_commits_url":"https://api.github.com/repos/rails/rails/git/commits{/sha}","comments_url":"https://api.github.com/repos/rails/rails/comments{/number}","issue_comment_url":"https://api.github.com/repos/rails/rails/issues/comments{/number}","contents_url":"https://api.github.com/repos/rails/rails/contents/{+path}","compare_url":"https://api.github.com/repos/rails/rails/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rails/rails/merges","archive_url":"https://api.github.com/repos/rails/rails/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rails/rails/downloads","issues_url":"https://api.github.com/repos/rails/rails/issues{/number}","pulls_url":"https://api.github.com/repos/rails/rails/pulls{/number}","milestones_url":"https://api.github.com/repos/rails/rails/milestones{/number}","notifications_url":"https://api.github.com/repos/rails/rails/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rails/rails/labels{/name}","releases_url":"https://api.github.com/repos/rails/rails/releases{/id}","deployments_url":"https://api.github.com/repos/rails/rails/deployments","created_at":"2008-04-11T02:19:47Z","updated_at":"2018-04-02T19:46:10Z","pushed_at":"2018-04-02T20:27:22Z","git_url":"git://github.com/rails/rails.git","ssh_url":"git@github.com:rails/rails.git","clone_url":"https://github.com/rails/rails.git","svn_url":"https://github.com/rails/rails","homepage":"http://rubyonrails.org","size":160553,"stargazers_count":39161,"watchers_count":39161,"language":"Ruby","has_issues":true,"has_projects":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":15829,"mirror_url":null,"archived":false,"open_issues_count":1069,"license":{"key":"mit","name":"MIT License","spdx_id":"MIT","url":"https://api.github.com/licenses/mit"},"forks":15829,"open_issues":1069,"watchers":39161,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rails/rails/pulls/32418"},"html":{"href":"https://github.com/rails/rails/pull/32418"},"issue":{"href":"https://api.github.com/repos/rails/rails/issues/32418"},"comments":{"href":"https://api.github.com/repos/rails/rails/issues/32418/comments"},"review_comments":{"href":"https://api.github.com/repos/rails/rails/pulls/32418/comments"},"review_comment":{"href":"https://api.github.com/repos/rails/rails/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rails/rails/pulls/32418/commits"},"statuses":{"href":"https://api.github.com/repos/rails/rails/statuses/eede8d8130558cb46aba8736bd63e8a28e37eac2"}},"author_association":"CONTRIBUTOR","merged":true,"mergeable":null,"rebaseable":null,"mergeable_state":"unknown","merged_by":{"login":"rafaelfranca","id":47848,"avatar_url":"https://avatars3.githubusercontent.com/u/47848?v=4","gravatar_id":"","url":"https://api.github.com/users/rafaelfranca","html_url":"https://github.com/rafaelfranca","followers_url":"https://api.github.com/users/rafaelfranca/followers","following_url":"https://api.github.com/users/rafaelfranca/following{/other_user}","gists_url":"https://api.github.com/users/rafaelfranca/gists{/gist_id}","starred_url":"https://api.github.com/users/rafaelfranca/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rafaelfranca/subscriptions","organizations_url":"https://api.github.com/users/rafaelfranca/orgs","repos_url":"https://api.github.com/users/rafaelfranca/repos","events_url":"https://api.github.com/users/rafaelfranca/events{/privacy}","received_events_url":"https://api.github.com/users/rafaelfranca/received_events","type":"User","site_admin":false},"comments":7,"review_comments":1,"maintainer_can_modify":false,"commits":1,"additions":33,"deletions":1,"changed_files":5}}
{"actor":{"display_login":"rafaelfranca"}}