Re: A portion of long data bytes as a property

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Sin Jeong-hun wrote:
[...] In the original code I wrote, it would be very
inefficient because every time "Body" is accessed a new copy will be
created.

Define "very inefficient". Inefficient compared to what? Creating an impact to what degree in the rest of the code?

class Packet
{
private:
char EntireData[1024];
public:
const char* GetBody()
{
int HeaderLength=12;
return EntireData+HeaderLength;
}
};

Like this C++ code. Getting Body doesn't involve any copying.

That's true. But it also exposes implementation detail to client code, making it that much more convenient to shoot yourself in the foot.

Short answer: no, you can't do this in C#.

Slightly longer answer: copying the array isn't really that big of a problem most of the time; usually that copy is going to be sent off somewhere that is WAY slower than memory access. You could copy the array in memory a dozen, maybe even a hundred times, before the overhead becomes noticeable.

There are exceptions to every rule, of course. But for most purposes it's true.

Note that if performance really is a big concern, you may prefer to use the Buffer.BlockCopy() method instead of Array.Copy(). It doesn't do as much validation on the operation, copying raw bytes from one place to the other (even though it can handle any type of array), and so is somewhat faster.

That said, here's a slight variation on what I posted earlier that might work for you:

void DoSomething()
{
FileStream fs;
Packet p;
...
p.UseBody((byte[] rgb, int ibFirst, int cbLength) =>
fs.Write(rgb, ibFirst, cbLength));
}

class Packet
{
byte[] EntireData;
....

public void UseBody(Action<byte[], int, int> actionUse)
{
actionUse(EntireBody, HeaderSize, EntireSize - HeaderSize);
}

public byte[] Body
{
get
{
byte[] rtn = new byte[EntireSize - HeaderSize];
Array.Copy(EntireData, HeaderSize, rtn, BodySize);
return rtn;
}
}
}

That of course exposes implementation details, as your proposed C/C++ variant would. But, it avoids the client having to query three different properties, or pass local variables to some method that returns three different values.

Note that whether you return the byte[] reference to the caller, or you pass it to a delegate the caller provides, you can improve your encapsulation by returning/passing not the byte[] itself, but the result of calling AsReadOnly() on the byte[]. Of course, this is only useful if you don't literally need a byte[]; if you're writing the bytes to a Stream of some sort, for example, then that's probably not going to work for you. I mention it only if it should come up in other situations where it would be feasible.

Pete
.



Relevant Pages

  • [opensuse] RAID5 hangs
    ... I was happy copying over my data to a new array of 4x500GB and it ... Active Devices: 4 ... Number Major Minor RaidDevice State ...
    (SuSE)
  • Re: copy of very large files freezes system
    ... >> Copying lots of small files isn't blindly ... Direct Memory Access, yes the memory could be the problem. ... P.s. the source drive is an adaptec 2410S based raid array. ... I'm suspecting problems with the adaptec controller or ...
    (comp.os.linux)
  • Re: Array.Clear vs List<>.Clear
    ... so every time you add one element over the List.Capacity, the it will create a new array, copy the elements, add the new item and then destroying the old array. ... default size starts out at 4 elements, and each time you exceed the capacity of the list, the newly allocated storage has double the previous capacity. ... It's true that for smaller lists, there are more allocations, and so the copying is a larger part of the overall time cost. ... But then, for smaller lists, the total time cost is significantly lower anyway, so that proportional difference isn't really a big deal. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: set double array to 0.0
    ... >> appropriate copying. ... >> correct data versus a memmove? ... >> the memory overlap or not?! ... > You are quite happy with that array, need it to be an array because ...
    (comp.lang.c)
  • GetDib unexplained crash
    ... BmpBuf, BmpInfoBuf: array of Byte; ... SetLength(BmpInfoBuf, HeaderSize); ...
    (microsoft.public.win32.programmer.mmedia)