Instance variable attr methods are cheaper and we can shove the
complexity down to tmpio by allowing it to accept a nil argument
for the temporary directory.

This adds 4 bytes to tmpio but removes over 1K in http_context
on a 32-bit system.
---
 lib/yahns/http_context.rb | 14 +++-----------
 lib/yahns/tmpio.rb        |  4 ++--
 2 files changed, 5 insertions(+), 13 deletions(-)

diff --git a/lib/yahns/http_context.rb b/lib/yahns/http_context.rb
index 73bb49a..13b8686 100644
--- a/lib/yahns/http_context.rb
+++ b/lib/yahns/http_context.rb
@@ -18,7 +18,7 @@ module Yahns::HttpContext # :nodoc:
   attr_reader :app
   attr_reader :app_defaults
   attr_writer :input_buffer_tmpdir
-  attr_writer :output_buffer_tmpdir
+  attr_accessor :output_buffer_tmpdir
 
   def http_ctx_init(yahns_rack)
     @yahns_rack = yahns_rack
@@ -80,19 +80,11 @@ module Yahns::HttpContext # :nodoc:
   def tmpio_for(len)
     if len # Content-Length given
       len <= @client_body_buffer_size ? StringIO.new("")
-           : Yahns::TmpIO.new(input_buffer_tmpdir)
+           : Yahns::TmpIO.new(@input_buffer_tmpdir)
     else # chunked, unknown length
       mbs = @client_max_body_size
-      tmpdir = input_buffer_tmpdir
+      tmpdir = @input_buffer_tmpdir
       mbs ? Yahns::CapInput.new(mbs, tmpdir) : Yahns::TmpIO.new(tmpdir)
     end
   end
-
-  def input_buffer_tmpdir
-    @input_buffer_tmpdir || Dir.tmpdir
-  end
-
-  def output_buffer_tmpdir
-    @output_buffer_tmpdir || Dir.tmpdir
-  end
 end
diff --git a/lib/yahns/tmpio.rb b/lib/yahns/tmpio.rb
index d25d5e8..ca86b4e 100644
--- a/lib/yahns/tmpio.rb
+++ b/lib/yahns/tmpio.rb
@@ -12,10 +12,10 @@ class Yahns::TmpIO < File # :nodoc:
   # creates and returns a new File object.  The File is unlinked
   # immediately, switched to binary mode, and userspace output
   # buffering is disabled
-  def self.new(tmpdir = Dir.tmpdir)
+  def self.new(dir)
     retried = false
     begin
-      fp = super("#{tmpdir}/#{rand}", RDWR|CREAT|EXCL|APPEND, 0600)
+      fp = super("#{dir || Dir.tmpdir}/#{rand}", RDWR|CREAT|EXCL|APPEND, 0600)
     rescue Errno::EEXIST
       retry
     rescue Errno::EMFILE, Errno::ENFILE
-- 
EW


Reply via email to