From 88e51fb08e0735a98a519db46649f01bcc88d03c Mon Sep 17 00:00:00 2001 From: schneems Date: Mon, 17 Sep 2018 11:41:14 -0500 Subject: [PATCH] Freeze all the strings! Reduces runtime allocation by freezing string literals by default. We could also remove a ton of manual `.freeze` calls, however the ruby supported version is 2.2 and the magic comment only targets 2.3+. --- lib/puma/binder.rb | 2 ++ lib/puma/cli.rb | 2 ++ lib/puma/client.rb | 2 ++ lib/puma/cluster.rb | 2 ++ lib/puma/commonlogger.rb | 2 ++ lib/puma/configuration.rb | 2 ++ lib/puma/const.rb | 2 ++ lib/puma/control_cli.rb | 2 ++ lib/puma/convenient.rb | 2 ++ lib/puma/daemon_ext.rb | 2 ++ lib/puma/delegation.rb | 2 ++ lib/puma/detect.rb | 2 ++ lib/puma/dsl.rb | 2 ++ lib/puma/events.rb | 2 ++ lib/puma/io_buffer.rb | 2 ++ lib/puma/java_io_buffer.rb | 2 ++ lib/puma/jruby_restart.rb | 2 ++ lib/puma/launcher.rb | 2 ++ lib/puma/minissl.rb | 2 ++ lib/puma/null_io.rb | 2 ++ lib/puma/plugin.rb | 2 ++ lib/puma/reactor.rb | 2 ++ lib/puma/runner.rb | 2 ++ lib/puma/server.rb | 2 ++ lib/puma/single.rb | 2 ++ lib/puma/state_file.rb | 2 ++ lib/puma/tcp_logger.rb | 2 ++ lib/puma/thread_pool.rb | 2 ++ lib/puma/util.rb | 1 + 29 files changed, 57 insertions(+) diff --git a/lib/puma/binder.rb b/lib/puma/binder.rb index bcaeeec0c3..7fc59d09f5 100644 --- a/lib/puma/binder.rb +++ b/lib/puma/binder.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'uri' require 'socket' diff --git a/lib/puma/cli.rb b/lib/puma/cli.rb index cff013c200..aad896482a 100644 --- a/lib/puma/cli.rb +++ b/lib/puma/cli.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'optparse' require 'uri' diff --git a/lib/puma/client.rb b/lib/puma/client.rb index c395555cd4..b608714598 100644 --- a/lib/puma/client.rb +++ b/lib/puma/client.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + class IO # We need to use this for a jruby work around on both 1.8 and 1.9. # So this either creates the constant (on 1.8), or harmlessly diff --git a/lib/puma/cluster.rb b/lib/puma/cluster.rb index 3a1aa586bf..aef6c0050d 100644 --- a/lib/puma/cluster.rb +++ b/lib/puma/cluster.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'puma/runner' require 'puma/util' require 'puma/plugin' diff --git a/lib/puma/commonlogger.rb b/lib/puma/commonlogger.rb index ee1f9331d8..25989e7245 100644 --- a/lib/puma/commonlogger.rb +++ b/lib/puma/commonlogger.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Puma # Rack::CommonLogger forwards every request to the given +app+, and # logs a line in the diff --git a/lib/puma/configuration.rb b/lib/puma/configuration.rb index 7226f7dc08..0b2191fa3f 100644 --- a/lib/puma/configuration.rb +++ b/lib/puma/configuration.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'puma/rack/builder' require 'puma/plugin' require 'puma/const' diff --git a/lib/puma/const.rb b/lib/puma/const.rb index f9e0a2a22c..3e6a7c08c1 100644 --- a/lib/puma/const.rb +++ b/lib/puma/const.rb @@ -1,4 +1,6 @@ #encoding: utf-8 +# frozen_string_literal: true + module Puma class UnsupportedOption < RuntimeError end diff --git a/lib/puma/control_cli.rb b/lib/puma/control_cli.rb index 692ac82028..1a74008eaa 100644 --- a/lib/puma/control_cli.rb +++ b/lib/puma/control_cli.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'optparse' require 'puma/state_file' require 'puma/const' diff --git a/lib/puma/convenient.rb b/lib/puma/convenient.rb index 0793c11e7c..f1d01f308b 100644 --- a/lib/puma/convenient.rb +++ b/lib/puma/convenient.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'puma/launcher' require 'puma/configuration' diff --git a/lib/puma/daemon_ext.rb b/lib/puma/daemon_ext.rb index 56d9e5a2a5..859a030204 100644 --- a/lib/puma/daemon_ext.rb +++ b/lib/puma/daemon_ext.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Process # This overrides the default version because it is broken if it diff --git a/lib/puma/delegation.rb b/lib/puma/delegation.rb index e11df31a71..c2fdaba509 100644 --- a/lib/puma/delegation.rb +++ b/lib/puma/delegation.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Puma module Delegation def forward(what, who) diff --git a/lib/puma/detect.rb b/lib/puma/detect.rb index b6e67dc99d..0c87a45cd6 100644 --- a/lib/puma/detect.rb +++ b/lib/puma/detect.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Puma IS_JRUBY = defined?(JRUBY_VERSION) diff --git a/lib/puma/dsl.rb b/lib/puma/dsl.rb index 220c4f7356..0740885d1d 100644 --- a/lib/puma/dsl.rb +++ b/lib/puma/dsl.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Puma # The methods that are available for use inside the config file. # These same methods are used in Puma cli and the rack handler diff --git a/lib/puma/events.rb b/lib/puma/events.rb index 10b802706d..721cb64ad6 100644 --- a/lib/puma/events.rb +++ b/lib/puma/events.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'puma/const' require "puma/null_io" require 'stringio' diff --git a/lib/puma/io_buffer.rb b/lib/puma/io_buffer.rb index 794f52400d..5ceb867902 100644 --- a/lib/puma/io_buffer.rb +++ b/lib/puma/io_buffer.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'puma/detect' if Puma.jruby? diff --git a/lib/puma/java_io_buffer.rb b/lib/puma/java_io_buffer.rb index d6434d7ad9..8a196924d1 100644 --- a/lib/puma/java_io_buffer.rb +++ b/lib/puma/java_io_buffer.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'java' # Conservative native JRuby/Java implementation of IOBuffer diff --git a/lib/puma/jruby_restart.rb b/lib/puma/jruby_restart.rb index da944e8ce7..c199ac9f7d 100644 --- a/lib/puma/jruby_restart.rb +++ b/lib/puma/jruby_restart.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'ffi' module Puma diff --git a/lib/puma/launcher.rb b/lib/puma/launcher.rb index c6ee7b7787..a80d090dcc 100644 --- a/lib/puma/launcher.rb +++ b/lib/puma/launcher.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'puma/events' require 'puma/detect' diff --git a/lib/puma/minissl.rb b/lib/puma/minissl.rb index ab1eb32461..7dde52399a 100644 --- a/lib/puma/minissl.rb +++ b/lib/puma/minissl.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + begin require 'io/wait' rescue LoadError diff --git a/lib/puma/null_io.rb b/lib/puma/null_io.rb index 8046608354..62661fbfef 100644 --- a/lib/puma/null_io.rb +++ b/lib/puma/null_io.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Puma # Provides an IO-like object that always appears to contain no data. # Used as the value for rack.input when the request has no body. diff --git a/lib/puma/plugin.rb b/lib/puma/plugin.rb index 63dc23968b..571cff098f 100644 --- a/lib/puma/plugin.rb +++ b/lib/puma/plugin.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Puma class UnknownPlugin < RuntimeError; end diff --git a/lib/puma/reactor.rb b/lib/puma/reactor.rb index 7d0c139de6..98a7e8240b 100644 --- a/lib/puma/reactor.rb +++ b/lib/puma/reactor.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'puma/util' require 'puma/minissl' diff --git a/lib/puma/runner.rb b/lib/puma/runner.rb index 240cf3b857..b3626645ac 100644 --- a/lib/puma/runner.rb +++ b/lib/puma/runner.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'puma/server' require 'puma/const' diff --git a/lib/puma/server.rb b/lib/puma/server.rb index e2e862fd56..c8ef87c4e3 100644 --- a/lib/puma/server.rb +++ b/lib/puma/server.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'stringio' require 'puma/thread_pool' diff --git a/lib/puma/single.rb b/lib/puma/single.rb index ca07ff631a..7a24ef181d 100644 --- a/lib/puma/single.rb +++ b/lib/puma/single.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'puma/runner' require 'puma/detect' require 'puma/plugin' diff --git a/lib/puma/state_file.rb b/lib/puma/state_file.rb index caea14e240..e49f7e5a74 100644 --- a/lib/puma/state_file.rb +++ b/lib/puma/state_file.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'yaml' module Puma diff --git a/lib/puma/tcp_logger.rb b/lib/puma/tcp_logger.rb index a6287693b1..bbc6dd334d 100644 --- a/lib/puma/tcp_logger.rb +++ b/lib/puma/tcp_logger.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + module Puma class TCPLogger def initialize(logger, app, quiet=false) diff --git a/lib/puma/thread_pool.rb b/lib/puma/thread_pool.rb index 7b2de557f8..ed5d87ff8d 100644 --- a/lib/puma/thread_pool.rb +++ b/lib/puma/thread_pool.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'thread' module Puma diff --git a/lib/puma/util.rb b/lib/puma/util.rb index 9937a8873c..31cff15ffe 100644 --- a/lib/puma/util.rb +++ b/lib/puma/util.rb @@ -1,3 +1,4 @@ +# frozen_string_literal: true major, minor, patch = RUBY_VERSION.split('.').map { |v| v.to_i } if major == 1 && minor == 9 && patch == 3 && RUBY_PATCHLEVEL < 125