From df8a0e6fca0dd0513a1b89a2feaf03f9a0056416 Mon Sep 17 00:00:00 2001 From: Bryan Duxbury Date: Thu, 9 Jun 2011 22:46:28 +0000 Subject: [PATCH] THRIFT-418. rb: Don't do runtime sorting of struct fields Patch: Ilya Maykov git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1134122 13f79535-47bb-0310-9956-ffa450edef68 --- compiler/cpp/src/generate/t_rb_generator.cc | 8 ++++++-- lib/rb/ext/constants.h | 1 + lib/rb/ext/struct.c | 5 ++--- lib/rb/ext/thrift_native.c | 2 ++ lib/rb/lib/thrift/struct_union.rb | 2 +- 5 files changed, 12 insertions(+), 6 deletions(-) diff --git a/compiler/cpp/src/generate/t_rb_generator.cc b/compiler/cpp/src/generate/t_rb_generator.cc index 8ae44055..895026fd 100644 --- a/compiler/cpp/src/generate/t_rb_generator.cc +++ b/compiler/cpp/src/generate/t_rb_generator.cc @@ -620,9 +620,13 @@ void t_rb_generator::generate_field_defns(std::ofstream& out, t_struct* tstruct) 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, diff --git a/lib/rb/ext/constants.h b/lib/rb/ext/constants.h index 57df544b..38b1d61d 100644 --- a/lib/rb/ext/constants.h +++ b/lib/rb/ext/constants.h @@ -77,6 +77,7 @@ 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; diff --git a/lib/rb/ext/struct.c b/lib/rb/ext/struct.c index 90c4b05a..4455c1c5 100644 --- a/lib/rb/ext/struct.c +++ b/lib/rb/ext/struct.c @@ -58,6 +58,7 @@ ID name_to_id_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 @@ -375,9 +376,7 @@ static VALUE rb_thrift_struct_write(VALUE self, VALUE protocol) { // iterate through all the fields here VALUE struct_fields = STRUCT_FIELDS(self); - - VALUE struct_field_ids_unordered = rb_funcall(struct_fields, keys_method_id, 0); - VALUE struct_field_ids_ordered = rb_funcall(struct_field_ids_unordered, sort_method_id, 0); + VALUE struct_field_ids_ordered = STRUCT_FIELD_IDS(self); int i = 0; for (i=0; i < RARRAY_LEN(struct_field_ids_ordered); i++) { diff --git a/lib/rb/ext/thrift_native.c b/lib/rb/ext/thrift_native.c index 09b9fe49..2a61a3ef 100644 --- a/lib/rb/ext/thrift_native.c +++ b/lib/rb/ext/thrift_native.c @@ -92,6 +92,7 @@ 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; @@ -174,6 +175,7 @@ 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"); diff --git a/lib/rb/lib/thrift/struct_union.rb b/lib/rb/lib/thrift/struct_union.rb index 849c856d..86275080 100644 --- a/lib/rb/lib/thrift/struct_union.rb +++ b/lib/rb/lib/thrift/struct_union.rb @@ -33,7 +33,7 @@ module Thrift end def each_field - struct_fields.keys.sort.each do |fid| + struct_field_ids.each do |fid| data = struct_fields[fid] yield fid, data end -- 2.17.1