Thread overview
array literals and the read only segment
Dec 11, 2016
Shachar Shemesh
Dec 11, 2016
Johan Engelen
Dec 12, 2016
Shachar Shemesh
Dec 12, 2016
Johan Engelen
Dec 12, 2016
Kagamin
December 11, 2016
Hello everyone,

Please consider the following snippet:
immutable ubyte[] array1 = [ 1, 2, 3, 4 ];
immutable ubyte[] array2 = cast(immutable ubyte[]) x"01 02 03 04";

Examining the resulting code, it is obvious that the literals initializing array2 are stored in the read only segment (.rodata), as they should be. It is equally obvious that array1 is stored in the data segment.

This is bad for a variety of reasons.

Is this a know issue?

Shachar
December 11, 2016
On Sunday, 11 December 2016 at 12:48:17 UTC, Shachar Shemesh wrote:
>
> Is this a know issue?

Which compiler(s) did you test?

-Johan

December 12, 2016
On 11/12/16 21:03, Johan Engelen wrote:
> On Sunday, 11 December 2016 at 12:48:17 UTC, Shachar Shemesh wrote:
>>
>> Is this a know issue?
>
> Which compiler(s) did you test?
>
> -Johan
>

DMD 2.072.1 and ldc 2.070.2

It's easy to verify. Just create a large array (1M) and check the segment sizes of the result.

Shachar
December 12, 2016
On Sunday, 11 December 2016 at 12:48:17 UTC, Shachar Shemesh wrote:
> Is this a know issue?

What's the issue? That array1 is in data section? That array2 is in rodata section? Or that they are in different sections?
I suppose string literal goes to rodata because all string literals go there, and everything else goes to data.
December 12, 2016
On Monday, 12 December 2016 at 06:28:09 UTC, Shachar Shemesh wrote:
> 
> DMD 2.072.1 and ldc 2.070.2
>
> It's easy to verify. Just create a large array (1M) and check the segment sizes of the result.

For LDC, it depends whether the variables are defined inside a function or not.

```
immutable ubyte[] array1 = [ 1, 2, 3, 4 ]; // ends up in data section
immutable ubyte[] array2 = cast(immutable ubyte[]) x"01 02 03 04"; // not in data section

void foo()
{
    // Both initializers are _not_ put in the data section.
    immutable ubyte[] array1 = [ 1, 2, 3, 4 ];
    immutable ubyte[] array2 = cast(immutable ubyte[]) x"01 02 03 04";
}
```

The module-scope variable's initializer ending up in the data section is a bug I think, but I have not thought it through enough.

-Johan