[#42] Re: possible bug: stack dump with <<-String, #{...} and large loops — ts <decoux@...>

32 messages 2002/05/25
[#43] Re: possible bug: stack dump with <<-String, #{...} and large loops — nobu.nokada@... 2002/05/26

Hi,

[#45] Re: possible bug: stack dump with <<-String, #{...} and large loops — ts <decoux@...> 2002/05/26

>>>>> "n" == nobu nokada <nobu.nokada@softhome.net> writes:

[#46] Re: possible bug: stack dump with <<-String, #{...} and large loops — nobu.nokada@... 2002/05/26

Hi,

[#47] Re: possible bug: stack dump with <<-String, #{...} and large loops — ts <decoux@...> 2002/05/26

>>>>> "n" == nobu nokada <nobu.nokada@softhome.net> writes:

[#48] Re: possible bug: stack dump with <<-String, #{...} and large loops — ts <decoux@...> 2002/05/26

>>>>> "t" == ts <decoux@moulon.inra.fr> writes:

[#49] Re: possible bug: stack dump with <<-String, #{...} and large loops — nobu.nokada@... 2002/05/27

Hi,

[#50] Re: possible bug: stack dump with <<-String, #{...} and large loops — ts <decoux@...> 2002/05/27

>>>>> "n" == nobu nokada <nobu.nokada@softhome.net> writes:

[#51] Re: possible bug: stack dump with <<-String, #{...} and large loops — nobu.nokada@... 2002/05/27

Hi,

[#52] Re: possible bug: stack dump with <<-String, #{...} and large loops — ts <decoux@...> 2002/05/27

>>>>> "n" == nobu nokada <nobu.nokada@softhome.net> writes:

[#53] Re: possible bug: stack dump with <<-String, #{...} and large loops — nobu.nokada@... 2002/05/27

Hi,

[#54] Re: possible bug: stack dump with <<-String, #{...} and large loops — ts <decoux@...> 2002/05/27

>>>>> "n" == nobu nokada <nobu.nokada@softhome.net> writes:

[#55] Re: possible bug: stack dump with <<-String, #{...} and large loops — nobu.nokada@... 2002/05/27

Hi,

[#56] Re: possible bug: stack dump with <<-String, #{...} and large loops — ts <decoux@...> 2002/05/27

>>>>> "n" == nobu nokada <nobu.nokada@softhome.net> writes:

[#57] Re: possible bug: stack dump with <<-String, #{...} and large loops — nobu.nokada@... 2002/05/28

Hi,

[#65] Re: possible bug: stack dump with <<-String, #{...} and large loops — ts <decoux@...> 2002/05/28

>>>>> "n" == nobu nokada <nobu.nokada@softhome.net> writes:

[#84] Re: possible bug: stack dump with <<-String, #{...} and large loops — nobu.nokada@... 2002/05/29

Hi,

[#92] Re: possible bug: stack dump with <<-String, #{...} and large loops — ts <decoux@...> 2002/05/29

>>>>> "n" == nobu nokada <nobu.nokada@softhome.net> writes:

[#67] The warns-a-thon continues... — Sean Chittenden <sean@...>

I'm feeling left out in this race to clobber warnings!!! Attached are

19 messages 2002/05/28

[#104] Re: possible bug: stack dump with <<-String, #{...} and large loops — ts <decoux@...>

>>>>> "n" == nobu nokada <nobu.nokada@softhome.net> writes:

29 messages 2002/05/30
[#105] Re: possible bug: stack dump with <<-String, #{...} and large loops — nobu.nokada@... 2002/05/30

Hi,

[#125] Re: possible bug: stack dump with <<-String, #{...} and large loops — ts <decoux@...> 2002/06/04

>>>>> "n" == nobu nokada <nobu.nokada@softhome.net> writes:

[#126] Re: possible bug: stack dump with <<-String, #{...} and large loops — nobu.nokada@... 2002/06/04

Hi,

[#127] Re: possible bug: stack dump with <<-String, #{...} and large loops — ts <decoux@...> 2002/06/04

>>>>> "n" == nobu nokada <nobu.nokada@softhome.net> writes:

[#130] Re: possible bug: stack dump with <<-String, #{...} and large loops — nobu.nokada@... 2002/06/04

Hi,

[#132] Re: possible bug: stack dump with <<-String, #{...} and large loops — nobu.nokada@... 2002/06/05

Hi,

[#134] Re: possible bug: stack dump with <<-String, #{...} and large loops — ts <decoux@...> 2002/06/05

>>>>> "n" == nobu nokada <nobu.nokada@softhome.net> writes:

Useless patch...

From: Michal Rokos <m.rokos@...>
Date: 2002-05-14 12:09:13 UTC
List: ruby-core #7
Hi,

	yesterday I read through array.c and made some comments for my
	self.

	Here's the diff as a result. There can be maybe some good bits
	in it...

	If you have a time read it with me...

@@ -62,14 +62,14 @@
     VALUE *ptr;
 
     rb_ary_modify_check(ary);
-    if (!FL_TEST(ary, ELTS_SHARED)) return;
-    ptr = ALLOC_N(VALUE, RARRAY(ary)->len);
-    FL_UNSET(ary, ELTS_SHARED);
-    RARRAY(ary)->aux.capa = RARRAY(ary)->len;
-    MEMCPY(ptr, RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
-    RARRAY(ary)->ptr = ptr;
+    if (FL_TEST(ary, ELTS_SHARED)) {
+	ptr = ALLOC_N(VALUE, RARRAY(ary)->len);
+	FL_UNSET(ary, ELTS_SHARED);
+	RARRAY(ary)->aux.capa = RARRAY(ary)->len;
+	MEMCPY(ptr, RARRAY(ary)->ptr, VALUE, RARRAY(ary)->len);
+	RARRAY(ary)->ptr = ptr;
+    }
 }
-
 VALUE
 rb_ary_freeze(ary)
     VALUE ary;

Explanation for above: Just a reformulation - maybe a little cleaner...

@@ -155,10 +155,7 @@
     VALUE ary;
     long i;
 
-    if (n < 0) {
-	rb_raise(rb_eIndexError, "negative number of items(%d)", n);
-    }
-    ary = rb_ary_new2(n<ARY_DEFAULT_SIZE?ARY_DEFAULT_SIZE:n);
+    ary = rb_ary_new2(n);
 
     va_init_list(ar, n);
     for (i=0; i<n; i++) {

I _new* - I see no reason to allocate ARY_DEFAULT_SIZE, thus n < 0 can
be dealed with rb_ary_new2()

@@ -243,8 +240,8 @@
 	rb_raise(rb_eArgError, "array size too big");
     }
     if (len > RARRAY(ary)->aux.capa) {
+	REALLOC_N(RARRAY(ary)->ptr, VALUE, len);
 	RARRAY(ary)->aux.capa = len;
-	REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->aux.capa);
     }
     if (rb_block_given_p()) {
 	long i;

Could this be a little faster? (I don't mean the line swap - I think
about pointer deref.)

@@ -273,7 +270,10 @@
 {
     VALUE ary = rb_obj_alloc(klass);
 
-    if (argc != 0) {
+    if (argc < 0) {
+	rb_raise(rb_eArgError, "negative number of arguments");
+    }
+    if (argc > 0) {
 	RARRAY(ary)->ptr = ALLOC_N(VALUE, argc);
 	MEMCPY(RARRAY(ary)->ptr, argv, VALUE, argc);
     }

Just some check to make it slower :-)

@@ -331,13 +331,11 @@
     VALUE *argv;
     VALUE ary;
 {
-    if (argc == 0) {
+    if (argc <= 0) {
 	rb_raise(rb_eArgError, "wrong number arguments(at least 1)");
     }
-    if (argc > 0) {
-	while (argc--) {
-	    rb_ary_push(ary, *argv++);
-	}
+    while (argc--) {
+	rb_ary_push(ary, *argv++);
     }
     return ary;
 }

No need to check argc > 0 if the arg <= 0 is checked...

@@ -348,12 +346,7 @@
 {
     rb_ary_modify_check(ary);
     if (RARRAY(ary)->len == 0) return Qnil;
-    if (!FL_TEST(ary, ELTS_SHARED) &&
-	RARRAY(ary)->len * 10 < RARRAY(ary)->aux.capa &&
-	RARRAY(ary)->aux.capa > ARY_DEFAULT_SIZE) {
-	RARRAY(ary)->aux.capa = RARRAY(ary)->len * 2;
-	REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->aux.capa);
-    }
+    
     return RARRAY(ary)->ptr[--RARRAY(ary)->len];
 }

In #shift - do we need the complicated if for resizing? I don't think
so... => Faster, but eats more mem (?)

@@ -361,8 +354,7 @@
 ary_make_shared(ary)
     VALUE ary;
 {
-    if (FL_TEST(ary, ELTS_SHARED)) return;
-    else {
+    if (!FL_TEST(ary, ELTS_SHARED)) {
 	NEWOBJ(shared, struct RArray);
 	OBJSETUP(shared, rb_cArray, T_ARRAY);

Again - just reformulation...

@@ -419,19 +411,19 @@
     VALUE *argv;
     VALUE ary;
 {
-    if (argc == 0) {
+    long len = RARRAY(ary)->len;
+    
+    if (argc <= 0) {
 	rb_raise(rb_eArgError, "wrong number of arguments(at least 1)");
     }
-    if (argc > 0) {
-	long len = RARRAY(ary)->len;
 
-	/* make rooms by setting the last item */
-	rb_ary_store(ary, len + argc - 1, Qnil);
+    /* make rooms by setting the last item */
+    rb_ary_store(ary, len + argc - 1, Qnil);
 
-	/* sliding items */
-	MEMMOVE(RARRAY(ary)->ptr + argc, RARRAY(ary)->ptr, VALUE, len);
-	MEMCPY(RARRAY(ary)->ptr, argv, VALUE, argc);
-    }
+    /* sliding items */
+    MEMMOVE(RARRAY(ary)->ptr + argc, RARRAY(ary)->ptr, VALUE, len);
+    MEMCPY(RARRAY(ary)->ptr, argv, VALUE, argc);
+    
     return ary;
 }

In short: raise error when argc <= 0, no need to check for argc > 0

@@ -464,16 +456,15 @@
 
     if (beg + len > RARRAY(ary)->len) {
 	len = RARRAY(ary)->len - beg;
-    }
-    if (len < 0) {
-	len = 0;
+	if (len < 0)
+	    len = 0;
     }
     klass = rb_obj_class(ary);
-    if (len == 0) return ary_new(klass,0);
+    if (len == 0) return ary_new(klass, 0);
 
     ary_make_shared(ary);
     ary2 = rb_obj_alloc(klass);
-    RARRAY(ary2)->ptr = RARRAY(ary)->ptr+beg;
+    RARRAY(ary2)->ptr = RARRAY(ary)->ptr + beg;
     RARRAY(ary2)->len = len;
     RARRAY(ary2)->aux.shared = RARRAY(ary)->aux.shared;
     FL_SET(ary2, ELTS_SHARED);

Len < 0 has been already checked (no need to check it again...)

@@ -487,38 +478,32 @@
     VALUE *argv;
     VALUE ary;
 {
-    VALUE arg;
+    VALUE arg, arg2;
     long beg, len;
 
-    if (argc == 2) {
-	beg = NUM2LONG(argv[0]);
-	len = NUM2LONG(argv[1]);
+    if (rb_scan_args(argc, argv, "11", &arg, &arg2) == 2) {
+	beg = NUM2LONG(arg);
+	len = NUM2LONG(arg2);
 	if (beg < 0) {
 	    beg += RARRAY(ary)->len;
 	}
 	return rb_ary_subseq(ary, beg, len);
     }
-    if (argc != 1) {
-	rb_scan_args(argc, argv, "11", 0, 0);
-    }
-    arg = argv[0];
     /* special case - speeding up */
     if (FIXNUM_P(arg)) {
 	return rb_ary_entry(ary, FIX2LONG(arg));
     }
-    else if (TYPE(arg) == T_BIGNUM) {
+    if (TYPE(arg) == T_BIGNUM) {
 	rb_raise(rb_eIndexError, "index too big");
     }
-    else {
-	/* check if idx is Range */
-	switch (rb_range_beg_len(arg, &beg, &len, RARRAY(ary)->len, 0)) {
-	  case Qfalse:
+    /* check if idx is Range */
+    switch (rb_range_beg_len(arg, &beg, &len, RARRAY(ary)->len, 0)) {
+	case Qfalse:
 	    break;
-	  case Qnil:
+	case Qnil:
 	    return Qnil;
-	  default:
+	default:
 	    return rb_ary_subseq(ary, beg, len);
-	}
     }
     return rb_ary_entry(ary, NUM2LONG(arg));
 }

Not so wise use of rb_scan_args in this diff, what I want to show is
'else if' cleanup...

@@ -585,7 +570,7 @@
 
     for (i=0; i<RARRAY(ary)->len; i++) {
 	if (rb_equal(RARRAY(ary)->ptr[i], val))
-	    return INT2NUM(i);
+	    return LONG2NUM(i);
     }
     return Qnil;
 }

Just make my eye happy...

@@ -648,13 +633,13 @@
     rpl = rb_ary_to_ary(rpl);
     rlen = RARRAY(rpl)->len;
 
-    if (len < 0) rb_raise(rb_eIndexError, "negative length %d", len);
+    if (len < 0) rb_raise(rb_eIndexError, "negative length (%d)", len);
     if (beg < 0) {
 	beg += RARRAY(ary)->len;
-    }
-    if (beg < 0) {
-	beg -= RARRAY(ary)->len;
-	rb_raise(rb_eIndexError, "index %d out of array", beg);
+	if (beg < 0) {
+	    beg -= RARRAY(ary)->len;
+	    rb_raise(rb_eIndexError, "index %d out of array", beg);
+	}
     }
     if (beg + len > RARRAY(ary)->len) {
 	len = RARRAY(ary)->len - beg;

Again: move 2nd check of beg < 0 where it should be.

@@ -664,11 +649,11 @@
     if (beg >= RARRAY(ary)->len) {
 	len = beg + rlen;
 	if (len >= RARRAY(ary)->aux.capa) {
-	    RARRAY(ary)->aux.capa=len;
-	    REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->aux.capa);
+	    RARRAY(ary)->aux.capa = len;
+	    REALLOC_N(RARRAY(ary)->ptr, VALUE, len);
 	}
-	rb_mem_clear(RARRAY(ary)->ptr+RARRAY(ary)->len, beg-RARRAY(ary)->len);
-	MEMCPY(RARRAY(ary)->ptr+beg, RARRAY(rpl)->ptr, VALUE, rlen);
+	rb_mem_clear(RARRAY(ary)->ptr + RARRAY(ary)->len, beg - RARRAY(ary)->len);
+	MEMCPY(RARRAY(ary)->ptr + beg, RARRAY(rpl)->ptr, VALUE, rlen);
 	RARRAY(ary)->len = len;
     }
     else {

Pointer deref speedups (and some indents)

@@ -681,12 +666,12 @@
 	alen = RARRAY(ary)->len + rlen - len;
 	if (alen >= RARRAY(ary)->aux.capa) {
 	    RARRAY(ary)->aux.capa = alen;
-	    REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->aux.capa);
+	    REALLOC_N(RARRAY(ary)->ptr, VALUE, alen);
 	}
 
 	if (len != rlen) {
-	    MEMMOVE(RARRAY(ary)->ptr+beg+rlen, RARRAY(ary)->ptr+beg+len,
-		    VALUE, RARRAY(ary)->len-(beg+len));
+	    MEMMOVE(RARRAY(ary)->ptr + beg + rlen, RARRAY(ary)->ptr + beg + len,
+		    VALUE, RARRAY(ary)->len - (beg + len));
 	    RARRAY(ary)->len = alen;
 	}
 	MEMMOVE(RARRAY(ary)->ptr+beg, RARRAY(rpl)->ptr, VALUE, rlen);

ditto.

@@ -711,18 +696,18 @@
     if (FIXNUM_P(argv[0])) {
 	offset = FIX2LONG(argv[0]);
 	goto fixnum;
+    }
+    if (TYPE(argv[0]) == T_BIGNUM) {
+	rb_raise(rb_eIndexError, "index too big");
     }
-    else if (rb_range_beg_len(argv[0], &beg, &len, RARRAY(ary)->len, 1)) {
+    if (RTEST(rb_range_beg_len(argv[0], &beg, &len, RARRAY(ary)->len, 1))) {
 	/* check if idx is Range */
 	rb_ary_update(ary, beg, len, argv[1]);
 	return argv[1];
     }
-    if (TYPE(argv[0]) == T_BIGNUM) {
-	rb_raise(rb_eIndexError, "index too big");
-    }
 
     offset = NUM2LONG(argv[0]);
-  fixnum:
+fixnum:
     rb_ary_store(ary, offset, argv[1]);
     return argv[1];
 }

Move sipler checks to the top, use RTEST (maybe useless), indents

@@ -825,14 +810,14 @@
 rb_ary_clone(ary)
     VALUE ary;
 {
-    return ary_copy(ary, Qtrue);
+    return ary_copy(ary, 1);
 }
 
 VALUE
 rb_ary_dup(ary)
     VALUE ary;
 {
-    return ary_copy(ary, Qfalse);
+    return ary_copy(ary, 0);
 }
 
 static VALUE

ary_copy(VALUE, int) - make my eye more happy :-)

@@ -861,15 +846,13 @@
 rb_ary_join(ary, sep)
     VALUE ary, sep;
 {
-    long len, i;
+    long len = 1, i;
     int taint = 0;
     VALUE result, tmp;
 
     if (RARRAY(ary)->len == 0) return rb_str_new(0, 0);
-    if (OBJ_TAINTED(ary)) taint = 1;
-    if (OBJ_TAINTED(sep)) taint = 1;
+    if (OBJ_TAINTED(ary) || OBJ_TAINTED(sep)) taint = 1;
 
-    len = 1;
     for (i=0; i<RARRAY(ary)->len; i++) {
 	if (TYPE(RARRAY(ary)->ptr[i]) == T_STRING) {
 	    len += RSTRING(RARRAY(ary)->ptr[i])->len;

Why did I make this changes???

@@ -930,12 +914,11 @@
 rb_ary_to_s(ary)
     VALUE ary;
 {
-    VALUE str, sep;
+    VALUE sep;
 
     if (RARRAY(ary)->len == 0) return rb_str_new(0, 0);
-    sep = rb_output_fs;
-    str = rb_ary_join(ary, sep);
-    return str;
+    
+    return rb_ary_join(ary, rb_output_fs);
 }
 
 static ID inspect_key;

Phew - 1 VALUE spared :-)

@@ -960,7 +943,7 @@
 
     inspect_tbl = rb_thread_local_aref(rb_thread_current(), inspect_key);
     rb_ary_pop(inspect_tbl);
-    return 0;
+    return obj;
 }
 
 VALUE

Should return a VALUE - make my eye happier.

@@ -972,9 +955,6 @@
     VALUE inspect_tbl;
     VALUE id;
 
-    if (!inspect_key) {
-	inspect_key = rb_intern("__inspect_key__");
-    }
     inspect_tbl = rb_thread_local_aref(rb_thread_current(), inspect_key);
     if (NIL_P(inspect_tbl)) {
 	inspect_tbl = rb_ary_new();

Move init of inspect_key to Init_range()

@@ -998,9 +978,6 @@
 {
     VALUE inspect_tbl;
 
-    if (!inspect_key) {
-	inspect_key = rb_intern("__inspect_key__");
-    }
     inspect_tbl = rb_thread_local_aref(rb_thread_current(), inspect_key);
     if (NIL_P(inspect_tbl)) return Qfalse;
     return rb_ary_includes(inspect_tbl, rb_obj_id(obj));

ditto.

@@ -1011,7 +988,7 @@
     VALUE ary;
 {
     int tainted = OBJ_TAINTED(ary);
-    long i = 0;
+    long i;
     VALUE s, str;
 
     str = rb_str_buf_new2("[");

No need for init... What is this patch good for?

@@ -1049,8 +1026,8 @@
     VALUE *p1, *p2;
     VALUE tmp;
 
-    if (RARRAY(ary)->len <= 1) return ary;
     rb_ary_modify(ary);
+    if (RARRAY(ary)->len <= 1) return ary;
 
     p1 = RARRAY(ary)->ptr;
     p2 = p1 + RARRAY(ary)->len - 1;	/* points last item */

Is this better?

@@ -1083,13 +1060,13 @@
 rb_cmpint(cmp)
     VALUE cmp;
 {
-    if (FIXNUM_P(cmp)) return NUM2LONG(cmp);
+    if (FIXNUM_P(cmp)) return FIX2INT(cmp);
     if (TYPE(cmp) == T_BIGNUM) {
 	if (RBIGNUM(cmp)->sign) return 1;
 	return -1;
     }
-    if (rb_funcall(cmp, '>', 1, INT2FIX(0))) return 1;
-    if (rb_funcall(cmp, '<', 1, INT2FIX(0))) return -1;
+    if (RTEST(rb_funcall(cmp, '>', 1, INT2FIX(0)))) return 1;
+    if (RTEST(rb_funcall(cmp, '<', 1, INT2FIX(0)))) return -1;
     return 0;
 }

Again: changes just for my eyes...

@@ -1107,8 +1084,8 @@
 {
     VALUE retval;
 
-    if (FIXNUM_P(*a)) {
-	if (FIXNUM_P(*b)) return *a - *b;
+    if (FIXNUM_P(*a) && FIXNUM_P(*b)) {
+	return FIX2INT(*a) - FIX2INT(*b);
     }
     else if (TYPE(*a) == T_STRING && TYPE(*b) == T_STRING) {
 	return rb_str_cmp(*a, *b);

Is this a fix (finaly good tiny patch from me?)?

@@ -1224,7 +1201,7 @@
 
     rb_ary_modify(ary);
     for (i1 = i2 = 0; i1 < RARRAY(ary)->len; i1++) {
-	if (rb_equal(RARRAY(ary)->ptr[i1], item)) continue;
+	if (RTEST(rb_equal(RARRAY(ary)->ptr[i1], item))) continue;
 	if (i1 != i2) {
 	    RARRAY(ary)->ptr[i2] = RARRAY(ary)->ptr[i1];
 	}

Is the RTEST needed here? (Don't think so... :-))

@@ -1238,6 +1215,11 @@
     }
     else {
 	RARRAY(ary)->len = i2;
+	if (i2 * 2 < RARRAY(ary)->aux.capa &&
+		RARRAY(ary)->aux.capa > ARY_DEFAULT_SIZE) {
+	    RARRAY(ary)->aux.capa = i2 * 2;
+	    REALLOC_N(RARRAY(ary)->ptr, VALUE, i2 * 2);
+	}
     }
 
     return item;

Make resize after delete.

@@ -1249,12 +1231,14 @@
     long pos;
 {
     long i, len = RARRAY(ary)->len;
-    VALUE del = Qnil;
+    VALUE del;
 
     rb_ary_modify(ary);
     if (pos >= len) return Qnil;
-    if (pos < 0) pos += len;
-    if (pos < 0) return Qnil;
+    if (pos < 0) {
+	pos += len;
+	if (pos < 0) return Qnil;
+    }
 
     del = RARRAY(ary)->ptr[pos];
     for (i = pos + 1; i < len; i++, pos++) {

Move test where it could be...

@@ -1294,23 +1278,12 @@
 	return arg2;
     }
 
-    if (!FIXNUM_P(arg1) && rb_range_beg_len(arg1, &pos, &len, RARRAY(ary)->len, 1)) {
+    if (!FIXNUM_P(arg1) && RTEST(rb_range_beg_len(arg1, &pos, &len, RARRAY(ary)->len, 1))) {
 	goto delete_pos_len;
     }
 
-    pos = NUM2LONG(arg1);
-    len = RARRAY(ary)->len;
+    rb_ary_delete_at(ary, NUM2LONG(arg1));
 
-    if (pos >= len) return Qnil;
-    if (pos < 0) pos += len;
-    if (pos < 0) return Qnil;
-
-    arg2 = RARRAY(ary)->ptr[pos];
-    for (i = pos + 1; i < len; i++, pos++) {
-	RARRAY(ary)->ptr[pos] = RARRAY(ary)->ptr[i];
-    }
-    RARRAY(ary)->len = pos;
-
     return arg2;
 }

Drop some lines that are same as in rb_ary_delete_at (and make it a bit
slower) :-)

@@ -1367,7 +1340,7 @@
 {
     rb_ary_modify(ary);
     RARRAY(ary)->len = 0;
-    if (ARY_DEFAULT_SIZE*3 < RARRAY(ary)->aux.capa) {
+    if (ARY_DEFAULT_SIZE * 2 < RARRAY(ary)->aux.capa) {
 	RARRAY(ary)->aux.capa = ARY_DEFAULT_SIZE * 2;
 	REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->aux.capa);
     }

??? What? Aha! 3 -> 2, but why I did it? (More efficient?)

@@ -1383,10 +1356,10 @@
     VALUE item, arg1, arg2;
     long beg, end, len;
     VALUE *p, *pend;
-    int block_p = Qfalse;
+    int block_p = 0;
 
     if (rb_block_given_p()) {
-	block_p = Qtrue;
+	block_p = 1;
 	rb_scan_args(argc, argv, "02", &arg1, &arg2);
 	argc += 1;		/* hackish */
     }

Again: Eye happing change.

@@ -1399,17 +1372,17 @@
 	len = RARRAY(ary)->len;
 	break;
       case 2:
-	if (rb_range_beg_len(arg1, &beg, &len, RARRAY(ary)->len, 1)) {
+	if (RTEST(rb_range_beg_len(arg1, &beg, &len, RARRAY(ary)->len, 1))) {
 	    break;
 	}
 	/* fall through */
       case 3:
-	beg = NIL_P(arg1)?0:NUM2LONG(arg1);
+	beg = NIL_P(arg1) ? 0 : NUM2LONG(arg1);
 	if (beg < 0) {
 	    beg = RARRAY(ary)->len + beg;
 	    if (beg < 0) beg = 0;
 	}
-	len = NIL_P(arg2)?RARRAY(ary)->len - beg:NUM2LONG(arg2);
+	len = NIL_P(arg2) ? RARRAY(ary)->len - beg : NUM2LONG(arg2);
 	break;
     }
     rb_ary_modify(ary);

Indents and RTEST...

@@ -1417,19 +1390,19 @@
     if (end > RARRAY(ary)->len) {
 	if (end >= RARRAY(ary)->aux.capa) {
 	    RARRAY(ary)->aux.capa = end;
-	    REALLOC_N(RARRAY(ary)->ptr, VALUE, RARRAY(ary)->aux.capa);
+	    REALLOC_N(RARRAY(ary)->ptr, VALUE, end);
 	}
 	if (beg > RARRAY(ary)->len) {
-	    rb_mem_clear(RARRAY(ary)->ptr+RARRAY(ary)->len,end-RARRAY(ary)->len);
+	    rb_mem_clear(RARRAY(ary)->ptr + RARRAY(ary)->len, end - RARRAY(ary)->len);
 	}
 	RARRAY(ary)->len = end;
     }
-    p = RARRAY(ary)->ptr + beg; pend = p + len;
+    p = RARRAY(ary)->ptr + beg;
+    pend = p + len;
 
     if (block_p) {
 	while (p < pend) {
-	    *p++ = rb_yield(INT2NUM(beg));
-	    beg++;
+	    *p++ = rb_yield(INT2NUM(beg++));
 	}
     }
     else {

Pointer defer speed up, indents.

@@ -1445,12 +1418,14 @@
     VALUE x, y;
 {
     VALUE z;
+    long len;
 
     y = to_ary(y);
-    z = rb_ary_new2(RARRAY(x)->len + RARRAY(y)->len);
+    len = RARRAY(x)->len + RARRAY(y)->len;
+    z = rb_ary_new2(len);
     MEMCPY(RARRAY(z)->ptr, RARRAY(x)->ptr, VALUE, RARRAY(x)->len);
-    MEMCPY(RARRAY(z)->ptr+RARRAY(x)->len, RARRAY(y)->ptr, VALUE, RARRAY(y)->len);
-    RARRAY(z)->len = RARRAY(x)->len + RARRAY(y)->len;
+    MEMCPY(RARRAY(z)->ptr + RARRAY(x)->len, RARRAY(y)->ptr, VALUE, RARRAY(y)->len);
+    RARRAY(z)->len = len;
     return z;
 }

Pointer defer speedup.

@@ -1540,7 +1515,7 @@
     if (TYPE(ary2) != T_ARRAY) return Qfalse;
     if (RARRAY(ary1)->len != RARRAY(ary2)->len) return Qfalse;
     for (i=0; i<RARRAY(ary1)->len; i++) {
-	if (!rb_equal(RARRAY(ary1)->ptr[i], RARRAY(ary2)->ptr[i]))
+	if (!RTEST(rb_equal(RARRAY(ary1)->ptr[i], RARRAY(ary2)->ptr[i])))
 	    return Qfalse;
     }
     return Qtrue;

RTEST again - need it to be here?

@@ -1575,7 +1550,7 @@
 	n = rb_hash(RARRAY(ary)->ptr[i]);
 	h ^= NUM2LONG(n);
     }
-    return INT2FIX(h);
+    return LONG2FIX(h);
 }
 
 VALUE

Eye happier again...

@@ -1585,7 +1560,7 @@
 {
     long i;
     for (i=0; i<RARRAY(ary)->len; i++) {
-	if (rb_equal(RARRAY(ary)->ptr[i], item)) {
+	if (RTEST(rb_equal(RARRAY(ary)->ptr[i], item))) {
 	    return Qtrue;
 	}
     }

RTEST again.

@@ -1628,8 +1603,8 @@
     ary2 = to_ary(ary2);
     ary3 = rb_ary_new();
     for (i=0; i<RARRAY(ary1)->len; i++) {
-	if (rb_ary_includes(ary2, RARRAY(ary1)->ptr[i])) continue;
-	if (rb_ary_includes(ary3, RARRAY(ary1)->ptr[i])) continue;
+	if (RTEST(rb_ary_includes(ary2, RARRAY(ary1)->ptr[i]))) continue;
+	if (RTEST(rb_ary_includes(ary3, RARRAY(ary1)->ptr[i]))) continue;
 	rb_ary_push(ary3, RARRAY(ary1)->ptr[i]);
     }
     return ary3;

ditto.

@@ -1784,7 +1759,7 @@
     return INT2NUM(n);
 }
 
-static int
+static long
 flatten(ary, idx, ary2, memo)
     VALUE ary;
     long idx;

it returns long and all other vars, where is this used, are long - thus
I changed it to long also.

@@ -1934,4 +1909,5 @@
     rb_define_method(rb_cArray, "nitems", rb_ary_nitems, 0);
 
     id_cmp = rb_intern("<=>");
+    inspect_key = rb_intern("__inspect_key__");
 }

Here is the init for inspect_key.

	Comments are very welcomed.

	(But who would read it? :-))

		Michal
		
-- 
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
Michal Rokos                         Czech Technical University, Prague
E-mail:m.rokos@sh.cvut.cz  ICQ:36118339  Jabber:majkl@jabber.sh.cvut.cz
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

In This Thread

Prev Next