Mapping structs on top of byte arrays w/o copying

  • Thread starter Thread starter Max Metral
  • Start date Start date
M

Max Metral

I have a "high performance" application I'm building in C#. I have a
preparation program that builds a large file that is layed out "properly"
with indexes etc. So my goal is to read that file into a big byte array,
and pass offsets around to various functions and classes. My structure is
something like:

int Id;
int numItems;
byte[] foo;
int[] fooIds;
byte[] foobars;

now forget the variable length weirdness, I can get around that. My
question is, given a byte array and an offset, how could I tell C# that I
have a struct that begins at a certain point and access its members without
conversions or copies?

Thanks.
 
Hi Max,

First of all, I would like to confirm my understanding of your issue. I'm
not quite sure about the problem. Do you mean that you have a byte array
and an index. You need to deserialize the byte array from certain index to
an object you have defined. If there is any misunderstanding, please feel
free to let me know.

If so, I think you have to made a conversion by copying this byte array
from that index to a MemoryStream object and then Deserialize it. Because a
BinaryFormatter only accepts a Stream object as argument. The MemoryStream
constructor has a constructor which can do this.

public MemoryStream(
byte[] buffer,
int index,
int count
);

For more information, please check the following link:

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemiomemorystreamclassctortopic5.asp

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Come on now! All the computer science texts put it as foobar, probably to
obfuscate what it really stands for. :)

Michael Culley said:
It's spelt fubar, not foobar!! :-)

--
Michael Culley


Max Metral said:
I have a "high performance" application I'm building in C#. I have a
preparation program that builds a large file that is layed out "properly"
with indexes etc. So my goal is to read that file into a big byte array,
and pass offsets around to various functions and classes. My structure
is
something like:

int Id;
int numItems;
byte[] foo;
int[] fooIds;
byte[] foobars;

now forget the variable length weirdness, I can get around that. My
question is, given a byte array and an offset, how could I tell C# that I
have a struct that begins at a certain point and access its members
without
conversions or copies?

Thanks.
 
Thanks Kevin. I'm trying to avoid any copies. So basically I want to do
something like:

byte[] fileBuf = new byte[fileSize];
binaryStream.ReadBytes(fileBuf,fileSize);

ByteArrayStructure b = new ByteArrayStructure(fileBuf,102524);

Console.WriteLine(b.EntryId);

i.e. I want to do something almost completely antithetical to c#. :) I'm
mainly trying to recreate this c++ statement:

int entryId = ((ByteArrayStructure*) (fileBuf+offset))->EntryId

Some digging implies that this will work, but haven't fully tested it yet:

unsafe

{

fixed (byte *pData = rawBytes)

{

_bStruct = * (ByteArrayStructure*) (pData+mStart);

}

}
 
Hi Max,

This is not quite safe, because it depends on how the original object is
serialized. And it's not safe to convert a byte array directly to an object.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."
 
Back
Top