From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: X-Spam-Status: No, score=-2.8 required=3.0 tests=ALL_TRUSTED,AWL,BAYES_00, URIBL_BLOCKED shortcircuit=no autolearn=unavailable version=3.3.2 X-Original-To: yahns-public@yhbt.net Received: from localhost (dcvr.yhbt.net [127.0.0.1]) by dcvr.yhbt.net (Postfix) with ESMTP id A47861F724; Tue, 18 Nov 2014 08:20:29 +0000 (UTC) From: Eric Wong To: yahns-public@yhbt.net Cc: Eric Wong Subject: [PATCH] save around 1500 bytes of memory on x86-64 Date: Tue, 18 Nov 2014 08:20:28 +0000 Message-Id: <1416298828-1873-1-git-send-email-e@80x24.org> X-Mailer: git-send-email 2.2.0.rc2.1.g1758c97.dirty List-Id: Replacing a Regexp argument to a rarely-called String#split with a literal String can save some memory. Each removed Regexp memsize is 469 bytes on Ruby 2.1, and Ruby does not currently deduplicate literal Regexps. On Ruby 2.1, the "objspace" extension shows: ObjectSpace.memsize_of(/,/) => 469 Is slightly smaller at 453 bytes on 2.2.0dev (r48474), and these numbers do not include the 40-byte object overhead. Nevertheless, this is a waste for non-performance-critical code during the startup phase. Identical literal strings are automatically deduplicated by Ruby 2.1, and has no additional overhead because Rack (and likely some real apps) also includes several instances of the literal "," byte. We'll drop the unnecessary "encoding: binary" magic comment in yahns/server.rb as well, as that file includes no literal binary strings. The downside of using a literal string argument in these cases is a 40-byte object gets allocated on every call, but the affected pieces of code are only called once in a process lifetime. --- lib/yahns/rackup_handler.rb | 4 ++-- lib/yahns/server.rb | 3 +-- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/yahns/rackup_handler.rb b/lib/yahns/rackup_handler.rb index 943d858..127db48 100644 --- a/lib/yahns/rackup_handler.rb +++ b/lib/yahns/rackup_handler.rb @@ -18,8 +18,8 @@ module Yahns::RackupHandler # :nodoc: app(:rack, app) do addr = o[:listen] || "#{o[:Host]||default_host}:#{o[:Port]||8080}" # allow listening to multiple addresses - if addr =~ /,/ - addr.split(/,/).each { |l| listen(l) } + if addr.include?(',') + addr.split(',').each { |l| listen(l) } else listen addr end diff --git a/lib/yahns/server.rb b/lib/yahns/server.rb index ae2ebd4..b28e741 100644 --- a/lib/yahns/server.rb +++ b/lib/yahns/server.rb @@ -1,4 +1,3 @@ -# -*- encoding: binary -*- # Copyright (C) 2013, Eric Wong and all contributors # License: GPLv3 or later (https://www.gnu.org/licenses/gpl-3.0.txt) require_relative 'queue_quitter' @@ -314,7 +313,7 @@ class Yahns::Server # :nodoc: # because that can completely break the non-blocking one. # Unfortunately, there is no one-off MSG_DONTWAIT-like flag for # accept4(2). - inherited = ENV['YAHNS_FD'].to_s.split(/,/).map do |fd| + inherited = ENV['YAHNS_FD'].to_s.split(',').map do |fd| io = Socket.for_fd(fd.to_i) set_server_sockopt(io, sock_opts(io)) @logger.info "inherited addr=#{sock_name(io)} fd=#{fd}" -- EW