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

Possibly Related Threads…
Thread Author Replies Views Last Post
  _Putimage Question. Pete 11 704 01-04-2026, 09:33 PM
Last Post: Pete
  Nice simple, I hope, question Mad Axeman 4 360 12-20-2025, 09:28 PM
Last Post: SMcNeill
Question Experimenting with a "StringList" type for simpler handling of string arrays/lists Heimdall 18 1,226 12-19-2025, 12:51 PM
Last Post: Heimdall
  NewBie Question niteflyer 2 345 11-06-2025, 07:11 PM
Last Post: Petr
  A Question About _MAPTRIANGLE Magdha 2 388 11-02-2025, 11:37 AM
Last Post: Magdha

Forum Jump:


Users browsing this thread: 1 Guest(s)