Permalink
Browse files

Avoid calling to_s on nil in journey/formatter

When `defaults[key]` in `generate` in the journey formatter is called, it often returns a `nil` when we call `to_s` on a nil, it allocates an empty string. We can skip this check when the default value is nil.

This change buys us 35,431 bytes of memory and 887 fewer objects per request.

Thanks to @matthewd for help with the readability
  • Loading branch information...
schneems committed Jul 25, 2015
1 parent e76a843 commit bff61ba27cb113b90f210a30173a8e66602615ca
Showing with 2 additions and 2 deletions.
  1. +2 −2 actionpack/lib/action_dispatch/journey/formatter.rb
@@ -32,8 +32,8 @@ def generate(name, options, path_parameters, parameterize = nil)
defaults = route.defaults
required_parts = route.required_parts
parameterized_parts.delete_if do |key, value|
value.to_s == defaults[key].to_s && !required_parts.include?(key)
parameterized_parts.keep_if do |key, value|
defaults[key].nil? || value.to_s != defaults[key].to_s || required_parts.include?(key)
end
return [route.format(parameterized_parts), params]

0 comments on commit bff61ba

Please sign in to comment.