-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplement CGI and URI modules for loading speed
Simply requiring "cgi" takes ~4 ms, while "uri" can take ~18 ms on Ruby 2.0.0, because loading either module results in Ruby interpreter having to parse a huge amount of code, most of which is not needed for our purposes. For instance, we only use `CGI.escape` and `CGI.unescape`, and this change provides a small replacement module that loads fast. This also provides a lightweight URI::HTTP replacement that's able to parse simple URLs. For the purpose of representing URLs of git remote and handling API communication, this should be enough.
- Loading branch information
Showing
6 changed files
with
110 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
unless defined?(CGI) | ||
$" << 'cgi.rb' | ||
|
||
module CGI | ||
ESCAPE_RE = /[^a-zA-Z0-9 .~_-]/ | ||
|
||
def self.escape(s) | ||
s.to_s.gsub(ESCAPE_RE) {|match| | ||
'%' + match.unpack('H2' * match.bytesize).join('%').upcase | ||
}.tr(' ', '+') | ||
end | ||
|
||
def self.unescape(s) | ||
s.tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/) { | ||
[$1.delete('%')].pack('H*') | ||
} | ||
end | ||
end | ||
end | ||
|
||
unless defined?(URI) | ||
$" << 'uri.rb' | ||
|
||
Kernel.module_eval do | ||
def URI(str) | ||
URI.parse(str) | ||
end | ||
end | ||
|
||
module URI | ||
InvalidURIError = Class.new(StandardError) | ||
|
||
def self.parse(str) | ||
URI::HTTP.new(str) | ||
end | ||
|
||
def self.encode_www_form(params) | ||
params.map { |k, v| | ||
if v.class == Array | ||
encode_www_form(v.map { |x| [k, x] }) | ||
else | ||
ek = CGI.escape(k) | ||
v.nil? ? ek : "#{ek}=#{CGI.escape(v)}" | ||
end | ||
}.join("&") | ||
end | ||
|
||
def self.===(other) | ||
other.respond_to?(:host) | ||
end | ||
|
||
class HTTP | ||
attr_accessor :scheme, :user, :password, :host, :path, :query, :fragment | ||
attr_writer :port | ||
alias hostname host | ||
|
||
def initialize(str) | ||
m = str.to_s.match(%r{^ ([\w-]+): // (?:([^/@]+)@)? ([^/?#]+) }x) | ||
raise InvalidURIError unless m | ||
_, self.scheme, self.userinfo, host = m.to_a | ||
self.host, self.port = host.split(':', 2) | ||
path, self.fragment = m.post_match.split('#', 2) | ||
self.path, self.query = path.to_s.split('?', 2) | ||
end | ||
|
||
def to_s | ||
url = "#{scheme}://" | ||
url << "#{userinfo}@" if user || password | ||
url << host | ||
url << ":#{@port}" if @port | ||
url << path | ||
url << "?#{query}" if query | ||
url << "##{fragment}" if fragment | ||
url | ||
end | ||
|
||
def request_uri | ||
url = path | ||
url += "?#{query}" if query | ||
url | ||
end | ||
|
||
def port | ||
(@port || (scheme == 'https' ? 443 : 80)).to_i | ||
end | ||
|
||
def userinfo=(info) | ||
self.user, self.password = info.to_s.split(':', 2) | ||
info | ||
end | ||
|
||
def userinfo | ||
if password then "#{user}:#{password}" | ||
elsif user then user | ||
end | ||
end | ||
|
||
def find_proxy | ||
end | ||
end | ||
end | ||
end |