summaryrefslogtreecommitdiff
path: root/struct.c
diff options
context:
space:
mode:
authormatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-05-19 04:58:36 +0000
committermatz <matz@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-05-19 04:58:36 +0000
commit7d1f48b5c3768aba7f742532f00dd3d5a404a819 (patch)
treecc5974a423a78150d3e682814e542e44b459b32f /struct.c
parent6009d184ff73b623ec4eaba1a636f8abd5747b4a (diff)
* 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
Diffstat (limited to 'struct.c')
-rw-r--r--struct.c23
1 files changed, 14 insertions, 9 deletions
diff --git a/struct.c b/struct.c
index 87de0ac771..c1d9c42484 100644
--- a/struct.c
+++ b/struct.c
@@ -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);
@@ -31,9 +29,15 @@ 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__");
}