Skip to content

Commit

Permalink
* array.c (rb_ary_shift): shift/unshift performance boost patch,
Browse files Browse the repository at this point in the history
  based on the patch from Eric Mahurin <eric_mahurin at yahoo.com>.
  [ruby-core:05861]

* array.c (rb_ary_unshift_m): ditto.

* array.c (ary_make_shared): ditto.

* array.c (RESIZE_CAPA): ditto.

* array.c (rb_ary_free): new function to free memory.  code moved
  from gc.c.

* string.c (rb_str_free): ditto.


git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@11038 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
  • Loading branch information
matz committed Sep 26, 2006
1 parent 4f6f0b1 commit 1113d54
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 48 deletions.
17 changes: 17 additions & 0 deletions ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,23 @@ Wed Sep 27 01:04:49 2006 Nobuyoshi Nakada <[email protected]>

* lib/mkmf.rb (have_type): simplified with typedef and sizeof.

Wed Sep 27 00:08:12 2006 Yukihiro Matsumoto <[email protected]>

* array.c (rb_ary_shift): shift/unshift performance boost patch,
based on the patch from Eric Mahurin <eric_mahurin at yahoo.com>.
[ruby-core:05861]

* array.c (rb_ary_unshift_m): ditto.

* array.c (ary_make_shared): ditto.

* array.c (RESIZE_CAPA): ditto.

* array.c (rb_ary_free): new function to free memory. code moved
from gc.c.

* string.c (rb_str_free): ditto.

Tue Sep 26 23:57:03 2006 Nobuyoshi Nakada <[email protected]>

* lib/optparse.rb (OptionParser#getopts): use strings as key.
Expand Down
131 changes: 95 additions & 36 deletions array.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ memfill(register VALUE *mem, register long size, register VALUE val)
}\
} while (0)

#define ARY_LFREE FL_USER6
#define ARY_LFREE_P(ary) FL_TEST(ary, ARY_LFREE)
#define LFREE_SIZE(ary) RARRAY(ary)->as.heap.ptr[-1]
#define LFREE_CAPA(ary) (LFREE_SIZE(ary)+RARRAY(ary)->as.heap.aux.capa)

#define ARY_CAPA(ary) ((ARY_EMBED_P(ary)) ? RARRAY_EMBED_LEN_MAX : RARRAY(ary)->as.heap.aux.capa)
#define RESIZE_CAPA(ary,capacity) do {\
if (ARY_EMBED_P(ary)) {\
Expand All @@ -77,6 +82,21 @@ memfill(register VALUE *mem, register long size, register VALUE val)
RARRAY(ary)->as.heap.aux.capa = (capacity);\
}\
}\
else if (ARY_LFREE_P(ary)) {\
VALUE *ptr = RARRAY(ary)->as.heap.ptr - LFREE_SIZE(ary);\
if (LFREE_CAPA(ary) >= (capacity)) {\
RARRAY(ary)->as.heap.aux.capa = LFREE_CAPA(ary);\
MEMMOVE(ptr, RARRAY(ary)->as.heap.ptr, VALUE, RARRAY_LEN(ary));\
FL_UNSET(ary, ARY_LFREE);\
RARRAY(ary)->as.heap.ptr = ptr;\
}\
else {\
long offset = LFREE_SIZE(ary);\
REALLOC_N(ptr, VALUE, offset+(capacity));\
RARRAY(ary)->as.heap.aux.capa = (capacity);\
RARRAY(ary)->as.heap.ptr = ptr + offset;\
}\
}\
else {\
REALLOC_N(RARRAY(ary)->as.heap.ptr, VALUE, (capacity));\
RARRAY(ary)->as.heap.aux.capa = (capacity);\
Expand Down Expand Up @@ -223,6 +243,19 @@ rb_ary_new4(long n, const VALUE *elts)
return ary;
}

void
rb_ary_free(VALUE ary)
{
if (!ARY_SHARED_P(ary) && !ARY_EMBED_P(ary)) {
if (ARY_LFREE_P(ary)) {
xfree(RARRAY(ary)->as.heap.ptr - RARRAY(ary)->as.heap.ptr[-1]);
}
else {
xfree(RARRAY(ary)->as.heap.ptr);
}
}
}

