{"action":"created","issue":{"url":"https://api.github.com/repos/rails/rails/issues/26599","repository_url":"https://api.github.com/repos/rails/rails","labels_url":"https://api.github.com/repos/rails/rails/issues/26599/labels{/name}","comments_url":"https://api.github.com/repos/rails/rails/issues/26599/comments","events_url":"https://api.github.com/repos/rails/rails/issues/26599/events","html_url":"https://github.com/rails/rails/pull/26599","id":178878116,"number":26599,"title":"Call ENV.fetch with a default parameter, not a block, in generated configs","user":{"login":"timrogers","id":116134,"avatar_url":"https://avatars.githubusercontent.com/u/116134?v=3","gravatar_id":"","url":"https://api.github.com/users/timrogers","html_url":"https://github.com/timrogers","followers_url":"https://api.github.com/users/timrogers/followers","following_url":"https://api.github.com/users/timrogers/following{/other_user}","gists_url":"https://api.github.com/users/timrogers/gists{/gist_id}","starred_url":"https://api.github.com/users/timrogers/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/timrogers/subscriptions","organizations_url":"https://api.github.com/users/timrogers/orgs","repos_url":"https://api.github.com/users/timrogers/repos","events_url":"https://api.github.com/users/timrogers/events{/privacy}","received_events_url":"https://api.github.com/users/timrogers/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-09-23T13:30:06Z","updated_at":"2016-09-23T15:22:30Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/rails/rails/pulls/26599","html_url":"https://github.com/rails/rails/pull/26599","diff_url":"https://github.com/rails/rails/pull/26599.diff","patch_url":"https://github.com/rails/rails/pull/26599.patch"},"body":"In config files generated by `rails new` (notably `database.yml` and `puma.rb`), we're using `ENV.fetch` with a default specified using a block.\r\n\r\n`ENV.fetch` also supports specifying a default with a second parameter, and this is significantly faster. See the following benchmarks:\r\n\r\n```ruby\r\nrequire 'benchmark/ips'\r\n\r\nBenchmark.ips do |x|\r\n  x.report(\"ENV.fetch with block\") do\r\n    ENV.fetch(\"RAILS_MAX_THREADS\") { 5 }\r\n  end\r\n\r\n  x.report(\"ENV.fetch with default arg\") do\r\n    ENV.fetch(\"RAILS_MAX_THREADS\", 5)\r\n  end\r\n\r\n  x.compare!\r\nend\r\n```\r\n\r\n```\r\nWarming up --------------------------------------\r\nENV.fetch with block   135.020k i/100ms\r\nENV.fetch with default arg\r\n                       146.578k i/100ms\r\nCalculating -------------------------------------\r\nENV.fetch with block      2.381M (± 3.9%) i/s -     12.017M in   5.054137s\r\nENV.fetch with default arg\r\n                          2.991M (± 3.8%) i/s -     14.951M in   5.006166s\r\n\r\nComparison:\r\nENV.fetch with default arg:  2990871.0 i/s\r\nENV.fetch with block:  2381259.4 i/s - 1.26x  slower\r\n```\r\n\r\n*Unless there's a reason I don't understand which makes the block necessary*, this seems worth the extremely minor change! It's just something I spotted when creating a new app."},"comment":{"url":"https://api.github.com/repos/rails/rails/issues/comments/249222099","html_url":"https://github.com/rails/rails/pull/26599#issuecomment-249222099","issue_url":"https://api.github.com/repos/rails/rails/issues/26599","id":249222099,"user":{"login":"schneems","id":59744,"avatar_url":"https://avatars.githubusercontent.com/u/59744?v=3","gravatar_id":"","url":"https://api.github.com/users/schneems","html_url":"https://github.com/schneems","followers_url":"https://api.github.com/users/schneems/followers","following_url":"https://api.github.com/users/schneems/following{/other_user}","gists_url":"https://api.github.com/users/schneems/gists{/gist_id}","starred_url":"https://api.github.com/users/schneems/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/schneems/subscriptions","organizations_url":"https://api.github.com/users/schneems/orgs","repos_url":"https://api.github.com/users/schneems/repos","events_url":"https://api.github.com/users/schneems/events{/privacy}","received_events_url":"https://api.github.com/users/schneems/received_events","type":"User","site_admin":false},"created_at":"2016-09-23T15:22:30Z","updated_at":"2016-09-23T15:22:30Z","body":"I took a look at a few of those `fetch` call sites and there is a reason we use the block form over the argument form. In some of the cases we are performing work in the block. In the first one we are calling `send` on a method which is slow, by using a block it's being done lazilly and we only hit the slow case when the key doesn't exist, if we were using the argument form factor we would have to call that method every time whether or not it was being used. "}}
{"actor":{"display_login":"schneems"}}