From 9ae8745eb06cd601d5c1f56eb1e3c4b0678508f8 Mon Sep 17 00:00:00 2001 From: Eric Wong Date: Tue, 7 Apr 2015 08:09:09 +0000 Subject: use String#include? for cold, simple substring matches Literal regexps costs over 400 bytes of memory on x86-64 per-site, so there's no point in using them to cause bloat at cold call sites where runtime performance does not matter. --- lib/yahns/client_expire_tcpi.rb | 2 +- lib/yahns/config.rb | 2 +- lib/yahns/socket_helper.rb | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) (limited to 'lib') diff --git a/lib/yahns/client_expire_tcpi.rb b/lib/yahns/client_expire_tcpi.rb index 8a89a42..4ca901a 100644 --- a/lib/yahns/client_expire_tcpi.rb +++ b/lib/yahns/client_expire_tcpi.rb @@ -38,4 +38,4 @@ module Yahns::ClientExpireTCPI # :nodoc: end # FreeBSD has "struct tcp_info", too, but does not support all the fields # Linux does as of FreeBSD 9 (haven't checked FreeBSD 10, yet). -end if RUBY_PLATFORM =~ /linux/ +end if RUBY_PLATFORM.include?('linux') diff --git a/lib/yahns/config.rb b/lib/yahns/config.rb index e880d92..9db0d59 100644 --- a/lib/yahns/config.rb +++ b/lib/yahns/config.rb @@ -236,7 +236,7 @@ class Yahns::Config # :nodoc: def canonicalize_tcp(addr, port) packed = Socket.pack_sockaddr_in(port, addr) port, addr = Socket.unpack_sockaddr_in(packed) - /:/ =~ addr ? "[#{addr}]:#{port}" : "#{addr}:#{port}" + addr.include?(':') ? "[#{addr}]:#{port}" : "#{addr}:#{port}" end def queue(*args, &block) diff --git a/lib/yahns/socket_helper.rb b/lib/yahns/socket_helper.rb index 66df8b0..a74f3f8 100644 --- a/lib/yahns/socket_helper.rb +++ b/lib/yahns/socket_helper.rb @@ -8,7 +8,7 @@ module Yahns::SocketHelper # :nodoc: def so_reuseport if defined?(Socket::SO_REUSEPORT) Socket::SO_REUSEPORT - elsif RUBY_PLATFORM =~ /linux/ + elsif RUBY_PLATFORM.include?('linux') 15 # only tested on x86_64 and i686 else nil @@ -108,7 +108,7 @@ module Yahns::SocketHelper # :nodoc: # returns rfc2732-style (e.g. "[::1]:666") addresses for IPv6 def tcp_name(sock) port, addr = Socket.unpack_sockaddr_in(sock.getsockname) - /:/ =~ addr ? "[#{addr}]:#{port}" : "#{addr}:#{port}" + addr.include?(':') ? "[#{addr}]:#{port}" : "#{addr}:#{port}" end # Returns the configuration name of a socket as a string. sock may -- cgit v1.2.3-24-ge0c7