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.
brixen committed May 12, 2015

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent 52c2849 commit 053e1e9
Showing 2 changed files with 15 additions and 1 deletion.
12 changes: 11 additions & 1 deletion vm/builtin/array.cpp
Original file line number Diff line number Diff line change
@@ -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);
}

4 changes: 4 additions & 0 deletions vm/builtin/tuple.hpp
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 053e1e9

Please sign in to comment.