Skip to content

Commit

Permalink
make reshape always share data for Arrays. for #2279
Browse files Browse the repository at this point in the history
for now, whether reshape shares data will just have to depend on argument
type. it is popular to write things like reshape(1:4, 2, 2).
  • Loading branch information
JeffBezanson committed Mar 7, 2013
1 parent d4418a9 commit 4f8487f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
6 changes: 1 addition & 5 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,7 @@ function reshape(a::AbstractArray, dims::Dims)
if prod(dims) != length(a)
error("reshape: invalid dimensions")
end
b = similar(a, dims)
for i = 1:length(a)
b[i] = a[i]
end
return b
copy!(similar(a, dims), a)
end
reshape(a::AbstractArray, dims::Int...) = reshape(a, dims)

Expand Down
16 changes: 8 additions & 8 deletions src/array.c
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,20 @@ jl_array_t *jl_reshape_array(jl_type_t *atype, jl_array_t *data,
if (d == jl_array_inline_data_area(data)) {
if (data->ndims == 1) {
// data might resize, so switch it to shared representation.
// problem: we'd like to do that, but it might not be valid,
// since the buffer might be used from C in a way that it's
// assumed not to move. for now, just copy the data (note this
// case only happens for sizes <= ARRAY_INLINE_NBYTES)
// problem: the buffer might be used from C in a way that it's
// assumed not to move. for now just hope this doesn't happen.
size_t datalen = jl_array_len(data);
jl_mallocptr_t *mp = array_new_buffer(data, datalen);
memcpy(mp->ptr, data->data, datalen * data->elsize);
a->data = mp->ptr;
jl_array_data_owner(a) = (jl_value_t*)mp;
a->ismalloc = 1;
//data->data = mp->ptr;
//data->offset = 0;
//data->maxsize = datalen;
//jl_array_data_owner(data) = (jl_value_t*)mp;

data->data = mp->ptr;
data->offset = 0;
data->maxsize = datalen;
jl_array_data_owner(data) = (jl_value_t*)mp;
data->ismalloc = 1;
}
else {
a->ismalloc = 0;
Expand Down

0 comments on commit 4f8487f

Please sign in to comment.