static VALUE
ary_make_shared(VALUE ary)
{
Expand All @@ -238,6 +271,10 @@ ary_make_shared(VALUE ary)
shared->as.heap.len = RARRAY(ary)->as.heap.len;
shared->as.heap.ptr = RARRAY(ary)->as.heap.ptr;
shared->as.heap.aux.capa = RARRAY(ary)->as.heap.aux.capa;
if (ARY_LFREE_P(ary)) {
FL_SET(shared,ARY_LFREE);
FL_UNSET(ary,ARY_LFREE);
}
RARRAY(ary)->as.heap.aux.shared = (VALUE)shared;
FL_SET(ary, ELTS_SHARED);
OBJ_FREEZE(shared);
Expand Down Expand Up @@ -578,18 +615,23 @@ rb_ary_shift(VALUE ary)
rb_ary_modify_check(ary);
if (RARRAY_LEN(ary) == 0) return Qnil;
top = RARRAY_PTR(ary)[0];
if (RARRAY_LEN(ary) < ARY_DEFAULT_SIZE && !FL_TEST(ary, ELTS_SHARED)) {
if (ARY_EMBED_P(ary)) {
MEMMOVE(RARRAY_PTR(ary), RARRAY_PTR(ary)+1, VALUE, RARRAY_LEN(ary));
ARY_SET_LEN(ary, RARRAY_LEN(ary)-1);
ARY_SET_EMBED_LEN(ary, RARRAY_LEN(ary)-1);
return top;
}
else {
if (!FL_TEST(ary, ELTS_SHARED)) {
RARRAY_PTR(ary)[0] = Qnil;
if (!ARY_SHARED_P(ary)) {
if (ARY_LFREE_P(ary)) {
RARRAY(ary)->as.heap.ptr[0] = RARRAY(ary)->as.heap.ptr[-1]+1;
}
else {
FL_SET(ary, ARY_LFREE);
RARRAY(ary)->as.heap.ptr[0] = 1;
}
ary_make_shared(ary);
RARRAY(ary)->as.heap.ptr++; /* shift ptr */
RARRAY(ary)->as.heap.len--;
RARRAY(ary)->as.heap.aux.capa--;
}
RARRAY(ary)->as.heap.ptr++; /* shift ptr */
RARRAY(ary)->as.heap.len--;

return top;
}
Expand Down Expand Up @@ -640,26 +682,6 @@ rb_ary_shift_m(int argc, VALUE *argv, VALUE ary)
return result;
}

VALUE
rb_ary_unshift(VALUE ary, VALUE item)
{
rb_ary_modify(ary);
if (RARRAY_LEN(ary) == ARY_CAPA(ary)) {
long capa_inc = ARY_CAPA(ary) / 2;
if (capa_inc < ARY_DEFAULT_SIZE) {
capa_inc = ARY_DEFAULT_SIZE;
}
RESIZE_CAPA(ary, ARY_CAPA(ary)+capa_inc);
}

/* sliding items */
MEMMOVE(RARRAY_PTR(ary) + 1, RARRAY_PTR(ary), VALUE, RARRAY_LEN(ary));
ARY_SET_LEN(ary, RARRAY_LEN(ary)+1);
RARRAY_PTR(ary)[0] = item;

return ary;
}

/*
* call-seq:
* array.unshift(obj, ...) -> array
Expand All @@ -675,20 +697,57 @@ rb_ary_unshift(VALUE ary, VALUE item)
static VALUE
rb_ary_unshift_m(int argc, VALUE *argv, VALUE ary)
{
long len = RARRAY_LEN(ary);

long lfree = ARY_LFREE_P(ary) ? RARRAY_PTR(ary)[-1] : 0;

rb_ary_modify_check(ary);
if (argc == 0) return ary;

/* make rooms by setting the last item */
rb_ary_store(ary, len + argc - 1, Qnil);

/* sliding items */
MEMMOVE(RARRAY_PTR(ary) + argc, RARRAY_PTR(ary), VALUE, len);
if (lfree < argc) {
int shared = ARY_SHARED_P(ary);
long len = RARRAY_LEN(ary);
long rfree = shared ? 0 : (ARY_CAPA(ary)-len)/2;
long free2 = argc+(RARRAY_LEN(ary)+argc)/2;
VALUE *ptr;
if (free2 < ARY_DEFAULT_SIZE) {
free2 = ARY_DEFAULT_SIZE;
}
ptr = ALLOC_N(VALUE,len+free2)+(free2-rfree);
MEMCPY(ptr, RARRAY_PTR(ary), VALUE, len);
if (shared) {
FL_UNSET(ary, ELTS_SHARED);
}
else if (ARY_EMBED_P(ary)) {
ARY_SET_NOEMBED(ary);
}
else {
free(RARRAY_PTR(ary)-lfree);
}
RARRAY(ary)->as.heap.ptr = ptr;
RARRAY(ary)->as.heap.len = len;
RARRAY(ary)->as.heap.aux.capa = len+rfree;
lfree = free2-rfree;
FL_SET(ary, ARY_LFREE);
}
RARRAY(ary)->as.heap.ptr -= argc;
RARRAY(ary)->as.heap.len += argc;
RARRAY(ary)->as.heap.aux.capa += argc;
lfree -= argc;
if (lfree > 0) {
RARRAY(ary)->as.heap.ptr[-1] = lfree;
} else {
FL_UNSET(ary, ARY_LFREE);
}
MEMCPY(RARRAY_PTR(ary), argv, VALUE, argc);

return ary;
}

VALUE
rb_ary_unshift(VALUE ary, VALUE item)
{
return rb_ary_unshift_m(1,&item,ary);
}

/* faster version - use this if you don't need to treat negative offset */
static inline VALUE
rb_ary_elt(VALUE ary, long offset)
Expand Down
10 changes: 2 additions & 8 deletions gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1174,16 +1174,10 @@ obj_free(VALUE obj)
}
break;
case T_STRING:
if (FL_TEST(obj, RSTRING_NOEMBED) &&
RANY(obj)->as.string.as.heap.ptr && !FL_TEST(obj, ELTS_SHARED)) {
RUBY_CRITICAL(free(RANY(obj)->as.string.as.heap.ptr));
}
rb_str_free(obj);
break;
case T_ARRAY:
if (FL_TEST(obj, RARRAY_NOEMBED) &&
RANY(obj)->as.array.as.heap.ptr && !FL_TEST(obj, ELTS_SHARED)) {
RUBY_CRITICAL(free(RANY(obj)->as.array.as.heap.ptr));
}
rb_ary_free(obj);
break;
case T_HASH:
if (RANY(obj)->as.hash.tbl) {
Expand Down
6 changes: 2 additions & 4 deletions intern.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ VALUE rb_ary_new(void);
VALUE rb_ary_new2(long);
VALUE rb_ary_new3(long,...);
VALUE rb_ary_new4(long, const VALUE *);
VALUE rb_values_new(long,...);
VALUE rb_values_new2(long, const VALUE *);
VALUE rb_values_from_ary(VALUE);
VALUE rb_ary_from_values(VALUE);
void rb_ary_free(VALUE);
VALUE rb_ary_freeze(VALUE);
VALUE rb_ary_aref(int, VALUE*, VALUE);
void rb_ary_store(VALUE, long, VALUE);
Expand Down Expand Up @@ -488,6 +485,7 @@ VALUE rb_tainted_str_new(const char*, long);
VALUE rb_tainted_str_new2(const char*);
VALUE rb_str_buf_new(long);
VALUE rb_str_buf_new2(const char*);
void rb_str_free(VALUE);
VALUE rb_str_buf_append(VALUE, VALUE);
VALUE rb_str_buf_cat(VALUE, const char*, long);
VALUE rb_str_buf_cat2(VALUE, const char*);
Expand Down
8 changes: 8 additions & 0 deletions string.c
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,14 @@ rb_str_buf_new2(const char *ptr)
return str;
}

void
rb_str_free(VALUE str)
{
if (!STR_EMBED_P(str) && !STR_SHARED_P(str)) {
xfree(RSTRING(str)->as.heap.ptr);
}
}

VALUE
rb_str_to_str(VALUE str)
{
Expand Down

0 comments on commit 1113d54

Please sign in to comment.