Skip to content

Commit

Permalink
Remove (another) array allocation
Browse files Browse the repository at this point in the history
We don't always need an array when generating a url with the formatter. We can be lazy about allocating the `missing_keys` array. This saves us:

35,606 bytes and 889 objects per request
  • Loading branch information
schneems committed Jul 30, 2015
1 parent 4d2ccc1 commit 61dae88
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions actionpack/lib/action_dispatch/journey/formatter.rb
Expand Up @@ -25,7 +25,7 @@ def generate(name, options, path_parameters, parameterize = nil)
next unless name || route.dispatcher?

missing_keys = missing_keys(route, parameterized_parts)
next unless missing_keys.empty?
next if missing_keys && missing_keys.any?
params = options.dup.delete_if do |key, _|
parameterized_parts.key?(key) || route.defaults.key?(key)
end
Expand Down Expand Up @@ -122,16 +122,25 @@ def self.===(regex)

# Returns an array populated with missing keys if any are present.
def missing_keys(route, parts)
missing_keys = []
missing_keys = nil
tests = route.path.requirements
route.required_parts.each { |key|
case tests[key]
when nil
missing_keys << key unless parts[key]
unless parts[key]
missing_keys ||= []
missing_keys << key
end
when RegexCaseComparator
missing_keys << key unless RegexCaseComparator::DEFAULT_REGEX === parts[key]
unless RegexCaseComparator::DEFAULT_REGEX === parts[key]
missing_keys ||= []
missing_keys << key
end
else
missing_keys << key unless /\A#{tests[key]}\Z/ === parts[key]
unless /\A#{tests[key]}\Z/ === parts[key]
missing_keys ||= []
missing_keys << key
end
end
}
missing_keys
Expand Down

1 comment on commit 61dae88

@jonatack
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! 😃

Please sign in to comment.