Ruby mogilefs-client dev/users discussion/patches/bugs/help/...
 help / color / Atom feed
* [PATCH] minor garbage reductions for newer Rubies
@ 2015-11-04 21:43 Eric Wong
  0 siblings, 0 replies; only message in thread
From: Eric Wong @ 2015-11-04 21:43 UTC (permalink / raw)
  To: mogilefs-client-public

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


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, back to index

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-04 21:43 [PATCH] minor garbage reductions for newer Rubies Eric Wong

Ruby mogilefs-client dev/users discussion/patches/bugs/help/...

Archives are clonable:
	git clone --mirror https://yhbt.net/mogilefs-client-public
	git clone --mirror http://ou63pmih66umazou.onion/mogilefs-client-public

Example config snippet for mirrors

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.file-systems.mogilefs.ruby
	nntp://ou63pmih66umazou.onion/inbox.comp.file-systems.mogilefs.ruby

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/public-inbox.git