Skip to content

Commit 6d85d59

Browse files
committed
optimise slice reallocation using unitilizedSlice.
1 parent dcc460a commit 6d85d59

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

source/mir/ndslice/allocation.d

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,16 @@ Params:
4242
Returns:
4343
n-dimensional slice
4444
+/
45-
Slice!(Contiguous, [N], T*)
46-
slice(T, size_t N)(size_t[N] lengths...)
45+
ContiguousTensor!(N, T)
46+
slice(T, size_t N)(size_t[N] lengths...)
4747
{
4848
immutable len = lengthsProduct(lengths);
4949
return new T[len].sliced(lengths);
5050
}
5151

5252
/// ditto
53-
auto slice(T, size_t N)(size_t[N] lengths, T init)
53+
ContiguousTensor!(N, T)
54+
slice(T, size_t N)(size_t[N] lengths, T init)
5455
{
5556
immutable len = lengthsProduct(lengths);
5657
static if (!hasElaborateAssign!T)
@@ -70,7 +71,12 @@ auto slice(T, size_t N)(size_t[N] lengths, T init)
7071
/// ditto
7172
auto slice(SliceKind kind, size_t[] packs, Iterator)(Slice!(kind, packs, Iterator) slice)
7273
{
73-
auto ret = .slice!(Unqual!(slice.DeepElemType))(slice.shape);
74+
alias T = Unqual!(slice.DeepElemType);
75+
static if (hasElaborateAssign!T)
76+
alias fun = .slice;
77+
else
78+
alias fun = .uninitializedSlice;
79+
auto ret = fun!T(slice.shape);
7480
ret[] = slice;
7581
return ret;
7682
}
@@ -104,8 +110,8 @@ auto slice(size_t dim, Slices...)(Concatenation!(dim, Slices) concatenation)
104110
alias fun = .slice;
105111
else
106112
alias fun = .uninitializedSlice;
107-
auto ret = fun!(Unqual!(concatenation.DeepElemType))(concatenation.shape);
108-
ret.opIndexAssign(concatenation);
113+
auto ret = fun!T(concatenation.shape);
114+
ret[] = concatenation;
109115
return ret;
110116
}
111117

0 commit comments

Comments
 (0)