06-08-2025, 04:48 PM
(06-04-2025, 07:31 PM)madscijr Wrote:Steve is overstating the difference between the two(06-04-2025, 04:21 PM)SMcNeill Wrote: A practical use for mem?
Anything that requires speed.
Direct memory manipulation is much faster than referenced access to the same memory. Array(index) has to first look up the index, then calculate the offset position of that index, then check the memory location for the data you want, then return it to you. With mem, you specify the offset yourself, the amount of data you want, and presto, retrieve it
My question is, can't we update the compiler to be a little smarter, and generate code that uses these direct memory manipulation tricks, for commands like array(index)?

If you need to move large parts of an array though then `_Mem` is faster, `_MemCopy` uses faster techniques than just doing a loop. In theory the compiler could do optimizations like that, but determining whether a loop in the code can be replaced is complicated and in many cases not possible.
For example, a looping copy of an array to shift elements has pretty specific behavior when there's an error - it will copy every element before it hits the index that is too large. Optimizing the loop while also maintaining the same behavior on errors is pretty messy, `_MemCopy` for example does not have the same behavior (it errors before doing any copying). The alternative of determining at compile-time that the code cannot hit an error is also hard (and in many cases not possible).