byte[] to byte*... What is the fastest way?

T

ThunderMusic

Hi,
The subject says it all... I want to use a byte[] and use it as byte* so I
can increment the pointer to iterate through it.

What is the fastest way of doing so in C#?

Thanks

ThunderMusic
 
M

Mattias Sjögren

The subject says it all... I want to use a byte[] and use it as byte* so I
can increment the pointer to iterate through it.

What is the fastest way of doing so in C#?

The fixed statement?


Mattias
 
N

Nicholas Paldino [.NET/C# MVP]

ThunderMusic,

Use unsafe code:

byte[] bytes = ...;

unsafe
{
fixed (byte* p = bytes)
{
// Work with pointer here.
}
}

As a matter of fact, that's the only way to do it, as you need to pin
down the location of the array to prevent the reference from moving around.

Is there a reason you need the pointer, or are you just looking for a
faster way to iterate through the array?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

ThunderMusic said:
Hi,
The subject says it all... I want to use a byte[] and use it as byte* so I
can increment the pointer to iterate through it.

What is the fastest way of doing so in C#?

Out of curiosity, why you want to do that?

What is wrong with using a indexer like
for( int i=0; i< buffer.Lengh; i++
buffer .....
 
C

Chris Mullins [MVP]

ThunderMusic said:
The subject says it all... I want to use a byte[] and use it as byte* so I
can increment the pointer to iterate through it.

I really hate to be pedantic, but I'm willing to bet that the difference in
how you iterate through your array makes little to no difference in the
overall performance of your code.

People frequently are guilting of over-optimizing things that are already
"Fast Enough". Unless you've verified this section is slow via a Profiler,
you're better off not getting fancy with optimizations.
 
T

ThunderMusic

hi,
thanks for the really quick answer... Your solution works, but I can't
do p++ because it's fixed. So I must use another pointer like byte* p2 = p;
so now I can do p2++;...

Actually, I'm looking for a faster way to compute a checksum on a byte
array... For now, I'm using the Adler-32 algorithm, but I'm open to advises
on a performant checksum algorithm. It will be for an error checking
mecanism for tcp and udp communication on a closed network environment. So
it doesn't need to be human-modification resistant, it's just to prevent
modification due to the noise on the line (if it can happen)...

Thanks

ThunderMusic

Nicholas Paldino said:
ThunderMusic,

Use unsafe code:

byte[] bytes = ...;

unsafe
{
fixed (byte* p = bytes)
{
// Work with pointer here.
}
}

As a matter of fact, that's the only way to do it, as you need to pin
down the location of the array to prevent the reference from moving
around.

Is there a reason you need the pointer, or are you just looking for a
faster way to iterate through the array?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ThunderMusic said:
Hi,
The subject says it all... I want to use a byte[] and use it as byte* so
I can increment the pointer to iterate through it.

What is the fastest way of doing so in C#?

Thanks

ThunderMusic
 
R

r norman

Hi,
The subject says it all... I want to use a byte[] and use it as byte* so I
can increment the pointer to iterate through it.

What is the fastest way of doing so in C#?

I have recently started with C#, converting C++ applications that must
communicate with external equipment using protocols with rigidly fixed
byte sequences. Initially I was terribly frustrated abandoning my
precious pointer manipulation and access to byte arrays. But I
developed a bunch of utilities to create managed code byte[] arrays
from pieces and extract appropriate values from them and can now avoid
all unmanaged code. Look into Encoding.Convert for converting between
ASCII byte sequences and Unicode, MemoryStream.Write and ToArray along
with BinaryWriter.Write for creating byte[] data and the BitConverter
set of methods to extract values from byte[].

I keep telling myself that staying entirely within managed code is
worth all that effort, but I am still early in the process. Some old
timers here can probably fill you in on much more detail, including
the virtues of doing so.
 
N

Nicholas Paldino [.NET/C# MVP]

ThunderMusic,

If you are looking to tune this algoritm, I would say that the iteration
through the bytes is NOT the way to do it. There are probably a bunch of
other areas that can be improved upon.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ThunderMusic said:
hi,
thanks for the really quick answer... Your solution works, but I can't
do p++ because it's fixed. So I must use another pointer like byte* p2 =
p; so now I can do p2++;...

Actually, I'm looking for a faster way to compute a checksum on a byte
array... For now, I'm using the Adler-32 algorithm, but I'm open to
advises on a performant checksum algorithm. It will be for an error
checking mecanism for tcp and udp communication on a closed network
environment. So it doesn't need to be human-modification resistant, it's
just to prevent modification due to the noise on the line (if it can
happen)...

Thanks

ThunderMusic

Nicholas Paldino said:
ThunderMusic,

Use unsafe code:

byte[] bytes = ...;

unsafe
{
fixed (byte* p = bytes)
{
// Work with pointer here.
}
}

As a matter of fact, that's the only way to do it, as you need to pin
down the location of the array to prevent the reference from moving
around.

Is there a reason you need the pointer, or are you just looking for a
faster way to iterate through the array?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ThunderMusic said:
Hi,
The subject says it all... I want to use a byte[] and use it as byte* so
I can increment the pointer to iterate through it.

What is the fastest way of doing so in C#?

Thanks

ThunderMusic
 
U

UL-Tomten

on a performant checksum algorithm. It will be for an error checking
mecanism for tcp and udp communication on a closed network environment. So
it doesn't need to be human-modification resistant, it's just to prevent
modification due to the noise on the line (if it can happen)...

It can't happen.

Now, how's that for optimization?
 
T

ThunderMusic

hi,
I know it may be overoptimizing, but this part of the code will be called
many many many times per seconds, so I'm must make sure it's as optimized as
it can be. It's for a checksum mecanism, so each time a message must be
sent, it is called to compute the checksum to append to the message and will
be computed again when the client receives it so it can verify the validity
of the message.

Thanks for the comment tought...

ThunderMusic

Chris Mullins said:
ThunderMusic said:
The subject says it all... I want to use a byte[] and use it as byte* so
I can increment the pointer to iterate through it.

I really hate to be pedantic, but I'm willing to bet that the difference
in how you iterate through your array makes little to no difference in the
overall performance of your code.

People frequently are guilting of over-optimizing things that are already
"Fast Enough". Unless you've verified this section is slow via a Profiler,
you're better off not getting fancy with optimizations.
 
T

ThunderMusic

hi,
So, here is the code to get my checksum. If you see something that can be
optimized, let me know.

thanks

ThunderMusic

Code:
private const UInt16 MOD_ADLER = 65521;
private unsafe uint GetChecksum(byte[] databytes)
{
UInt32 a = 1, b = 0;
fixed (byte* tmpdata = databytes)
{
byte* data = tmpdata;
int len = databytes.Length; /* Length in bytes */
while (len > 0)
{
int tlen = len > 5550 ? 5550 : len;
len -= tlen;
do
{
a += *(data++);
b += a;
} while ((--tlen) > 0);
a = (a & 0xffff) + (a >> 16) * (65536 - MOD_ADLER);
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
}
/* It can be shown that a <= 0x1013a here, so a single subtract will
do. */
if (a >= MOD_ADLER)
a -= MOD_ADLER;
/* It can be shown that b can reach 0xffef1 here. */
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
if (b >= MOD_ADLER)
b -= MOD_ADLER;
}
return ((b << 16) | a);
}





Nicholas Paldino said:
ThunderMusic,

If you are looking to tune this algoritm, I would say that the
iteration through the bytes is NOT the way to do it. There are probably a
bunch of other areas that can be improved upon.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

ThunderMusic said:
hi,
thanks for the really quick answer... Your solution works, but I can't
do p++ because it's fixed. So I must use another pointer like byte* p2 =
p; so now I can do p2++;...

Actually, I'm looking for a faster way to compute a checksum on a byte
array... For now, I'm using the Adler-32 algorithm, but I'm open to
advises on a performant checksum algorithm. It will be for an error
checking mecanism for tcp and udp communication on a closed network
environment. So it doesn't need to be human-modification resistant, it's
just to prevent modification due to the noise on the line (if it can
happen)...

Thanks

ThunderMusic

Nicholas Paldino said:
ThunderMusic,

Use unsafe code:

byte[] bytes = ...;

unsafe
{
fixed (byte* p = bytes)
{
// Work with pointer here.
}
}

As a matter of fact, that's the only way to do it, as you need to pin
down the location of the array to prevent the reference from moving
around.

Is there a reason you need the pointer, or are you just looking for a
faster way to iterate through the array?


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi,
The subject says it all... I want to use a byte[] and use it as byte*
so I can increment the pointer to iterate through it.

What is the fastest way of doing so in C#?

Thanks

ThunderMusic
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

ThunderMusic said:
hi,
I know it may be overoptimizing, but this part of the code will be called
many many many times per seconds, so I'm must make sure it's as optimized
as it can be. It's for a checksum mecanism, so each time a message must be
sent, it is called to compute the checksum to append to the message and
will be computed again when the client receives it so it can verify the
validity of the message.

Thanks for the comment tought...

Why don't you do something, try using the "vanila" for statement versus the
pointer arithmetic you want to use, I will be surprised if you find any
noticeable difference.
 
C

Chris Mullins [MVP]

I understand well the "This code it called alot, therefore it needs to be
optimized." frame of mind. I build big, high-performance, network socket
servers for a living. I know this space really well, and have done lots in
both TCP & UDP land at scales that are pretty signfigant.

Put a profiler on this, and you'll likley find that this chunk of code won't
even show up. Certainly the difference between the two (pointer vs iterator)
is unlikley to show up at all.

--
Chris Mullins, MCSD.NET, MCPD:Enterprise, Microsoft C# MVP
http://www.coversant.com/blogs/cmullins

ThunderMusic said:
hi,
I know it may be overoptimizing, but this part of the code will be called
many many many times per seconds, so I'm must make sure it's as optimized
as it can be. It's for a checksum mecanism, so each time a message must be
sent, it is called to compute the checksum to append to the message and
will be computed again when the client receives it so it can verify the
validity of the message.

Thanks for the comment tought...

ThunderMusic

Chris Mullins said:
ThunderMusic said:
The subject says it all... I want to use a byte[] and use it as byte* so
I can increment the pointer to iterate through it.

I really hate to be pedantic, but I'm willing to bet that the difference
in how you iterate through your array makes little to no difference in
the overall performance of your code.

People frequently are guilting of over-optimizing things that are already
"Fast Enough". Unless you've verified this section is slow via a
Profiler, you're better off not getting fancy with optimizations.
 
W

Willy Denoyette [MVP]

ThunderMusic said:
hi,
So, here is the code to get my checksum. If you see something that can be
optimized, let me know.

thanks

ThunderMusic

Code:
private const UInt16 MOD_ADLER = 65521;
private unsafe uint GetChecksum(byte[] databytes)
{
UInt32 a = 1, b = 0;
fixed (byte* tmpdata = databytes)
{
byte* data = tmpdata;
int len = databytes.Length; /* Length in bytes */
while (len > 0)
{
int tlen = len > 5550 ? 5550 : len;
len -= tlen;
do
{
a += *(data++);
b += a;
} while ((--tlen) > 0);
a = (a & 0xffff) + (a >> 16) * (65536 - MOD_ADLER);
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
}
/* It can be shown that a <= 0x1013a here, so a single subtract
will do. */
if (a >= MOD_ADLER)
a -= MOD_ADLER;
/* It can be shown that b can reach 0xffef1 here. */
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
if (b >= MOD_ADLER)
b -= MOD_ADLER;
}
return ((b << 16) | a);
}

You can try to do some partial loop unrolling, this is what a C++ compiler
does and what the C# compiler/JIT unfortunately fails to do.

UInt32 a = 1, b = 0;
fixed (byte* tmpdata = databytes)
{
byte* data = tmpdata;
int len = databytes.Length; /* Length in bytes */
while(len > 0)
{
int tlen = len > 5550 ? 5550 : len;
len -= tlen;
for(int c = 0; c < tlen / 4; c++)
{
a += *(data++);
b += a;
a += *(data++);
b += a;
a += *(data++);
b += a;
a += *(data++);
b += a;
}
a = (a & 0xffff) + (a >> 16) * (65536 - MOD_ADLER);
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
for(int c = 0; c < tlen % 4; c++)
{
a += *(data++);
b += a;
}
a = (a & 0xffff) + (a >> 16) * (65536 - MOD_ADLER);
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
}
if (a >= MOD_ADLER)
a -= MOD_ADLER;
/* It can be shown that b can reach 0xffef1 here. */
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
if (b >= MOD_ADLER)
b -= MOD_ADLER;
}
return ((b << 16) | a);


Willy.
 
T

ThunderMusic

wow!! that's pretty impressive... this little modification made the loop 2
to 3 times faster!!

thanks a lot

ThunderMusic

Willy Denoyette said:
ThunderMusic said:
hi,
So, here is the code to get my checksum. If you see something that can be
optimized, let me know.

thanks

ThunderMusic

Code:
private const UInt16 MOD_ADLER = 65521;
private unsafe uint GetChecksum(byte[] databytes)
{
UInt32 a = 1, b = 0;
fixed (byte* tmpdata = databytes)
{
byte* data = tmpdata;
int len = databytes.Length; /* Length in bytes */
while (len > 0)
{
int tlen = len > 5550 ? 5550 : len;
len -= tlen;
do
{
a += *(data++);
b += a;
} while ((--tlen) > 0);
a = (a & 0xffff) + (a >> 16) * (65536 - MOD_ADLER);
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
}
/* It can be shown that a <= 0x1013a here, so a single subtract
will do. */
if (a >= MOD_ADLER)
a -= MOD_ADLER;
/* It can be shown that b can reach 0xffef1 here. */
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
if (b >= MOD_ADLER)
b -= MOD_ADLER;
}
return ((b << 16) | a);
}

You can try to do some partial loop unrolling, this is what a C++ compiler
does and what the C# compiler/JIT unfortunately fails to do.

UInt32 a = 1, b = 0;
fixed (byte* tmpdata = databytes)
{
byte* data = tmpdata;
int len = databytes.Length; /* Length in bytes */
while(len > 0)
{
int tlen = len > 5550 ? 5550 : len;
len -= tlen;
for(int c = 0; c < tlen / 4; c++)
{
a += *(data++);
b += a;
a += *(data++);
b += a;
a += *(data++);
b += a;
a += *(data++);
b += a;
}
a = (a & 0xffff) + (a >> 16) * (65536 - MOD_ADLER);
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
for(int c = 0; c < tlen % 4; c++)
{
a += *(data++);
b += a;
}
a = (a & 0xffff) + (a >> 16) * (65536 - MOD_ADLER);
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
}
if (a >= MOD_ADLER)
a -= MOD_ADLER;
/* It can be shown that b can reach 0xffef1 here. */
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
if (b >= MOD_ADLER)
b -= MOD_ADLER;
}
return ((b << 16) | a);


Willy.
 
W

Willy Denoyette [MVP]

ThunderMusic said:
wow!! that's pretty impressive... this little modification made the loop
2 to 3 times faster!!

2 to 3 times faster? It's hard to believe honestly, I would have expected
something like a 20-30% improvement. Are you sure about your time
measurement techniques?

Willy.
 
B

Ben Voigt [C++ MVP]

r norman said:
Hi,
The subject says it all... I want to use a byte[] and use it as byte* so I
can increment the pointer to iterate through it.

What is the fastest way of doing so in C#?

I have recently started with C#, converting C++ applications that must
communicate with external equipment using protocols with rigidly fixed
byte sequences. Initially I was terribly frustrated abandoning my
precious pointer manipulation and access to byte arrays. But I
developed a bunch of utilities to create managed code byte[] arrays
from pieces and extract appropriate values from them and can now avoid
all unmanaged code. Look into Encoding.Convert for converting between
ASCII byte sequences and Unicode, MemoryStream.Write and ToArray along
with BinaryWriter.Write for creating byte[] data and the BitConverter
set of methods to extract values from byte[].

I keep telling myself that staying entirely within managed code is
worth all that effort, but I am still early in the process. Some old
timers here can probably fill you in on much more detail, including
the virtues of doing so.

Staying in managed code may be worth the effort, converting to C# is not.
The C++ compiler still makes better code than the JIT can.
 
B

Ben Voigt [C++ MVP]

Willy Denoyette said:
ThunderMusic said:
hi,
So, here is the code to get my checksum. If you see something that can be
optimized, let me know.

thanks

ThunderMusic

Code:
private const UInt16 MOD_ADLER = 65521;
private unsafe uint GetChecksum(byte[] databytes)
{
UInt32 a = 1, b = 0;
fixed (byte* tmpdata = databytes)
{
byte* data = tmpdata;
int len = databytes.Length; /* Length in bytes */
while (len > 0)
{
int tlen = len > 5550 ? 5550 : len;
len -= tlen;
do
{
a += *(data++);
b += a;
} while ((--tlen) > 0);
a = (a & 0xffff) + (a >> 16) * (65536 - MOD_ADLER);
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
}
/* It can be shown that a <= 0x1013a here, so a single subtract
will do. */
if (a >= MOD_ADLER)
a -= MOD_ADLER;
/* It can be shown that b can reach 0xffef1 here. */
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
if (b >= MOD_ADLER)
b -= MOD_ADLER;
}
return ((b << 16) | a);
}

You can try to do some partial loop unrolling, this is what a C++ compiler
does and what the C# compiler/JIT unfortunately fails to do.

So, why not use .NET's C++ compiler (C++/CLI)? You have no
managed/unmanaged overhead, you get pointers without pinning (interior_ptr),
and you get the best optimizing compiler available for .NET

In this particular case, Duff's Device is ideal for loop unrolling without
overhead, and only C++ can do it.

This particular function is the poster child for C++/CLI if I've ever seen
one.

public ref class Checksum
{
literal unsigned MOD_ADLER = 65521;

public:
static unsigned Adler( array<System::Byte>^ databytes )
{
unsigned a = 1, b = 0;
interior_ptr<System::Byte> data = &databytes[0];
unsigned len = databytes->Length; /* Length in bytes */
while (len > 5550)
{
int iters = 555;
do {
a += *(data++);
b += a;
a += *(data++);
b += a;
a += *(data++);
b += a;
a += *(data++);
b += a;
a += *(data++);
b += a;
a += *(data++);
b += a;
a += *(data++);
b += a;
a += *(data++);
b += a;
a += *(data++);
b += a;
a += *(data++);
b += a;
} while (--iters);
a = (a & 0xffff) + (a >> 16) * (65536 - MOD_ADLER);
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
len -= 5550;
}

int iters = (len + 7) >> 3;
switch (len & 7)
{
do {
case 0:
a += *(data++);
b += a;
case 7:
a += *(data++);
b += a;
case 6:
a += *(data++);
b += a;
case 5:
a += *(data++);
b += a;
case 4:
a += *(data++);
b += a;
case 3:
a += *(data++);
b += a;
case 2:
a += *(data++);
b += a;
case 1:
a += *(data++);
b += a;
} while (--iters);
}
a = (a & 0xffff) + (a >> 16) * (65536 - MOD_ADLER);
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
/* It can be shown that a <= 0x1013a here, so a single subtract will
do. */
if (a >= MOD_ADLER)
a -= MOD_ADLER;
/* It can be shown that b can reach 0xffef1 here. */
b = (b & 0xffff) + (b >> 16) * (65536 - MOD_ADLER);
if (b >= MOD_ADLER)
b -= MOD_ADLER;
return ((b << 16) | a);
}
};
 
T

ThunderMusic

I use a StopWatch before (start) and after (stop) a loop of 100000 calls to
the function... It's not the most effective way, but it sure gives an idea
of the performance we can get...
 
T

ThunderMusic

Ben Voigt said:
r norman said:
Hi,
The subject says it all... I want to use a byte[] and use it as byte* so
I
can increment the pointer to iterate through it.

What is the fastest way of doing so in C#?

I have recently started with C#, converting C++ applications that must
communicate with external equipment using protocols with rigidly fixed
byte sequences. Initially I was terribly frustrated abandoning my
precious pointer manipulation and access to byte arrays. But I
developed a bunch of utilities to create managed code byte[] arrays
from pieces and extract appropriate values from them and can now avoid
all unmanaged code. Look into Encoding.Convert for converting between
ASCII byte sequences and Unicode, MemoryStream.Write and ToArray along
with BinaryWriter.Write for creating byte[] data and the BitConverter
set of methods to extract values from byte[].

I keep telling myself that staying entirely within managed code is
worth all that effort, but I am still early in the process. Some old
timers here can probably fill you in on much more detail, including
the virtues of doing so.

Staying in managed code may be worth the effort, converting to C# is not.
The C++ compiler still makes better code than the JIT can.

hi,
That's an idea... I'll look into it... probably that some methods should
be more suited to C++ because I'm seeking a bit of performance for key
features in my app.. I never think about managed C++, but it's sure to be a
good idea... I don't want to make a C++ library for only one method
tought...

Thanks a lot

ThunderMusic
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top