Skip to content

Commit

Permalink
Fixed Array#concat.
Browse files Browse the repository at this point in the history
Amortizing the allocation costs over the number of elements
requires doubling the capacity rather than using the exact
size needed.
  • Loading branch information
brixen committed May 12, 2015
1 parent 52c2849 commit 053e1e9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 11 additions & 1 deletion vm/builtin/array.cpp
Expand Up @@ -194,10 +194,20 @@ namespace rubinius {
// There is enough space in the current tuple, so just copy into it.
tuple_->copy_from(state, other->tuple(), other->start(), other->total(), total_);
} else {
native_int s = size();

if(s == 0) s = 2;
while(s <= new_size) s *= 2;

// We need to create a bigger tuple, then copy both tuples into it.
Tuple* nt = Tuple::create_dirty(state, new_size);
Tuple* nt = Tuple::create_dirty(state, s);
nt->copy_from(state, tuple_, start_, total_, Fixnum::from(0));
nt->copy_from(state, other->tuple(), other->start(), other->total(), total_);

for(native_int i = new_size; i < s; i++) {
nt->put_nil(i);
}

tuple(state, nt);
}

Expand Down
4 changes: 4 additions & 0 deletions vm/builtin/tuple.hpp
Expand Up @@ -37,6 +37,10 @@ namespace rubinius {
// Rubinius.primitive :tuple_at
Object* at_prim(STATE, Fixnum* pos);

void put_nil(native_int idx) {
field[idx] = cNil;
}

Object* put(STATE, native_int idx, Object* val) {
field[idx] = val;
write_barrier(state, val);
Expand Down

0 comments on commit 053e1e9

Please sign in to comment.