On Sun, 26 May 2013 14:06:39 +0200, Joseph Rushton Wakeling
<joseph.wakel...@webdrake.net> wrote:

On 05/24/2013 04:39 PM, Simen Kjaeraas wrote:
First, *is* it read-only? If so, store it as immutable and enjoy free
sharing. If not, how and why not?

I can confess that it's as simple as feeling extremely uncomfortable dealing
with immutable where it relates to any kind of complex data structure.

I mean, for that double array structure I'd have to do something like,

        Tuple!(size_t, size_t)[][] dataCopy;

        foreach(x; data) {
                Tuple!(size_t, size_t)[] xCopy;
                foreach(y; x) {
                        immutable(Tuple!(size_t, size_t)) yCopy = y.idup;
                        xCopy ~= cast(Tuple!(size_t, size_t)) yCopy;
                }
                immutable xImm = assumeUnique(xCopy);
                dataCopy ~= cast(Tuple!(size_t, size_t)[]) xImm;
        }

        immutable dataImm = assumeUnique(dataCopy);

... no? Which feels like a lot of hassle compared to just being able to pass
each thread the information to independently load the required data.

I'd be delighted to discover I'm wrong about the hassle of converting the data
to immutable -- I don't think I understand how to use it at all well, bad
experiences in the past have meant that I've tended to avoid it.

That looks very complex for what it purports to do.

I understand data is the original data before sharing?

If so, will that array ever change again?


I think a bit more information is needed. I'm going to assume this is
(roughly) how things work:

1. Read from file/generate/load from database/create data.
2. Share data with other threads.
3. Never change data again.

If this is correct, this should work:


    Tuple!(size_t, size_t)[][] data = createData();
    immutable dataImm = assumeUnique(data);
    data = null; // Simply to ensure no mutable references exist.
    sendToOtherThreads(dataImm);


And that's it. If nobody's going to change the data again, it's perfectly
safe to tell the compiler 'this is now immutable'. No copies need to be
made, no idup, no explicit casting (except that done internally by
assumeUnique), no troubles.

--
Simen

Reply via email to