THRIFT-1189. rb: Ruby deserializer speed improvements

This patch gives the Ruby deserialization stack a healthy performance boost.

Patch: Ilya Maykov

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1140780 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/rb/ext/memory_buffer.c b/lib/rb/ext/memory_buffer.c
index 74efa2c..23cd9eb 100644
--- a/lib/rb/ext/memory_buffer.c
+++ b/lib/rb/ext/memory_buffer.c
@@ -30,6 +30,11 @@
 
 #define GET_BUF(self) rb_ivar_get(self, buf_ivar_id)
 
+VALUE rb_thrift_memory_buffer_write(VALUE self, VALUE str);
+VALUE rb_thrift_memory_buffer_read(VALUE self, VALUE length_value);
+VALUE rb_thrift_memory_buffer_read_byte(VALUE self);
+VALUE rb_thrift_memory_buffer_read_into_buffer(VALUE self, VALUE buffer_value, VALUE size_value);
+
 VALUE rb_thrift_memory_buffer_write(VALUE self, VALUE str) {
   VALUE buf = GET_BUF(self);
   rb_str_buf_cat(buf, RSTRING_PTR(str), RSTRING_LEN(str));
@@ -62,10 +67,59 @@
   return data;
 }
 
+VALUE rb_thrift_memory_buffer_read_byte(VALUE self) {
+  VALUE index_value = rb_ivar_get(self, index_ivar_id);
+  int index = FIX2INT(index_value);
+
+  VALUE buf = GET_BUF(self);
+  if (index >= RSTRING_LEN(buf)) {
+    rb_raise(rb_eEOFError, "Not enough bytes remain in memory buffer");
+  }
+  char byte = RSTRING_PTR(buf)[index++];
+  rb_ivar_set(self, index_ivar_id, INT2FIX(index));
+
+  if (index >= GARBAGE_BUFFER_SIZE) {
+    rb_ivar_set(self, buf_ivar_id, rb_funcall(buf, slice_method_id, 2, INT2FIX(index), INT2FIX(RSTRING_LEN(buf) - 1)));
+    index = 0;
+  }
+  int result = (int) byte;
+  return INT2FIX(result);
+}
+
+VALUE rb_thrift_memory_buffer_read_into_buffer(VALUE self, VALUE buffer_value, VALUE size_value) {
+  int i = 0;
+  int size = FIX2INT(size_value);
+  int index;
+  VALUE buf = GET_BUF(self);
+
+  while (i < size) {
+    index = FIX2INT(rb_ivar_get(self, index_ivar_id));
+    if (index >= RSTRING_LEN(buf)) {
+      rb_raise(rb_eEOFError, "Not enough bytes remain in memory buffer");
+    }
+    char byte = RSTRING_PTR(buf)[index++];
+    rb_ivar_set(self, index_ivar_id, INT2FIX(index));
+
+    if (index >= GARBAGE_BUFFER_SIZE) {
+      rb_ivar_set(self, buf_ivar_id, rb_funcall(buf, slice_method_id, 2, INT2FIX(index), INT2FIX(RSTRING_LEN(buf) - 1)));
+      index = 0;
+    }
+
+    if (i >= RSTRING_LEN(buffer_value)) {
+      rb_raise(rb_eIndexError, "index %d out of string", i);
+    }
+    ((char*)RSTRING_PTR(buffer_value))[i] = byte;
+    i++;
+  }
+  return INT2FIX(i);
+}
+
 void Init_memory_buffer() {
   VALUE thrift_memory_buffer_class = rb_const_get(thrift_module, rb_intern("MemoryBufferTransport"));
   rb_define_method(thrift_memory_buffer_class, "write", rb_thrift_memory_buffer_write, 1);
   rb_define_method(thrift_memory_buffer_class, "read", rb_thrift_memory_buffer_read, 1);
+  rb_define_method(thrift_memory_buffer_class, "read_byte", rb_thrift_memory_buffer_read_byte, 0);
+  rb_define_method(thrift_memory_buffer_class, "read_into_buffer", rb_thrift_memory_buffer_read_into_buffer, 2);
   
   buf_ivar_id = rb_intern("@buf");
   index_ivar_id = rb_intern("@index");