ruby-changes:7376
From: matz <ko1@a...>
Date: Thu, 28 Aug 2008 20:50:44 +0900 (JST)
Subject: [ruby-changes:7376] Ruby:r18895 (trunk): * object.c (convert_type): call less rb_intern() less frequently
matz 2008-08-28 20:50:31 +0900 (Thu, 28 Aug 2008) New Revision: 18895 http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=18895 Log: * object.c (convert_type): call less rb_intern() less frequently by using cache structure. Modified files: trunk/ChangeLog trunk/object.c Index: ChangeLog =================================================================== --- ChangeLog (revision 18894) +++ ChangeLog (revision 18895) @@ -4,6 +4,9 @@ behave like array. a patch from Yusuke Endoh <mame at tsg.ne.jp> in [ruby-dev:35988]. [ruby-dev:35977] + * object.c (convert_type): call less rb_intern() less frequently + by using cache structure. + Thu Aug 28 19:04:50 2008 NAKAMURA Usaku <usa@r...> * bootstraptest/test_io.rb: no need to create real file. Index: object.c =================================================================== --- object.c (revision 18894) +++ object.c (revision 18895) @@ -1960,12 +1960,36 @@ return rb_cvar_defined(obj, id); } +static struct conv_method_tbl { + const char *method; + ID id; +} conv_method_names[] = { + {"to_int", 0}, + {"to_ary", 0}, + {"to_str", 0}, + {"to_sym", 0}, + {"to_hash", 0}, + {"to_proc", 0}, + {"to_io", 0}, + {"to_a", 0}, + {"to_s", 0}, + {NULL, 0} +}; + static VALUE convert_type(VALUE val, const char *tname, const char *method, int raise) { - ID m; + ID m = 0; + int i; - m = rb_intern(method); + for (i=0; conv_method_names[i].method; i++) { + if (conv_method_names[i].method[0] == method[0] && + strcmp(conv_method_names[i].method, method) == 0) { + m = conv_method_names[i].id; + break; + } + } + if (!m) m = rb_intern(method); if (!rb_respond_to(val, m)) { if (raise) { rb_raise(rb_eTypeError, "can't convert %s into %s", @@ -2427,6 +2451,8 @@ void Init_Object(void) { + int i; + #undef rb_intern #define rb_intern(str) rb_intern_const(str) @@ -2606,4 +2632,8 @@ id_match = rb_intern("=~"); id_inspect = rb_intern("inspect"); id_init_copy = rb_intern("initialize_copy"); + + for (i=0; conv_method_names[i].method; i++) { + conv_method_names[i].id = rb_intern(conv_method_names[i].method); + } } -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/