From: Eric Wong <e@80x24.org>
To: mogilefs-client-public@bogomips.org
Subject: [PATCH] minor garbage reductions for newer Rubies
Date: Wed, 4 Nov 2015 21:43:18 +0000 [thread overview]
Message-ID: <20151104214318.8884-1-e@80x24.org> (raw)
opt_str_freeze and String#split is optimized for single-byte splits.
Use bang methods for escaping, avoiding much garbage in tight
loops.
---
lib/mogilefs/backend.rb | 18 +++++++++++-------
lib/mogilefs/chunker.rb | 5 ++---
lib/mogilefs/mogilefs.rb | 4 ++--
3 files changed, 15 insertions(+), 12 deletions(-)
diff --git a/lib/mogilefs/backend.rb b/lib/mogilefs/backend.rb
index f4d5b1f..632d6f1 100644
--- a/lib/mogilefs/backend.rb
+++ b/lib/mogilefs/backend.rb
@@ -313,7 +313,7 @@ class MogileFS::Backend
types.each { |type| opts[type] = 1 }
sockets = @hosts.map do |host|
- MogileFS::Socket.start(*(host.split(/:/))) rescue nil
+ MogileFS::Socket.start(*(host.split(':'.freeze))) rescue nil
end
sockets.compact!
@@ -346,7 +346,7 @@ class MogileFS::Backend
next if dead = @dead[host] and dead[0] > (MogileFS.now - @fail_timeout)
begin
- addr, port = host.split(/:/)
+ addr, port = host.split(':'.freeze)
@socket = MogileFS::Socket.tcp(addr, port, @timeout)
@active_host = host
rescue SystemCallError, MogileFS::Timeout => err
@@ -365,8 +365,8 @@ class MogileFS::Backend
# Turns a url params string into a Hash.
def url_decode(str) # :nodoc:
rv = {}
- str.split(/&/).each do |pair|
- k, v = pair.split(/=/, 2).map! { |x| url_unescape(x) }
+ str.split('&'.freeze).each do |pair|
+ k, v = pair.split('='.freeze, 2).map! { |x| url_unescape(x) }
rv[k.freeze] = v
end
rv
@@ -382,13 +382,15 @@ class MogileFS::Backend
def url_encode(params) # :nodoc:
params.map do |k,v|
"#{url_escape k.to_s}=#{url_escape v.to_s}"
- end.join("&")
+ end.join('&'.freeze)
end
# Escapes naughty URL characters.
if ''.respond_to?(:ord) # Ruby 1.9
def url_escape(str) # :nodoc:
- str.gsub(/([^\w\,\-.\/\\\: ])/) { "%%%02x" % $1.ord }.tr(' ', '+')
+ str = str.gsub(/([^\w\,\-.\/\\\: ])/) { "%%%02x".freeze % $1.ord }
+ str.tr!(' '.freeze, '+'.freeze)
+ str
end
else # Ruby 1.8
def url_escape(str) # :nodoc:
@@ -398,6 +400,8 @@ class MogileFS::Backend
# Unescapes naughty URL characters.
def url_unescape(str) # :nodoc:
- str.tr('+', ' ').gsub(/%([a-f0-9][a-f0-9])/i) { [$1.to_i(16)].pack 'C' }
+ str = str.tr('+'.freeze, ' '.freeze)
+ str.gsub!(/%([a-f0-9][a-f0-9])/i) { [$1.to_i(16)].pack('C'.freeze) }
+ str
end
end
diff --git a/lib/mogilefs/chunker.rb b/lib/mogilefs/chunker.rb
index 0d5c627..7d91452 100644
--- a/lib/mogilefs/chunker.rb
+++ b/lib/mogilefs/chunker.rb
@@ -1,6 +1,5 @@
# -*- encoding: binary -*-
class MogileFS::Chunker
- CRLF = "\r\n"
attr_reader :io
def initialize(io, md5, expect_md5)
@@ -14,7 +13,7 @@ class MogileFS::Chunker
@io.write("#{rv.to_s(16)}\r\n")
@io.write(buf)
@md5.update(buf) if @md5
- @io.write(CRLF)
+ @io.write("\r\n".freeze)
rv
end
@@ -30,7 +29,7 @@ class MogileFS::Chunker
end
@io.write("0\r\nContent-MD5: #{content_md5}\r\n\r\n")
else
- @io.write("0\r\n\r\n")
+ @io.write("0\r\n\r\n".freeze)
end
end
end
diff --git a/lib/mogilefs/mogilefs.rb b/lib/mogilefs/mogilefs.rb
index 2ccd78b..f454a0f 100644
--- a/lib/mogilefs/mogilefs.rb
+++ b/lib/mogilefs/mogilefs.rb
@@ -503,7 +503,7 @@ class MogileFS::MogileFS < MogileFS::Client
def file_info_cleanup(rv) # :nodoc:
%w(fid length devcount).each { |f| rv[f] = rv[f].to_i }
devids = rv["devids"] and
- rv["devids"] = devids.split(/,/).map! { |x| x.to_i }
+ rv["devids"] = devids.split(','.freeze).map! { |x| x.to_i }
rv
end
@@ -540,7 +540,7 @@ class MogileFS::MogileFS < MogileFS::Client
nexttry|fromdevid|failcount|flags|devid|type)\z/x
rv[k] = v.to_i
when /devids\z/
- rv[k] = v.split(/,/).map! { |x| x.to_i }
+ rv[k] = v.split(','.freeze).map! { |x| x.to_i }
end
end
end
--
EW
reply other threads:[~2015-11-04 21:43 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://yhbt.net/mogilefs-client/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20151104214318.8884-1-e@80x24.org \
--to=e@80x24.org \
--cc=mogilefs-client-public@bogomips.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://yhbt.net/mogilefs-client.git/
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).