diff options
author | schneems <richard.schneeman@gmail.com> | 2015-07-24 22:47:34 -0500 |
---|---|---|
committer | schneems <richard.schneeman@gmail.com> | 2015-07-29 20:41:57 -0500 |
commit | 097ec6fb7c7e1854cdd96113213b555ae7415953 (patch) | |
tree | 22e75ef77f95adfa1c6f80e05aaa80834c692143 /actionpack/lib | |
parent | 9b8258814e695fe7fbb728456498fd0fd8709f5c (diff) | |
download | rails-097ec6fb7c7e1854cdd96113213b555ae7415953.tar.gz rails-097ec6fb7c7e1854cdd96113213b555ae7415953.tar.bz2 rails-097ec6fb7c7e1854cdd96113213b555ae7415953.zip |
Speed up journey missing_keys
Most routes have a `route.path.requirements[key]` of `/[-_.a-zA-Z0-9]+\/[-_.a-zA-Z0-9]+/` yet every time this method is called a new regex is generated on the fly with `/\A#{DEFAULT_INPUT}\Z/`. OBJECT ALLOCATIONS BLERG!
This change uses a special module that implements `===` so it can be used in a case statement to pull out the default input. When this happens, we use a pre-generated regex.
This change buys us 1,643,465 bytes of memory and 7,990 fewer objects per request.
Diffstat (limited to 'actionpack/lib')
-rw-r--r-- | actionpack/lib/action_dispatch/journey/formatter.rb | 18 |
1 files changed, 15 insertions, 3 deletions
diff --git a/actionpack/lib/action_dispatch/journey/formatter.rb b/actionpack/lib/action_dispatch/journey/formatter.rb index 545b67422d..985cf8b947 100644 --- a/actionpack/lib/action_dispatch/journey/formatter.rb +++ b/actionpack/lib/action_dispatch/journey/formatter.rb @@ -111,15 +111,27 @@ module ActionDispatch routes end + module RegexCaseComparator + DEFAULT_INPUT = /[-_.a-zA-Z0-9]+\/[-_.a-zA-Z0-9]+/ + DEFAULT_REGEX = /\A#{DEFAULT_INPUT}\Z/ + + def self.===(regex) + DEFAULT_INPUT == regex + end + end + # Returns an array populated with missing keys if any are present. def missing_keys(route, parts) missing_keys = [] tests = route.path.requirements route.required_parts.each { |key| - if tests.key?(key) - missing_keys << key unless /\A#{tests[key]}\Z/ === parts[key] - else + case tests[key] + when nil missing_keys << key unless parts[key] + when RegexCaseComparator + missing_keys << key unless RegexCaseComparator::DEFAULT_REGEX === parts[key] + else + missing_keys << key unless /\A#{tests[key]}\Z/ === parts[key] end } missing_keys |