Skip to content

Commit

Permalink
reducing macro calls inside of the loop by keeping pointers in local …
Browse files Browse the repository at this point in the history
…variables
  • Loading branch information
Watson1978 committed Jan 3, 2012
1 parent 83c8c52 commit d67475a
Showing 1 changed file with 55 additions and 27 deletions.
82 changes: 55 additions & 27 deletions struct.c
Expand Up @@ -97,14 +97,16 @@ rb_struct_members_m(VALUE obj, SEL sel)
VALUE
rb_struct_getmember(VALUE obj, ID id)
{
VALUE members, slot;
long i;
VALUE members, slot, *ptr;
long i, len;

ptr = RSTRUCT_PTR(obj);
members = rb_struct_members(obj);
slot = ID2SYM(id);
for (i=0; i<RARRAY_LEN(members); i++) {
len = RARRAY_LEN(members);
for (i=0; i<len; i++) {
if (RARRAY_AT(members, i) == slot) {
return RSTRUCT_PTR(obj)[i];
return ptr[i];
}
}
rb_name_error(id, "%s is not struct member", rb_id2name(id));
Expand Down Expand Up @@ -154,8 +156,8 @@ rb_struct_modify(VALUE s)
static VALUE
rb_struct_set(VALUE obj, SEL sel, VALUE val)
{
VALUE members, slot;
long i;
VALUE members, slot, *ptr;
long i, len;

// foo=: -> foo
char buf[100];
Expand All @@ -164,11 +166,13 @@ rb_struct_set(VALUE obj, SEL sel, VALUE val)
ID field = rb_intern(buf);

members = rb_struct_members(obj);
len = RARRAY_LEN(members);
rb_struct_modify(obj);
for (i=0; i<RARRAY_LEN(members); i++) {
ptr = RSTRUCT_PTR(obj);
for (i=0; i<len; i++) {
slot = RARRAY_AT(members, i);
if (SYM2ID(slot) == field) {
GC_WB(&RSTRUCT_PTR(obj)[i], val);
GC_WB(&ptr[i], val);
return val;
}
}
Expand All @@ -182,7 +186,7 @@ make_struct(VALUE name, VALUE members, VALUE klass)
{
VALUE nstr;
ID id;
long i, count;
long i, len;

OBJ_FREEZE(members);
if (NIL_P(name)) {
Expand Down Expand Up @@ -213,7 +217,8 @@ make_struct(VALUE name, VALUE members, VALUE klass)
rb_objc_define_method(*(VALUE *)nstr, "new", rb_class_new_instance_imp, -1);
rb_objc_define_method(*(VALUE *)nstr, "[]", rb_class_new_instance_imp, -1);
rb_objc_define_method(*(VALUE *)nstr, "members", rb_struct_s_members_m, 0);
for (i = 0, count = RARRAY_LEN(members); i < count; i++) {
len = RARRAY_LEN(members);
for (i=0; i< len; i++) {
ID id = SYM2ID(RARRAY_AT(members, i));
if (rb_is_local_id(id) || rb_is_const_id(id)) {
if (i < N_REF_FUNC) {
Expand Down Expand Up @@ -512,20 +517,23 @@ inspect_struct(VALUE s, VALUE dummy, int recur)
{
const char *cname = rb_class2name(rb_obj_class(s));
VALUE str, members;
long i;
VALUE *ptr;
long i, len;

if (recur) {
return rb_sprintf("#<struct %s:...>", cname);
}

members = rb_struct_members(s);
ptr = RSTRUCT_PTR(s);
len = RSTRUCT_LEN(s);
if (cname[0] == '#') {
str = rb_str_new2("#<struct ");
}
else {
str = rb_sprintf("#<struct %s ", cname);
}
for (i=0; i<RSTRUCT_LEN(s); i++) {
for (i=0; i<len; i++) {
VALUE slot;
ID id;

Expand All @@ -541,7 +549,7 @@ inspect_struct(VALUE s, VALUE dummy, int recur)
rb_str_buf_append(str, rb_inspect(slot));
}
rb_str_cat2(str, "=");
rb_str_buf_append(str, rb_inspect(RSTRUCT_PTR(s)[i]));
rb_str_buf_append(str, rb_inspect(ptr[i]));
}
rb_str_cat2(str, ">");
OBJ_INFECT(str, s);
Expand Down Expand Up @@ -603,14 +611,15 @@ rb_struct_init_copy(VALUE copy, SEL sel, VALUE s)
static VALUE
rb_struct_aref_id(VALUE s, ID id)
{
VALUE members;
VALUE *ptr, members;
long i, len;

ptr = RSTRUCT_PTR(s);
members = rb_struct_members(s);
len = RARRAY_LEN(members);
for (i=0; i<len; i++) {
if (SYM2ID(RARRAY_AT(members, i)) == id) {
return RSTRUCT_PTR(s)[i];
return ptr[i];
}
}
rb_name_error(id, "no member '%s' in struct", rb_id2name(id));
Expand Down Expand Up @@ -665,19 +674,20 @@ rb_struct_aref(VALUE s, VALUE idx)
static VALUE
rb_struct_aset_id(VALUE s, ID id, VALUE val)
{
VALUE members;
VALUE members, *ptr;
long i, len;

members = rb_struct_members(s);
rb_struct_modify(s);
len = RARRAY_LEN(members);
if (RSTRUCT_LEN(s) != RARRAY_LEN(members)) {
rb_struct_modify(s);
if (RSTRUCT_LEN(s) != len) {
rb_raise(rb_eTypeError, "struct size differs (%ld required %ld given)",
RARRAY_LEN(members), RSTRUCT_LEN(s));
len, RSTRUCT_LEN(s));
}
ptr = RSTRUCT_PTR(s);
for (i=0; i<len; i++) {
if (SYM2ID(RARRAY_AT(members, i)) == id) {
GC_WB(&RSTRUCT_PTR(s)[i], val);
GC_WB(&ptr[i], val);
return val;
}
}
Expand Down Expand Up @@ -802,11 +812,17 @@ rb_struct_select(VALUE s, SEL sel, int argc, VALUE *argv)
static VALUE
rb_struct_equal_r(VALUE s, VALUE s2, int recur)
{
VALUE *ptr, *ptr2;
long i, len;

if (recur) {
return Qtrue;
}
for (int i = 0; i < RSTRUCT_LEN(s); i++) {
if (!rb_equal(RSTRUCT_PTR(s)[i], RSTRUCT_PTR(s2)[i])) {
ptr = RSTRUCT_PTR(s);
ptr2 = RSTRUCT_PTR(s2);
len = RSTRUCT_LEN(s);
for (i=0; i<len; i++) {
if (!rb_equal(ptr[i], ptr2[i])) {
return Qfalse;
}
}
Expand Down Expand Up @@ -851,11 +867,17 @@ rb_struct_equal(VALUE s, SEL sel, VALUE s2)
static VALUE
rb_struct_hash_r(VALUE s, VALUE s2, int recur)
{
long h = rb_hash(rb_obj_class(s));
long i, len;
st_index_t h;
VALUE n, *ptr;

h = rb_hash(rb_obj_class(s));
if (!recur) {
for (long i = 0; i < RSTRUCT_LEN(s); i++) {
ptr = RSTRUCT_PTR(s);
len = RSTRUCT_LEN(s);
for (i = 0; i < len; i++) {
h = (h << 1) | (h < 0 ? 1 : 0);
VALUE n = rb_hash(RSTRUCT_PTR(s)[i]);
n = rb_hash(ptr[i]);
h ^= NUM2LONG(n);
}
}
Expand All @@ -878,11 +900,17 @@ rb_struct_hash(VALUE s, SEL sel)
static VALUE
rb_struct_eql_r(VALUE s, VALUE s2, int recur)
{
VALUE *ptr, *ptr2;
long i, len;

if (recur) {
return Qtrue;
}
for (int i = 0; i < RSTRUCT_LEN(s); i++) {
if (!rb_eql(RSTRUCT_PTR(s)[i], RSTRUCT_PTR(s2)[i])) {
ptr = RSTRUCT_PTR(s);
ptr2 = RSTRUCT_PTR(s2);
len = RSTRUCT_LEN(s);
for (i=0; i<len; i++) {
if (!rb_eql(ptr[i], ptr2[i])) {
return Qfalse;
}
}
Expand Down

0 comments on commit d67475a

Please sign in to comment.