On 14-Sep-06, at 9:45 AM, Daniel Stenning wrote:

Naturally one could just use the MemoryBlock built in methods to address the
block directly, but this currently requires the developer to calculate
offsets himself.

Another option would be for MemoryBlocks to access data by [index] ( like C ) instead of by byte offset. For integer, floats, and structures this
should be doable.

You can do this yourself with a memoryblock subclass of course, and if you use a cached Ptr to avoid the memoryblock accessors this approach isn't much slower than using the built-in accessors directly. Just keep in mind the Ptr has to be refreshed when ever the block is reallocated (you can overload the Size method to do this automatically).

When you need speed instead of pretty code just access the pointer directly and calculate the offsets - I'll often define a stride constant in the memoryblock subclass to keep things manageable. With this in mind, pretty code for an array of single-precision floats might look like this:

        m.Float(3) = 1.0 // 1 virtual function call

When you need speed it looks more like:

        m.Pointer( m.kStride * 3 ) = 1.0 // No function calls!

Not pretty, but it's much faster (though sadly still not as fast as C code) and if you decide you need doubles later on just change the stride and everything will continue to work. You can also pass m.Pointer to declares to save a conversion during those calls.

I agree it would be nice to be able to define a byte stride (and even interleaved strides) for MemoryBlock and Ptr to avoid these extra steps but it would have to be implemented at the preprocessor or compiler level to be any good. I'd be happy with a more robust Ptr type - e.g. instead of "Dim p As Ptr" use something like "Pointer p As Integer" or "Pointer p As myStruct" where p's offsets match it's type.

Frank.
<http://developer.chaoticbox.com/>


_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives of this list here:
<http://support.realsoftware.com/listarchives/lists.html>

Reply via email to