www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - array initializers

reply "Trass3r" <un known.com> writes:
If you have
immutable int[] arr = [0,1,0,3];

Couldn't the type of the literal be inferred as immutable?
Then you could put the data into read-only memory, and maybe even 
elide the copy to the heap?
The immutable arr type is even passed to 
ArrayLiteralExp::inferType but doesn't influence the literal 
type. But not sure what this is supposed to do.
Jul 11 2014
next sibling parent "bearophile" <bearophileHUGS lycos.com> writes:
Trass3r:

 If you have
 immutable int[] arr = [0,1,0,3];

 Couldn't the type of the literal be inferred as immutable?
 Then you could put the data into read-only memory,
Did you mean this? immutable int[4] arr = [0, 1, 0, 3]; Bye, bearophile
Jul 11 2014
prev sibling parent reply "David Nadlinger" <code klickverbot.at> writes:
On Friday, 11 July 2014 at 17:25:47 UTC, Trass3r wrote:
 Couldn't the type of the literal be inferred as immutable?
 Then you could put the data into read-only memory, and maybe 
 even elide the copy to the heap?
By the way, LDC already does this today (even without optimizations turned on). David
Jul 11 2014
parent reply "Trass3r" <un known.com> writes:
 By the way, LDC already does this today (even without 
 optimizations turned on).
My ldc doesn't. I had to cast(immutable) to actually get it to put the data as a constant. And even then it's still copied to the GC heap.
Jul 11 2014
parent reply "David Nadlinger" <code klickverbot.at> writes:
On Friday, 11 July 2014 at 18:36:14 UTC, Trass3r wrote:
 By the way, LDC already does this today (even without 
 optimizations turned on).
My ldc doesn't.
What LDC version are you on? With Git master, this --- auto foo() { immutable int[] arr = [0, 1, 0, 3]; return arr; } --- produces (with optimizations on, but just for brevity) --- ret { i64, i32* } { i64 4, i32* getelementptr inbounds ([4 x i32]* .immutablearray, i32 0, i32 0) } } --- Cheers, David
Jul 11 2014
parent reply "Trass3r" <un known.com> writes:
 auto foo() {
    immutable int[] arr = [0, 1, 0, 3];
    return arr;
 }
 ---
 produces (with optimizations on, but just for brevity)
 ---

   ret { i64, i32* } { i64 4, i32* getelementptr inbounds ([4 x 
 i32]*  .immutablearray, i32 0, i32 0) }
 }
 ---
Indeed. But bool blub() { immutable int[] arr = [0,0,0,0,0,0,0,0]; return arr == [1,1,1,1,1,1,1,1]; } yields .immutablearray = internal constant [8 x i32] zeroinitializer .arrayliteral = internal unnamed_addr constant [8 x i32] [i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1] %.gc_mem = tail call { i64, i8* } _d_newarrayvT(%object.TypeInfo* _D11TypeInfo_Ai6__initZ, i64 8) %.ptr = extractvalue { i64, i8* } %.gc_mem, 1 tail call void llvm.memcpy.p0i8.p0i8.i64(i8* %.ptr, i8* bitcast ([8 x i32]* .arrayliteral to i8*), i64 32, i32 1, i1 false) %tmp27 = tail call i32 _adEq2({ i64, i8* } { i64 8, i8* bitcast ([8 x i32]* .immutablearray to i8*) }, { i64, i8* } %.gc_mem, %object.TypeInfo* bitcast (%"typeid(immutable(int)[])"* %tmp28 = icmp ne i32 %tmp27, 0 ret i1 %tmp28 }
Jul 11 2014
parent reply "Trass3r" <un known.com> writes:
And

private immutable int[] aaa = [0,1,2,3,4,5,6,7];
int foo() pure nothrow
{
	int sum;
	foreach (int i; aaa)
		sum += i;
	return sum;
}

 _D5immut3aaayAi = constant { i64, i32* } { i64 8, i32* 
getelementptr inbounds ([8 x i32]*  .constarray, i32 0, i32 0) }
 .constarray = internal global [8 x i32] [i32 0, i32 1, i32 2, 
i32 3, i32 4, i32 5, i32 6, i32 7]


forbody:
   %tmp15 = load i32* getelementptr inbounds ([8 x i32]* 
 .constarray, i64 0, i64 0), align 16
   %tmp15.1 = load i32* getelementptr inbounds ([8 x i32]* 
 .constarray, i64 0, i64 1), align 4
   %tmp19.1 = add i32 %tmp15.1, %tmp15
   %tmp15.2 = load i32* getelementptr inbounds ([8 x i32]* 
 .constarray, i64 0, i64 2), align 8
   %tmp19.2 = add i32 %tmp15.2, %tmp19.1
...


while with cast(immutable)[0,1,... :

 _D5immut3aaayAi = constant { i64, i32* } { i64 8, i32* 
getelementptr inbounds ([8 x i32]*  .dynarrayStorage, i32 0, i32 
0) }
 .dynarrayStorage = internal unnamed_addr constant [8 x i32] [i32 
0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7]


endfor:
   ret i32 28
}
Jul 11 2014
parent "Trass3r" <un known.com> writes:
Can you reproduce this?
Jul 13 2014