DMD optimization bug?

Jacob Carlborg doob at me.com
Fri Mar 22 02:01:38 PDT 2013


On 2013-03-22 09:14, Dmitry Olshansky wrote:

> OK so c points to [1, 2, 3, 4, 5] (and who knows if it's 5 bytes or 5
> ints) slice. *c is that slice, (cast(int[])*c)[1..3] is pieces of that
> slice an r-value.
> Mmm so you are returning a pointer to r-value (a local temporary)?
> What's the point of the code?

I'm showing here a reduced minimal test case, that's why it looks 
strange. The full source code is here:

https://github.com/jacob-carlborg/orange/blob/master/orange/serialization/Serializer.d#L1672

private T* getDeserializedSlice (T) (Slice slice)
{
     if (auto array = slice.id in deserializedSlices)
         return &(cast(T) *array)[slice.offset .. slice.offset + 
slice.length];

     return null;
}

In my serializer I'm storing already deserialized arrays in a 
associative array, looking like this:

void[][size_t] deserializedSlices;

The "size_t" is a unique identification. What I'm doing here is 
returning a slice from an already deserialized array. "Slice" is a 
struct as follows:

struct Slice
{
     size_t id; // id of the array the slice originates to
     size_t offset; // start position of the slice in the array
     size_t length; // length of the slice
}

What I'm doing in "getDeserializedSlice" is checking if the slice id is 
available in the associative array. If it is I deference the array 
(since I'm getting a pointer from the "in" expression), then casting it 
to the correct array type. After that I'm slicing the array and returns 
a pointer to it. I figured that was safe since it's always stored in the 
associative array, but that might not be the case.

I guess I could return the slice by an out parameter instead and return 
a bool from the function.

-- 
/Jacob Carlborg


More information about the Digitalmars-d mailing list