THRIFT-418. rb: Don't do runtime sorting of struct fields

A simpler version of the already-committed patch.

Patch: Ilya Maykov

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1136189 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/lib/rb/ext/constants.h b/lib/rb/ext/constants.h
index 38b1d61..57df544 100644
--- a/lib/rb/ext/constants.h
+++ b/lib/rb/ext/constants.h
@@ -77,7 +77,6 @@
 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;
diff --git a/lib/rb/ext/struct.c b/lib/rb/ext/struct.c
index 4455c1c..dae338e 100644
--- a/lib/rb/ext/struct.c
+++ b/lib/rb/ext/struct.c
@@ -55,10 +55,10 @@
 
 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 @@
 
   // 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 @@
 
   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");
 }
diff --git a/lib/rb/ext/thrift_native.c b/lib/rb/ext/thrift_native.c
index 2a61a3e..09b9fe4 100644
--- a/lib/rb/ext/thrift_native.c
+++ b/lib/rb/ext/thrift_native.c
@@ -92,7 +92,6 @@
 
 // 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 @@
 
   // 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");