Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit 4161631

Browse files
committed
Fix _d_arrayctor refactor to use NRVO
Signed-off-by: Teodor Dutu <teodor.dutu@gmail.com>
1 parent 5a4b83b commit 4161631

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/core/internal/array/construction.d

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module core.internal.array.construction;
2121
* purity, and throwabilty checks. To prevent breaking existing code, this function template
2222
* is temporarily declared `@trusted` until the implementation can be brought up to modern D expectations.
2323
*/
24-
T[] _d_arrayctor(Tarr : T[], T)(scope Tarr from, size_t length) @trusted
24+
Tarr1 _d_arrayctor(Tarr1 : T1[], Tarr2 : T2[], T1, T2)(scope Tarr2 from) @trusted
2525
{
2626
pragma(inline, false);
2727
import core.internal.traits : hasElaborateCopyConstructor, Unqual;
@@ -30,10 +30,9 @@ T[] _d_arrayctor(Tarr : T[], T)(scope Tarr from, size_t length) @trusted
3030
import core.stdc.stdint : uintptr_t;
3131
debug(PRINTF) import core.stdc.stdio : printf;
3232

33-
debug(PRINTF) printf("_d_arrayctor(to = %p,%d, from = %p,%d) size = %d\n", from.ptr, from.length, to.ptr, to.length, T.tsize);
33+
debug(PRINTF) printf("_d_arrayctor(to = %p,%d, from = %p,%d) size = %d\n", from.ptr, from.length, to.ptr, to.length, T1.tsize);
3434

35-
T[] to;
36-
to.length = length;
35+
Tarr1 to = void;
3736

3837
void[] vFrom = (cast(void*)from.ptr)[0..from.length];
3938
void[] vTo = (cast(void*)to.ptr)[0..to.length];
@@ -49,13 +48,11 @@ T[] _d_arrayctor(Tarr : T[], T)(scope Tarr from, size_t length) @trusted
4948
(cast(Type)&enforceRawArraysConformableNogc)(action, elementSize, a1, a2, false);
5049
}
5150

52-
enforceRawArraysConformable("initialization", T.sizeof, vFrom, vTo);
51+
enforceRawArraysConformable("initialization", T1.sizeof, vFrom, vTo);
5352

54-
static if (hasElaborateCopyConstructor!T)
53+
static if (hasElaborateCopyConstructor!T1)
5554
{
56-
// Use uint instead of size_t as a temporary workaround until this bug is fixed:
57-
// https://issues.dlang.org/show_bug.cgi?id=22372
58-
uint i;
55+
size_t i;
5956
try
6057
{
6158
for (i = 0; i < to.length; i++)
@@ -67,7 +64,7 @@ T[] _d_arrayctor(Tarr : T[], T)(scope Tarr from, size_t length) @trusted
6764
*/
6865
while (i--)
6966
{
70-
auto elem = cast(Unqual!T*)&to[i];
67+
auto elem = cast(Unqual!T1*)&to[i];
7168
destroy(*elem);
7269
}
7370

@@ -77,7 +74,7 @@ T[] _d_arrayctor(Tarr : T[], T)(scope Tarr from, size_t length) @trusted
7774
else
7875
{
7976
// blit all elements at once
80-
memcpy(cast(void*) to.ptr, from.ptr, to.length * T.sizeof);
77+
memcpy(cast(void*) to.ptr, from.ptr, to.length * T1.sizeof);
8178
}
8279

8380
return to;
@@ -95,7 +92,7 @@ T[] _d_arrayctor(Tarr : T[], T)(scope Tarr from, size_t length) @trusted
9592

9693
S[4] arr1;
9794
S[4] arr2 = [S(0), S(1), S(2), S(3)];
98-
arr1 = _d_arrayctor(arr2[], arr1.length);
95+
arr1 = _d_arrayctor!(typeof(arr1))(arr2[]);
9996

10097
assert(counter == 4);
10198
assert(arr1 == arr2);
@@ -118,7 +115,7 @@ T[] _d_arrayctor(Tarr : T[], T)(scope Tarr from, size_t length) @trusted
118115

119116
S[4] arr1;
120117
S[4] arr2 = [S(0), S(1), S(2), S(3)];
121-
arr1 = _d_arrayctor(arr2[], arr1.length);
118+
arr1 = _d_arrayctor!(typeof(arr1))(arr2[]);
122119

123120
assert(counter == 4);
124121
assert(arr1 == arr2);
@@ -144,7 +141,7 @@ T[] _d_arrayctor(Tarr : T[], T)(scope Tarr from, size_t length) @trusted
144141
{
145142
Throw[4] a;
146143
Throw[4] b = [Throw(1), Throw(2), Throw(3), Throw(4)];
147-
a = _d_arrayctor(b[], a.length);
144+
a = _d_arrayctor!(typeof(a))(b[]);
148145
}
149146
catch (Exception)
150147
{
@@ -169,7 +166,7 @@ T[] _d_arrayctor(Tarr : T[], T)(scope Tarr from, size_t length) @trusted
169166
{
170167
NoThrow[4] a;
171168
NoThrow[4] b = [NoThrow(1), NoThrow(2), NoThrow(3), NoThrow(4)];
172-
a = _d_arrayctor(b[], a.length);
169+
a = _d_arrayctor!(typeof(a))(b[]);
173170
}
174171
catch (Exception)
175172
{
@@ -276,7 +273,7 @@ void _d_arraysetctor(Tarr : T[], T)(scope Tarr p, scope ref T value) @trusted
276273
{
277274
Throw[4] a;
278275
Throw[4] b = [Throw(1), Throw(2), Throw(3), Throw(4)];
279-
a = _d_arrayctor(b[], a.length);
276+
a = _d_arrayctor!(typeof(a))(b[]);
280277
}
281278
catch (Exception)
282279
{

0 commit comments

Comments
 (0)