Thanks a lot David.
Actually you're right about calculating an offset using a single
dimensional array (and that's what I'm currently doing) and normally I'm
not sure if I would calculate an offset in a 2 dimensional array, but
was only wondering why it's not being done this way down at the IL
level.. but I got my answer :)
I translated some numerical code written in C to C# and was using
rectangular arrays (instead of sparse arrays) and noticed a performance
hit. I thought maybe it's because of the get set method calls, but as
you say, they're inlined and are treated as intrinsics.. so they
shouldn't matter, and I guess it really must be due to bounds checking.
On very large arrays, I see quite a large difference if I choose to use
rectangular arrays. Does this imply that the IL instructions you have
for jagged arrays (ldelem and stelem) aren't any different from the Get
and Set methods in regard to performance (or at least they shouldn't be
any different)? They should essentially perform equally well but because
currently range checks aren't properly eliminated for rectangular
arrays, we end up with rectangular arrays performing badly?
Thanks again for the clarification David, I can sleep well tonight :)
cheers,
-Andre
[quoted text, click to view] David Notario wrote:
> Hi Andre.
>
> The JIT knows about those helpers and treats them as an intrinsic. We
> currently inline 2 and 3 dimensional arrays, so the code is generated inline
> and optimizable (as in removing range checks, and potentially using a single
> offset), so we could potentially end up generating optimal code (we could do
> it for more dimensions, but you could end up have more bloated code, and 4+
> dimensional arrays are not so common, so we go through the helper for
> those). Unfortunately, I've noticed that we're not so good removing those
> range checks in the 2 and 3 dimensional case as we could and I'm not
> convinced this will be a high priority item for us. Let us know how
> important this is for you.
>
> We cannot use an offset (at least in IL), because we have to do range
> checking. I dont know why they didn't implement the Get and Set as a new IL
> instructions (a good argument is that probably they didnt like the idea of
> having IL instructions having a variable number of arguments, and then
> decided to use a helper. I dont think this matters, though, the JIT can
> treat them as intrinsics). If you want to use a single offset, use single
> dimensional arrays and calculate it yourself (that's what I would do if
> writing code)
>