views:

40

answers:

2

I've created ILockBytesOnHGlobal and I write 64k of data repeatedly. What I've noticed is that WriteAt performance decreases over the time.

What could be the reason for the performance slow down? Does it have to do with stream growth? Here is what I'm doing (in C#)

    public override void Write(byte[] buffer, int offset, int count)
    {
        EnsureBufferSize(count);
        Marshal.Copy(buffer, offset, hGlobalBuffer, count);
        lockBytes.WriteAt(writeOffset, hGlobalBuffer, count, out temp);
        writeOffset += temp.ToUInt32();
    }
+1  A: 

Hi there.

This is a pure guess, but I found this in the documentation of WriteAt

If ulOffset is past the end of the byte array and cb is greater than zero, ILockBytes::WriteAt increases the size of the byte array. The fill bytes written to the byte array are not initialized to any particular value.

I found the documentation here. Could it be that your input array is being increased in size? Do you need to close any handles that your code is using. Often with the Win32 API you need to close handles for any handles that your code is opening.

Sorry I can't be of more help.

Cheers. Jas.

Jason Evans
+1  A: 

CreateILockBytesOnHGlobal documentation says that it uses GlobalReAlloc to increase the memory block. GlobalReAlloc copies the data from the old memory block to the new (and larger) memory block, so this causes performance to go down over time.

Pent Ploompuu
Ah, I missed that. Sounds like the culprit!
Jason Evans