You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by br...@apache.org on 2011/06/15 22:52:57 UTC

svn commit: r1136189 - in /thrift/trunk: compiler/cpp/src/generate/t_rb_generator.cc lib/rb/ext/constants.h lib/rb/ext/struct.c lib/rb/ext/thrift_native.c lib/rb/lib/thrift/struct_union.rb

Author: bryanduxbury
Date: Wed Jun 15 20:52:57 2011
New Revision: 1136189

URL: http://svn.apache.org/viewvc?rev=1136189&view=rev
Log:
THRIFT-418. rb: Don't do runtime sorting of struct fields

A simpler version of the already-committed patch.

Patch: Ilya Maykov

Modified:
    thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc
    thrift/trunk/lib/rb/ext/constants.h
    thrift/trunk/lib/rb/ext/struct.c
    thrift/trunk/lib/rb/ext/thrift_native.c
    thrift/trunk/lib/rb/lib/thrift/struct_union.rb

Modified: thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc
URL: http://svn.apache.org/viewvc/thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc?rev=1136189&r1=1136188&r2=1136189&view=diff
==============================================================================
--- thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc (original)
+++ thrift/trunk/compiler/cpp/src/generate/t_rb_generator.cc Wed Jun 15 20:52:57 2011
@@ -620,13 +620,9 @@ void t_rb_generator::generate_field_defn
   indent_down();
   out << endl;
   indent(out) << "}" << endl << endl;
-
-  // Generate the pre-sorted array of field ids, used for iterating through the fields in sorted order.
-  indent(out) << "FIELD_IDS = FIELDS.keys.sort" << endl << endl;
-
+  
   indent(out) << "def struct_fields; FIELDS; end" << endl << endl;
-
-  indent(out) << "def struct_field_ids; FIELD_IDS; end" << endl << endl;
+  
 }
 
 void t_rb_generator::generate_field_data(std::ofstream& out, t_type* field_type,

Modified: thrift/trunk/lib/rb/ext/constants.h
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/rb/ext/constants.h?rev=1136189&r1=1136188&r2=1136189&view=diff
==============================================================================
--- thrift/trunk/lib/rb/ext/constants.h (original)
+++ thrift/trunk/lib/rb/ext/constants.h Wed Jun 15 20:52:57 2011
@@ -77,7 +77,6 @@ extern ID read_all_method_id;
 extern ID native_qmark_method_id;
 
 extern ID fields_const_id;
-extern ID field_ids_const_id;
 extern ID transport_ivar_id;
 extern ID strict_read_ivar_id;
 extern ID strict_write_ivar_id;

Modified: thrift/trunk/lib/rb/ext/struct.c
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/rb/ext/struct.c?rev=1136189&r1=1136188&r2=1136189&view=diff
==============================================================================
--- thrift/trunk/lib/rb/ext/struct.c (original)
+++ thrift/trunk/lib/rb/ext/struct.c Wed Jun 15 20:52:57 2011
@@ -55,10 +55,10 @@ ID setvalue_id;
 
 ID to_s_method_id;
 ID name_to_id_method_id;
+static ID sorted_field_ids_method_id;
 
 #define IS_CONTAINER(ttype) ((ttype) == TTYPE_MAP || (ttype) == TTYPE_LIST || (ttype) == TTYPE_SET)
 #define STRUCT_FIELDS(obj) rb_const_get(CLASS_OF(obj), fields_const_id)
-#define STRUCT_FIELD_IDS(obj) rb_const_get(CLASS_OF(obj), field_ids_const_id)
 
 //-------------------------------------------
 // Writing section
@@ -376,11 +376,11 @@ static VALUE rb_thrift_struct_write(VALU
 
   // iterate through all the fields here
   VALUE struct_fields = STRUCT_FIELDS(self);
-  VALUE struct_field_ids_ordered = STRUCT_FIELD_IDS(self);
+  VALUE sorted_field_ids = rb_funcall(self, sorted_field_ids_method_id, 0);
 
   int i = 0;
-  for (i=0; i < RARRAY_LEN(struct_field_ids_ordered); i++) {
-    VALUE field_id = rb_ary_entry(struct_field_ids_ordered, i);
+  for (i=0; i < RARRAY_LEN(sorted_field_ids); i++) {
+    VALUE field_id = rb_ary_entry(sorted_field_ids, i);
 
     VALUE field_info = rb_hash_aref(struct_fields, field_id);
 
@@ -713,4 +713,5 @@ void Init_struct() {
 
   to_s_method_id = rb_intern("to_s");
   name_to_id_method_id = rb_intern("name_to_id");
+  sorted_field_ids_method_id = rb_intern("sorted_field_ids");
 }

Modified: thrift/trunk/lib/rb/ext/thrift_native.c
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/rb/ext/thrift_native.c?rev=1136189&r1=1136188&r2=1136189&view=diff
==============================================================================
--- thrift/trunk/lib/rb/ext/thrift_native.c (original)
+++ thrift/trunk/lib/rb/ext/thrift_native.c Wed Jun 15 20:52:57 2011
@@ -92,7 +92,6 @@ ID native_qmark_method_id;
 
 // constant ids
 ID fields_const_id;
-ID field_ids_const_id;
 ID transport_ivar_id;
 ID strict_read_ivar_id;
 ID strict_write_ivar_id;
@@ -175,7 +174,6 @@ void Init_thrift_native() {
 
   // constant ids
   fields_const_id = rb_intern("FIELDS");
-  field_ids_const_id = rb_intern("FIELD_IDS");
   transport_ivar_id = rb_intern("@trans");
   strict_read_ivar_id = rb_intern("@strict_read");
   strict_write_ivar_id = rb_intern("@strict_write");  

Modified: thrift/trunk/lib/rb/lib/thrift/struct_union.rb
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/rb/lib/thrift/struct_union.rb?rev=1136189&r1=1136188&r2=1136189&view=diff
==============================================================================
--- thrift/trunk/lib/rb/lib/thrift/struct_union.rb (original)
+++ thrift/trunk/lib/rb/lib/thrift/struct_union.rb Wed Jun 15 20:52:57 2011
@@ -32,8 +32,17 @@ module Thrift
       names_to_ids[name]
     end
 
+    def sorted_field_ids
+      sorted_field_ids = self.class.instance_variable_get(:@sorted_field_ids)
+      unless sorted_field_ids
+        sorted_field_ids = struct_fields.keys.sort
+        self.class.instance_variable_set(:@sorted_field_ids, sorted_field_ids)
+      end
+      sorted_field_ids
+    end
+
     def each_field
-      struct_field_ids.each do |fid|
+      sorted_field_ids.each do |fid|
         data = struct_fields[fid]
         yield fid, data
       end