Ruby mogilefs-client dev/users discussion/patches/bugs/help/...
 help / color / mirror / code / Atom feed
From: Eric Wong <e@80x24.org>
To: mogilefs-client-public@bogomips.org
Subject: [PATCH] freeze string literals in a few more places
Date: Tue, 31 Jan 2017 01:58:12 +0000	[thread overview]
Message-ID: <20170131015812.8898-1-e@80x24.org> (raw)

This should reduce the amount of garbage we produce and
keep the process leaner and meaner.
---
 lib/mogilefs/admin.rb         | 1 +
 lib/mogilefs/http_file.rb     | 8 ++++----
 lib/mogilefs/mysql.rb         | 6 +++---
 lib/mogilefs/paths_size.rb    | 2 +-
 lib/mogilefs/socket_common.rb | 5 ++---
 5 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/lib/mogilefs/admin.rb b/lib/mogilefs/admin.rb
index 96a7898..d330439 100644
--- a/lib/mogilefs/admin.rb
+++ b/lib/mogilefs/admin.rb
@@ -1,4 +1,5 @@
 # -*- encoding: binary -*-
+# frozen_string_literal: true
 
 # \MogileFS administration client, this has little real-world usage
 # and is considered a work-in-progress
diff --git a/lib/mogilefs/http_file.rb b/lib/mogilefs/http_file.rb
index c7949a5..a58801a 100644
--- a/lib/mogilefs/http_file.rb
+++ b/lib/mogilefs/http_file.rb
@@ -54,7 +54,7 @@ def request_put(sock, uri, file_size, input = nil)
                  "Content-Length: #{file_size}\r\n\r\n")
       rv = input ? MogileFS.io.copy_stream(@active = input, sock) : yield(sock)
     else
-      trailers = @md5 ? "Trailer: Content-MD5\r\n" : ""
+      trailers = @md5 ? "Trailer: Content-MD5\r\n".freeze : "".freeze
       sock.write("PUT #{uri.request_uri} HTTP/1.1\r\n" \
                  "Host: #{host_with_port}\r\n#{trailers}" \
                  "Transfer-Encoding: chunked\r\n\r\n")
@@ -143,14 +143,14 @@ def nhp_put(devid, uri)
     end
 
     put = Net::HTTP::Put.new(uri.path)
-    put["Content-Type"] = "application/octet-stream".freeze
+    put["Content-Type".freeze] = "application/octet-stream".freeze
     if md5 = @opts[:content_md5]
       if md5.respond_to?(:call)
         md5 = md5.call.strip
       elsif md5 == :trailer
-        md5 = [ Digest::MD5.digest(string) ].pack("m").chomp!
+        md5 = [ Digest::MD5.digest(string) ].pack("m".freeze).chomp!
       end
-      put["Content-MD5"] = md5
+      put["Content-MD5".freeze] = md5
     end
     put.body = string
     res = @opts[:nhp_put].request(uri, put)
diff --git a/lib/mogilefs/mysql.rb b/lib/mogilefs/mysql.rb
index e2a16e5..81b2a61 100644
--- a/lib/mogilefs/mysql.rb
+++ b/lib/mogilefs/mysql.rb
@@ -28,7 +28,7 @@ def initialize(args = {})
   ##
   # Lists keys starting with +prefix+ follwing +after+ up to +limit+.  If
   # +after+ is nil the list starts at the beginning.
-  def _list_keys(domain, prefix = '', after = '', limit = 1000)
+  def _list_keys(domain, prefix = ''.freeze, after = ''.freeze, limit = 1000)
     # this code is based on server/lib/MogileFS/Worker/Query.pm
     dmid = get_dmid(domain)
 
@@ -43,7 +43,7 @@ def _list_keys(domain, prefix = '', after = '', limit = 1000)
     end
 
     raise MogileFS::Backend::InvalidCharsError if /[%\\]/ =~ prefix
-    prefix.gsub!(/_/, '\_') # not sure why MogileFS::Worker::Query does this...
+    prefix.gsub!(/_/, '\_'.freeze)
 
     sql = <<-EOS
     SELECT dkey,length,devcount FROM file
@@ -150,7 +150,7 @@ def refresh_device(force = false)
         tmp[devid.to_i] = {
           :hostip => hostip.freeze,
           :altip => (altip || hostip).freeze,
-          :readable => (host_status == "alive" &&
+          :readable => (host_status == "alive".freeze &&
                         DEV_STATUS_READABLE.include?(dev_status)),
           :http_port => http_port,
           :http_get_port => http_get_port ?  http_get_port.to_i : http_port,
diff --git a/lib/mogilefs/paths_size.rb b/lib/mogilefs/paths_size.rb
index ba61f9e..0aa676d 100644
--- a/lib/mogilefs/paths_size.rb
+++ b/lib/mogilefs/paths_size.rb
@@ -10,7 +10,7 @@ def self.call(paths)
       begin
         case r = Net::HTTP.start(uri.host, uri.port) { |x| x.head(uri.path) }
         when Net::HTTPOK
-          return r["Content-Length"].to_i
+          return r['Content-Length'.freeze].to_i
         else
           errors[path] = r
         end
diff --git a/lib/mogilefs/socket_common.rb b/lib/mogilefs/socket_common.rb
index d430068..886c738 100644
--- a/lib/mogilefs/socket_common.rb
+++ b/lib/mogilefs/socket_common.rb
@@ -23,18 +23,17 @@ def request_truncated!(written, expect, timeout)
      "request truncated (sent #{written} expected #{expect} timeout=#{timeout})"
   end
 
-  SEP_RE = /\A(.*?#{Regexp.escape("\n")})/
   def timed_gets(timeout = 5)
     unless defined?(@rbuf) && @rbuf
       @rbuf = timed_read(1024, "", timeout) or return # EOF
     end
     begin
-      @rbuf.sub!(SEP_RE, "") and return $1
+      @rbuf.sub!(/\A(.*\n)/o, "".freeze) and return $1
       tmp ||= ""
       if timed_read(1024, tmp, timeout)
         @rbuf << tmp
       else
-        # EOF, return the last buffered bit even without SEP_RE matching
+        # EOF, return the last buffered bit even without separatar matching
         # (not ideal for MogileFS, this is an error)
         return @rbuf.empty? ? nil : @rbuf.slice!(0, @rbuf.size)
       end
-- 
EW


                 reply	other threads:[~2017-01-31  1:58 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=20170131015812.8898-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).