Ruby-uriparser is a wrapper for uriparser C library which is fast (linear input length time complexity), approximatelly 7.5x faster than the standard ruby URI library with a low memory consumption.
It is a fairly reasonable hypothesis to consider that all Ruby Rack applications will call, at least once, the URI.parse
method for each http request, so this simple optimization can bring a significant impact on a high load environment. (See Benchmark)
require 'uriparser'
UriParser.parse('https://localhost:9000/path/?a=1#x') # return UriParser::URI object
If you want to override the URI class just include the following line in your code:
# Will override URI#parse and Kernel#URI methods
require 'uriparser/uri_gem'
The following numbers were computed for 100,000 URI.parse
calls using the ruby benchmark library in a Mac OS X (10.8.5)/ 2.9Ghz Intel Core I5/ 8GB 1600 MHz DDR3 machine:
Rehearsal ------------------------------------------------
URI 0.750000 0.000000 0.750000 ( 0.750619)
UriParser 0.110000 0.010000 0.120000 ( 0.111885)
Addressable 1.880000 0.000000 1.880000 ( 1.886091)
--------------------------------------- total: 2.750000sec
user system total real
URI 0.730000 0.000000 0.730000 ( 0.728169)
UriParser 0.110000 0.000000 0.110000 ( 0.107235)
Addressable 1.870000 0.000000 1.870000 ( 1.863334)
gem install ruby-uriparser
The ruby-uriparser gems needs the library uriparser installed to compile.
See the availables ports list: http://uriparser.sourceforge.net/#ports.
If you are on Mac OS X just run brew install uriparser
.
This project is released under the MIT license (See LICENSE file).
Copyright (c) 2014 Thiago Lewin - http://github.com/tlewin
This project includes code from the New BSD licensed:
- Copyright (C) 2007, Weijia Song songweijia@gmail.com
- Copyright (C) 2007, Sebastian Pipping webmaster@hartwork.org
All rights reserved.