You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by kc...@apache.org on 2009/03/26 04:47:00 UTC

svn commit: r758510 - /incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb

Author: kclark
Date: Thu Mar 26 03:46:58 2009
New Revision: 758510

URL: http://svn.apache.org/viewvc?rev=758510&view=rev
Log:
THRIFT-401. rb: Speed up FramedTransport

Author: Tyler Kovacs

Modified:
    incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb

Modified: incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb
URL: http://svn.apache.org/viewvc/incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb?rev=758510&r1=758509&r2=758510&view=diff
==============================================================================
--- incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb (original)
+++ incubator/thrift/trunk/lib/rb/lib/thrift/transport.rb Thu Mar 26 03:46:58 2009
@@ -91,6 +91,7 @@
       @transport = transport
       @wbuf = ''
       @rbuf = ''
+      @index = 0
     end
 
     def open?
@@ -107,13 +108,16 @@
     end
 
     def read(sz)
-      ret = @rbuf.slice!(0...sz) 
+      @index += sz
+      ret = @rbuf.slice(@index - sz, sz) || ''
+
       if ret.length == 0
-        @rbuf = @transport.read([sz, DEFAULT_BUFFER].max) 
-        @rbuf.slice!(0...sz) 
-      else 
-        ret 
+        @rbuf = @transport.read([sz, DEFAULT_BUFFER].max)
+        @index = sz
+        ret = @rbuf.slice(0, sz) || ''
       end
+
+      ret
     end
 
     def write(buf)
@@ -143,6 +147,7 @@
       @wbuf      = ''
       @read      = read
       @write     = write
+      @index      = 0
     end
 
     def open?
@@ -162,9 +167,10 @@
 
       return '' if sz <= 0
 
-      read_frame if @rbuf.empty?
+      read_frame if @index >= @rbuf.length
 
-      @rbuf.slice!(0, sz)
+      @index += sz
+      @rbuf.slice(@index - sz, sz) || ''
     end
 
     def write(buf,sz=nil)
@@ -192,7 +198,8 @@
     def read_frame
       sz = @transport.read_all(4).unpack('N').first
 
-      @rbuf = @transport.read_all(sz).dup # protect against later #slice!
+      @index = 0
+      @rbuf = @transport.read_all(sz)
     end
   end