Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #10835 (closed enhancement: fixed)

Opened 11 months ago

Last modified 7 months ago

[PATCH] Routes recognition 6 times speed up

Reported by: oleganza Assigned to: nzkoz
Priority: normal Milestone: 2.x
Component: ActionPack Version: edge
Severity: major Keywords: performance, routes
Cc: johan@johansorensen.com

Description

This patch adds optimization on routes recognition. Current implementation scans through the all patterns, what consumes a lot of time. I've added first-level prefix testing. It helps skip a number of routes with a same static prefix if incoming path doesn't contain such prefix. Depending on your routes schema, it may give up to 2.7x speedup.

What's in the patch: 1) 1500 lines of routing.rb are split into routing/*.rb files 2) Modified recognition time test to use more realistic routes scheme (based on restful routes) 3) Added some inline changes and a new file routing_recognition_optimisation.rb

Patch was applied to rev. 5931f016d9b10479c0c4f509459d92f75b86a7ab at git://git.koziarski.com/rails.git

PS. In a separate toy library i got 8.8x performance boost. But to apply it to actionpack, we need great refactoring. See my experiment here: http://gitorious.org/projects/uri-fast-routing/repos/mainline

Attachments

routing-speedup.patch (95.2 kB) - added by oleganza on 01/17/08 02:23:39.
patch speeds routes recognition up 2.4-2.7 times
0001-time-test-for-a-large-restful-routeset-recognition.patch (3.7 kB) - added by oleganza on 01/18/08 08:53:51.
0002-added-optimisation-code.patch (5.2 kB) - added by oleganza on 01/18/08 08:54:02.
0003-warning-about-optimization-in-RouteSet-recognize_pat.patch (1.1 kB) - added by oleganza on 01/18/08 09:02:33.
warns a developer that there's another version of recognize_path method elsewhere
AddedOptimizationAfterTimeTest.patch (7.8 kB) - added by oleganza on 01/19/08 05:29:26.
some tiny refactoring done

Change History

01/17/08 02:23:39 changed by oleganza

  • attachment routing-speedup.patch added.

patch speeds routes recognition up 2.4-2.7 times

01/17/08 03:01:44 changed by oleganza

Oops. Patch contains modified time test, so you can't compare it's results with the results before patch. Old time test runs over a single-pattern scheme. It is not interesting in sense of speed optimization, so i've added some real-app restful routes.

01/17/08 04:38:59 changed by oleganza

useful script by technoweenie to check performance of your own app:

06:33 technoweenie git://activereload.net/route_benchmarks.git 06:34 technoweenie put that in vendor/plugins and run 'rake routes:benchmarks:recognition'

01/17/08 04:46:04 changed by oleganza

  • severity changed from normal to major.
  • summary changed from Routes recognition 2.4-2.7x speed up to [PATCH] Routes recognition 2.4-2.7x speed up.

01/17/08 12:23:18 changed by Catfish

Not bad. With our 295 routes, I get - Before : 0.205132437237238 ms/url 4874.8994233588 url/s

After : 0.0638155793335478 ms/url 15670.1546933117 url/s

Nice work.

01/17/08 12:25:11 changed by Catfish

*sigh* I hate trac's formatting

Before : 
0.205132437237238 ms/url
4874.8994233588 url/s

After :
0.0638155793335478 ms/url
15670.1546933117 url/s

01/17/08 15:04:27 changed by josh

+1

Plus even more points for slowing down ":controller/:action/:id". More reason to use named routes and resources.

01/17/08 15:22:21 changed by oleganza

josh, there's not problem with ":controller/:action/:id" itself. I mean, there's no speed up when you have extremely small routeset. If routeset contains only "/my/path", the speed will be the same.

01/17/08 15:26:26 changed by oleganza

01/17/08 15:32:32 changed by josh

was just kidding. but it still offers more advantage to named routes which is a good practice.

01/17/08 17:04:14 changed by johansorensen

  • cc set to johan@johansorensen.com.

01/17/08 20:26:53 changed by nzkoz

  • owner changed from core to nzkoz.
  • status changed from new to assigned.
Before:
0.337655297915141 ms/url
2961.6002064073 url/s


After:
0.0579805692036947 ms/url
17247.1573448485 url/s

Pretty stellar work oleg.

01/17/08 22:45:58 changed by nzkoz

(In [8652]) Restructure routing into several smaller files. References #10835 [oleganza]

01/18/08 00:31:18 changed by nzkoz

Can you rebase your patches against this so we see the changes in isolation?

01/18/08 08:53:51 changed by oleganza

  • attachment 0001-time-test-for-a-large-restful-routeset-recognition.patch added.

01/18/08 08:54:02 changed by oleganza

  • attachment 0002-added-optimisation-code.patch added.

01/18/08 08:55:10 changed by oleganza

Rebased patches added. Base is r8662.

01/18/08 09:02:33 changed by oleganza

  • attachment 0003-warning-about-optimization-in-RouteSet-recognize_pat.patch added.

warns a developer that there's another version of recognize_path method elsewhere

01/19/08 01:15:34 changed by nzkoz

I'd prefer to hold off and see if we can get the full-improvement that you've achieved in fast-routing. Drop by #rails-contrib again some time and we can discuss the next steps to getting that merged.

01/19/08 05:14:42 changed by oleganza

How to close this ticket:

1. Checkout edge rails.
2. Apply 0001-time-test-for-a-large-restful-routeset-recognition.patch
3. Apply AddedOptimizationAfterTimeTest.patch 
4. Run tests.
5. Check in.

01/19/08 05:24:47 changed by bitsweat

(In [8673]) Add timing test for large restful route recognition. References #10835 [oleganza]

01/19/08 05:25:22 changed by bitsweat

  • status changed from assigned to closed.
  • resolution set to fixed.

(In [8674]) Performance: optimize route recognition. Large speedup for apps with many resource routes. Closes #10835.

01/19/08 05:29:26 changed by oleganza

  • attachment AddedOptimizationAfterTimeTest.patch added.

some tiny refactoring done

05/16/08 21:01:17 changed by oleganza

  • summary changed from [PATCH] Routes recognition 2.4-2.7x speed up to [PATCH] Routes recognition 6 times speed up.

(renamed title to the actual numbers: see [8674])