Fluxtah

There does not seem to be a way to access vector elements using array notation, this is really useful for algorithms, any plans for this in the future

Fluxtah



Re: XNA Framework Vector array style

DukeZZZ

accessing vector element using ".field" notation is faster than accessing as "[index]" notation, because accessing trough an index requires some extra math, while accessing as a field requires a simpler memory addressing.
Considering that accessing vector element in a 3d game is one of the most frequent operations, saving even only 1 instruction per access will produce a great benefit in terms of performance (anyway, i think that the difference in the operatin count between the 2 methods is more than only 1 instruction per access).

last: unfortunately when you write real time code you always have to move the balance to the performance and sacrifice the code elegance. consider that players will only see how smooth your game will run , and not how elegant your code is :(




Re: XNA Framework Vector array style

Fluxtah

your probably right, its just a shame that my book for octree object insertion demonstrates an algorithm using array indexed vectors, so I guess I am just going to have to write out each iteration where it loops over the axis.





Re: XNA Framework Vector array style

Jon Watte

accessing vector element using ".field" notation is faster than accessing as "[index]" notation, because accessing trough an index requires some extra math, while accessing as a field requires a simpler memory addressing


Well, maybe, on some CPUs.

However, both x86 and PPC have instructions that access "register plus register displacement" in a single cycle, just like they have "register plus constant" in a single cycle. Thus, there is really no micro-optimization difference between the two.

Meanwhile, it's unlikely that, even if there were a difference in instruction count, that you would notice a difference in practice. The cache miss of accessing the vertex data in the first place will likely swallow the entire cost of calculating -- even on an in-order pipeline like the PPC found in the Xbox.

Now, if you want indexing, you can take the address of the "x" element as a float*, and index that. You'll need to use unsafe code to do this, but it's quite possible. Just don't index outside the actual vertex, or you'll be ... unsafe :-)







Re: XNA Framework Vector array style

DukeZZZ

ok for the address & displacement instruction in 1 cicle (and i think this is also used for the .field access style) but for an [index] access i think you still need a multiplication for the size of the element indexed before to load the offset in the offset register, or i've missed something

(i don't know the instruction set so deeply, but i think the offset have to be expressed in byte so you have to do a *4 (or left shif of 2) in order to address float, while the .field offsed could be _always_ precomputed at the compile time without ambiguity, and it isn't always possible precompute the offset for the [index] access... ops.. now i've realized that probably exists some 32 bit aligned version of the address&displacement operation you have mentioned, could you confirm )

please correct me if i'm in error.

 

 





Re: XNA Framework Vector array style

Fluxtah

Its not so important I guess at the moment, just seems a shame I have to decompose a very nice way of doing things from a book, teach me for always buying C++ games books, but thats all you can get really, I can live with what i have for now and if I need an optimisation, I will optimise later.

Make it work, make it right, make it fast (I should really abide by these rules more :))