1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* struct.c (struct_ivar_get): new function to avoid repeated

rb_intern() calls.

* struct.c (rb_struct_iv_get): use struct_ivar_get()

* struct.c (num_members): ditto.

* struct.c (rb_struct_s_members): ditto.

* class.c (rb_singleton_class): cache symbol to reduce calls to
  rb_intern().

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@23488 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
matz 2009-05-19 04:58:36 +00:00
parent 6009d184ff
commit 7d1f48b5c3
3 changed files with 31 additions and 10 deletions

View file

@ -1,3 +1,17 @@
Tue May 19 13:54:15 2009 Yukihiro Matsumoto <matz@ruby-lang.org>
* struct.c (struct_ivar_get): new function to avoid repeated
rb_intern() calls.
* struct.c (rb_struct_iv_get): use struct_ivar_get()
* struct.c (num_members): ditto.
* struct.c (rb_struct_s_members): ditto.
* class.c (rb_singleton_class): cache symbol to reduce calls to
rb_intern().
Tue May 19 07:52:05 2009 Tanaka Akira <akr@fsij.org>
* test/test_time.rb: make tests timezone independent.

View file

@ -857,6 +857,7 @@ VALUE
rb_singleton_class(VALUE obj)
{
VALUE klass;
ID attached;
if (FIXNUM_P(obj) || SYMBOL_P(obj)) {
rb_raise(rb_eTypeError, "can't define singleton");
@ -868,8 +869,9 @@ rb_singleton_class(VALUE obj)
rb_bug("unknown immediate %ld", obj);
}
CONST_ID(attached, "__attached__");
if (FL_TEST(RBASIC(obj)->klass, FL_SINGLETON) &&
rb_iv_get(RBASIC(obj)->klass, "__attached__") == obj) {
rb_ivar_get(RBASIC(obj)->klass, attached) == obj) {
klass = RBASIC(obj)->klass;
}
else {

View file

@ -12,15 +12,13 @@
#include "ruby/ruby.h"
VALUE rb_cStruct;
static ID id_members;
static VALUE struct_alloc(VALUE);
VALUE
rb_struct_iv_get(VALUE c, const char *name)
static inline VALUE
struct_ivar_get(VALUE c, ID id)
{
ID id;
id = rb_intern(name);
for (;;) {
if (rb_ivar_defined(c, id))
return rb_ivar_get(c, id);
@ -30,10 +28,16 @@ rb_struct_iv_get(VALUE c, const char *name)
}
}
VALUE
rb_struct_iv_get(VALUE c, const char *name)
{
return struct_ivar_get(c, rb_intern(name));
}
VALUE
rb_struct_s_members(VALUE klass)
{
VALUE members = rb_struct_iv_get(klass, "__members__");
VALUE members = struct_ivar_get(klass, id_members);
if (NIL_P(members)) {
rb_raise(rb_eTypeError, "uninitialized struct");
@ -193,7 +197,7 @@ make_struct(VALUE name, VALUE members, VALUE klass)
}
nstr = rb_define_class_under(klass, rb_id2name(id), klass);
}
rb_iv_set(nstr, "__members__", members);
rb_ivar_set(nstr, id_members, members);
rb_define_alloc_func(nstr, struct_alloc);
rb_define_singleton_method(nstr, "new", rb_class_new_instance, -1);
@ -248,7 +252,7 @@ rb_struct_define_without_accessor(const char *class_name, VALUE super, rb_alloc_
rb_class_inherited(super, klass);
}
rb_iv_set(klass, "__members__", members);
rb_ivar_set(klass, id_members, members);
if (alloc)
rb_define_alloc_func(klass, alloc);
@ -342,7 +346,7 @@ static size_t
num_members(VALUE klass)
{
VALUE members;
members = rb_struct_iv_get(klass, "__members__");
members = struct_ivar_get(klass, id_members);
if (TYPE(members) != T_ARRAY) {
rb_raise(rb_eTypeError, "broken members");
}
@ -907,4 +911,5 @@ Init_Struct(void)
rb_define_method(rb_cStruct, "values_at", rb_struct_values_at, -1);
rb_define_method(rb_cStruct, "members", rb_struct_members_m, 0);
id_members = rb_intern("__members__");
}