primitive vector types
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Sat Feb 21 11:00:15 PST 2009
Jarrett Billingsley wrote:
> On Sat, Feb 21, 2009 at 12:32 PM, Andrei Alexandrescu
> <SeeWebsiteForEmail at erdani.org> wrote:
>
>> Yah. Walter agrees that that's the right thing to do. The only thing that
>> worries us is passing by-value large statically-sized vectors to template
>> functions. But then gaming code wants to do exactly that. It's hard to
>> figure where to draw the line. Imagine the error message "Hey, you're going
>> a bit overboard by passing 512 bytes around on the stack".
>
> Structs already work like this. In fact, the compiler will pass a
> struct in a register if it's 1, 2, or 4 bytes on x86. Having the
> compiler "magically" put float[4]s in SSE registers seems like a
> similar idea.
I agree.
>> Besides, we already do have a solution for pass-by-value vectors:
>> Tuple!(T[N]). That would put the burden in the right place (on the
>> programmer actively wanting pass-by-value). But then it's a shame that the
>> built-in type T[N] is a weird exception that must be handled in all template
>> code.
>
> Please make them value types. I, for one, am tired of dealing with their crap.
Ok, you just tipped the balance :o). I'm also realizing something. The
scenario I'm most afraid of is something like:
char[10000] humongous = "This is a humongous message. I will type here
exactly 10000 characters. ... ";
foreach (i; 1 .. 100_000_000) writeln(humongous);
But then there is a reason making this scenario rather scarce: for large
static arrays, it's hard to keep the claimed length (10000) in sync with
the actual length of the vectors. I used to think that's a language
defect and suggested the syntax char[$] humongous = " ... " for it, such
that the compiler infers the length from the initializer. But now I get
to think that the defect actually discourages people from defining very
large statically-sized arrays unwittingly.
With mixins and template techniques, very large static arrays can still
be generated, but such advanced uses also has a nice feedback: those who
know the language well enough to embark on such styles of coding will
also likely understand the cautions needed in making them work well.
So, yes, it seems like it's a solid choice to make statically-sized
arrays value types. Now we only need to convince Walter that
implementation is "a simple matter of coding" :o).
Andrei
More information about the Digitalmars-d
mailing list