Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about _MEM blocks and arrays
#17
(06-04-2025, 07:31 PM)madscijr Wrote:
(06-04-2025, 04:21 PM)SMcNeill Wrote: A practical use for mem?   

Anything that requires speed.  Big Grin

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)?
Steve is overstating the difference between the two Tongue Functionally both do the exact same thing already and will have extremely similar performance. A slight difference is that arrays don't have to start from zero (Ex. `Dim a(20 to 40)`) so a subtraction has to be done to take an index like `a(21)` and make it zero-based (`21 - 20`), but overall that's pretty minor. Normal array access of a single element via `a(index)` vs `_MemGet()` are basically the same.

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).
Reply


Messages In This Thread
RE: Question about _MEM blocks and arrays - by DSMan195276 - 06-08-2025, 04:48 PM



Users browsing this thread: 2 Guest(s)