aboutsummaryrefslogtreecommitdiffstats
path: root/actionpack/lib/action_dispatch/routing/route_set.rb
Commit message (Collapse)AuthorAgeFilesLines
* provide a request and response to all controllersAaron Patterson2015-08-251-4/+6
| | | | | | | | | | Controllers should always have a request and response when responding. Since we make this The Rule(tm), then controllers don't need to be somewhere in limbo between "asking a response object for a rack response" or "I, myself contain a rack response". This duality leads to conditionals spread through the codebase that we can delete: * https://github.com/rails/rails/blob/85a78d9358aa728298cd020cdc842b55c16f9549/actionpack/lib/action_controller/metal.rb#L221-L223
* adding a direct dispatch method to controller classesAaron Patterson2015-08-251-1/+1
| | | | This saves a lambda and request allocation on each request.
* always dispatch to controllers the same wayAaron Patterson2015-08-251-2/+16
| | | | | controllers should always go through the `action` class method so that their middleware is respected.
* always return a controller class from the `controller_class` methodAaron Patterson2015-08-251-3/+1
| | | | | now the caller can just treat it like a regular controller even though it will return a 404
* pull up dispatcher allocationAaron Patterson2015-08-241-4/+0
| | | | | the dispatcher class isn't configurable anymore, so pull up allocation to the method that needs it.
* directly ask the request for the controller classAaron Patterson2015-08-241-6/+1
| | | | | | Now that we don't have subclasses depending on this method (they augment the request class instead of the dispatch class) we can remove this method and directly ask the request object for the controller class
* remove useless ivarAaron Patterson2015-08-241-2/+1
|
* remove setter for the dispatcher classAaron Patterson2015-08-241-2/+2
| | | | we don't need it anymore. We always use the same dispatcher in tests.
* use a custom request class to determine the controller classAaron Patterson2015-08-241-2/+7
| | | | | | controller class resolution has been moved to the request object, so we should override that method instead of relying on the RouteSet to generate the controller class.
* Remove unused block argumentsdeepj2015-08-231-2/+2
|
* Fix Railties test failure for asset routeseileencodes2015-08-221-1/+5
| | | | | | | | | Since none of the action pack tests failed without this conditional it didn't seem necessary. This fixes the build because it correctly returns a 404 instead of a 500 for the asset routes test. Test that was failing was in the `assets_test.rb` file and was the test named `test_assets_routes_are_not_drawn_when_compilation_is_disabled`.
* Refactor to remove controller class from route to requesteileencodes2015-08-221-40/+8
| | | | | | | | | | This refactoring moves the controller class name that was on the route set to the request. The purpose of this refactoring is for changes we need to move controller tests to integration tests, mainly being able to access the controller on the request instead of having to go through the router. [Eileen M. Uchitelle & Aaron Patterson]
* Remove unnecessary cachingeileencodes2015-08-211-5/+1
| | | | | | | `ActiveSupport::Dependencies.constantize(const_name)` calls `Reference.new` which is defined as `ActiveSupport::Dependencies.constantize(const_name)` meaning this call is already cached and we're doing caching that isn't necessary.
* make the routes reader privateAaron Patterson2015-08-181-0/+1
| | | | | nobody should be touching the routes hash without going through the NamedRouteCollection object.
* only keep one hash of named routesAaron Patterson2015-08-141-1/+2
| | | | | The outer router object already keeps a hash of named routes, so we should just use that.
* rm add_route2Aaron Patterson2015-08-141-1/+1
| | | | | refactor the tests with a backwards compatible method call so we can rm add_route2 from the journey router
* pass pass the mapping object down the add_route stackAaron Patterson2015-08-141-47/+1
| | | | | then we can let the mapping object derive stuff that the Route object needs.
* pass the mapping object to build_routeAaron Patterson2015-08-141-5/+5
| | | | | now that we aren't doing options manipulations, we can just pass the mapping object down and read values from it.
* pass the path ast downAaron Patterson2015-08-141-3/+2
| | | | | now we don't need to add it to a hash and delete it from the hash later just to pass it around
* stop adding path_info to the conditions hashAaron Patterson2015-08-141-1/+0
| | | | we don't need to keep adding it and deleting if from hashes.
* `build_path` doesn't need the path variable anymoreAaron Patterson2015-08-131-2/+2
| | | | | It just constructs a Path::Pattern object with the AST that it already has
* remove StrexpAaron Patterson2015-08-131-7/+1
| | | | | This was a useless object. We can just directly construct a Path::Pattern object without a Strexp object.
* pass anchor directly to `Pattern`Aaron Patterson2015-08-131-3/+2
| | | | | the caller already has it, there is no reason to pack it in to an object and just throw that object away.
* we already have access to the AST, so just use itAaron Patterson2015-08-131-3/+3
|
* remove default arguments that aren't usedAaron Patterson2015-08-131-1/+1
| | | | | we always pass all parameters, so there is no reason to provide default arguments.
* Remove wrong commentRafael Mendonça França2015-08-091-1/+0
| | | | | This method raises conditionally not always so we should not documment as it always raise.
* Remove the conditional since it is done in the methodRafael Mendonça França2015-08-091-4/+4
|
* Execute the block when the controller doesn't existRafael Mendonça França2015-08-091-0/+1
| | | | | We should return when the contoller key is not present or if the controller doesn't exist and we didn't raised an error.
* stop calling `scope` internallyAaron Patterson2015-08-081-6/+1
| | | | | | | we need to get a grip on what `scope` actually does. This commit removes some of the internal calls to `scope`. Eventually we should add public facing methods that provide the API that `scope` is trying to accomplish.
* remove useless conditionalAaron Patterson2015-08-081-1/+1
| | | | | | `prepare_params!` would raise an exception if `params` wasn't initialized, so it must always be available. Remove the existence conditional from the `controller` method.
* eliminate assignment in conditionalAaron Patterson2015-08-081-1/+3
| | | | | The method we called already has the conditional we need. Just add an else block so that we don't need two tests.
* Remove `defaults` hash from `Dispatcher`Aaron Patterson2015-08-081-7/+7
| | | | | | | `Dispatcher` doesn't need to hold on to the defaults hash. It only used the hash to determine whether or not it should raise an exception if there is a name error. We can pass that in further up the stack and alleviate Dispatcher from knowing about that hash.
* Merge pull request #21167 from AaronLasseigne/use_each_keyKasper Timm Hansen2015-08-081-1/+1
|\ | | | | replace each with each_key when only the key is needed
| * replace each with each_key when only the key is neededAaron Lasseigne2015-08-081-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Using each_key is faster and more intention revealing. Calculating ------------------------------------- each 31.378k i/100ms each_key 33.790k i/100ms ------------------------------------------------- each 450.225k (± 7.0%) i/s - 2.259M each_key 494.459k (± 6.3%) i/s - 2.467M Comparison: each_key: 494459.4 i/s each: 450225.1 i/s - 1.10x slower
* | Allow a custom dispatcher to be provided to routing.Xavier Shay2015-08-071-2/+3
| |
* | Move `controller_reference` and `controller_class_names` to protectedXavier Shay2015-08-071-2/+6
| | | | | | | | scope so that they are available to subclasses.
* | the request class is never changed, so just use it directly in the method bodyAaron Patterson2015-08-071-3/+3
|/
* Merge pull request #21061 from yui-knk/refactor/route_setRafael Mendonça França2015-08-061-4/+2
|\ | | | | Remove duplicated `Array#to_param`
| * Remove duplicated `Array#to_param`yui-knk2015-08-011-4/+2
| | | | | | | | | | `Array#to_param` is defind in active_support/core_ext/object/to_query.rb, so we can call `to_param` if value is_a Array.
* | Use #start_with? and #[] for speedBenjamin Quorning2015-08-021-2/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | While the readability may be slightly worse, the speed improvement is significant: Twice as fast when there's no leading "/" to remove, and over 4 times as fast when there is a leading "/". Benchmark: require 'benchmark/ips' def match(controller) if controller if m = controller.match(/\A\/(?<controller_without_leading_slash>.*)/) m[:controller_without_leading_slash] else controller end end end def start_with(controller) if controller if controller.start_with?('/'.freeze) controller[1..-1] else controller end end end Benchmark.ips do |x| x.report("match") { match("no_leading_slash") } x.report("start_with") { start_with("no_leading_slash") } x.compare! end Benchmark.ips do |x| x.report("match") { match("/a_leading_slash") } x.report("start_with") { start_with("/a_leading_slash") } x.compare! end Result (Ruby 2.2.2): Calculating ------------------------------------- match 70.324k i/100ms start_with 111.264k i/100ms ------------------------------------------------- match 1.468M (± 7.1%) i/s - 7.314M start_with 3.787M (± 3.5%) i/s - 18.915M Comparison: start_with: 3787389.4 i/s match: 1467636.4 i/s - 2.58x slower Calculating ------------------------------------- match 36.694k i/100ms start_with 86.071k i/100ms ------------------------------------------------- match 532.795k (± 4.7%) i/s - 2.679M start_with 2.518M (± 5.8%) i/s - 12.566M Comparison: start_with: 2518366.8 i/s match: 532794.5 i/s - 4.73x slower
* | Avoid hash duplication by skipping mutationschneems2015-07-301-2/+2
| | | | | | | | | | | | If we don't mutate the `recall` hash, then there's no reason to duplicate it. While this change doesn't get rid of that many objects, each hash object it gets rid of was massive. Saves 888 string objects per request, 206,013 bytes (thats 0.2 mb which is kinda a lot).
* | Only allocate new string when neededschneems2015-07-301-1/+7
| | | | | | | | | | | | Instead of calling `sub` on every link_to call for controller, we can detect when the string __needs__ to be allocated and only then create a new string (without the leading slash), otherwise, use the string that is given to us. Saves 888 string objects per request, 35,524 bytes.
* | Freeze a string in comparatorschneems2015-07-301-1/+1
| | | | | | | | Saves 888 string objects per request.
* | Reduce hash allocations in route_setschneems2015-07-291-1/+1
| | | | | | | | | | | | | | | | When generating a url with `url_for` the hash of arguments passed in, is dup-d and merged a TON. I wish I could clean this up better, and might be able to do it in the future. This change removes one dup, since it's literally right after we just dup-d the hash to pass into this constructor. This may be a breaking, change but the tests pass...so :shipit: we can revert if it causes problems This change buys us 205,933 bytes of memory and 887 fewer objects per request.
* | Decrease route_set allocationsschneems2015-07-291-3/+7
|/ | | | | | | | In handle_positional_args `Array#-=` is used which allocates a new array. Instead we can iterate through and delete elements, modifying the array in place. Also `Array#take` allocates a new array. We can build the same by iterating over the other element. This change buys us 106,470 bytes of memory and 2,663 fewer objects per request.
* Freeze string literals when not mutated.schneems2015-07-191-1/+1
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | I wrote a utility that helps find areas where you could optimize your program using a frozen string instead of a string literal, it's called [let_it_go](https://github.com/schneems/let_it_go). After going through the output and adding `.freeze` I was able to eliminate the creation of 1,114 string objects on EVERY request to [codetriage](codetriage.com). How does this impact execution? To look at memory: ```ruby require 'get_process_mem' mem = GetProcessMem.new GC.start GC.disable 1_114.times { " " } before = mem.mb after = mem.mb GC.enable puts "Diff: #{after - before} mb" ``` Creating 1,114 string objects results in `Diff: 0.03125 mb` of RAM allocated on every request. Or 1mb every 32 requests. To look at raw speed: ```ruby require 'benchmark/ips' number_of_objects_reduced = 1_114 Benchmark.ips do |x| x.report("freeze") { number_of_objects_reduced.times { " ".freeze } } x.report("no-freeze") { number_of_objects_reduced.times { " " } } end ``` We get the results ``` Calculating ------------------------------------- freeze 1.428k i/100ms no-freeze 609.000 i/100ms ------------------------------------------------- freeze 14.363k (± 8.5%) i/s - 71.400k no-freeze 6.084k (± 8.1%) i/s - 30.450k ``` Now we can do some maths: ```ruby ips = 6_226k # iterations / 1 second call_time_before = 1.0 / ips # seconds per iteration ips = 15_254 # iterations / 1 second call_time_after = 1.0 / ips # seconds per iteration diff = call_time_before - call_time_after number_of_objects_reduced * diff * 100 # => 0.4530373333993266 miliseconds saved per request ``` So we're shaving off 1 second of execution time for every 220 requests. Is this going to be an insane speed boost to any Rails app: nope. Should we merge it: yep. p.s. If you know of a method call that doesn't modify a string input such as [String#gsub](https://github.com/schneems/let_it_go/blob/b0e2da69f0cca87ab581022baa43291cdf48638c/lib/let_it_go/core_ext/string.rb#L37) please [give me a pull request to the appropriate file](https://github.com/schneems/let_it_go/blob/b0e2da69f0cca87ab581022baa43291cdf48638c/lib/let_it_go/core_ext/string.rb#L37), or open an issue in LetItGo so we can track and freeze more strings. Keep those strings Frozen ![](https://www.dropbox.com/s/z4dj9fdsv213r4v/let-it-go.gif?dl=1)
* Routes resources avoid :new and :edit endpoints if api_only is enabledJorge Bejar2015-06-111-6/+16
|
* remove unused codeAaron Patterson2015-06-081-2/+2
|
* we only care about methods that the request object responds toAaron Patterson2015-06-081-2/+1
| | | | | matches? should only deal with methods on the request object, so lets just filter out anything that the request object doesn't respond to
* extract required_defaults from the conditions hash before constructing the routeAaron Patterson2015-06-081-2/+3
| | | | | this way we can remove the strange "respond_to?" conditional in the `matches?` loop