Pointers vs. Safecode

03/10/2011 20:58 Arco.#1
Really, what is the difference between doing:
Code:
                *(ushort*)PTR = (ushort)Buffer.Length;
                *((ushort*)(PTR + 2)) = 2030;
                *((uint*)(PTR + 4)) = npc.StaticID;
                *((ushort*)(PTR + 8)) = (ushort)npc.Cords.Position.X;
                *((ushort*)(PTR + 10)) = (ushort)npc.Cords.Position.Y;
                *((ushort*)(PTR + 12)) = (ushort)(npc.StaticID + (byte)npc.Direction);
                *((ushort*)(PTR + 14)) = npc.Interaction;
And

Code:
            PacketBuilder Packet = new PacketBuilder(new byte[20]);
            Packet.WriteUInt16(Packet.Packet.Length);
            Packet.WriteUInt16(2030);
            Packet.WriteUInt32((uint)Type);
            Packet.WriteUInt16(X);
            Packet.WriteUInt16(Y);
            Packet.WriteUInt16(SubType);
            Packet.WriteUInt16(Dir);
            Packet.WriteUInt32((uint)Flag);
Whats the pros and cons of each one?
03/10/2011 21:03 Syst3m_W1z4rd#2
The first one is writing the buffer direct and you can send it direct.
The second is writing to a packetbuilder, which you're sending to the client after.
You can use the 2nd one without writing it in an fixed block, because the fixed block is in the builder, where the first one have to be in a fixed block.

Don't know if I explained it right, but that's how I see it.
03/10/2011 21:14 Arco.#3
I know what they do, I want to know the pros v. cons of each.
03/10/2011 21:16 pro4never#4
Pointers are faster/more efficient.

Even more efficient is writing it using a structure (that way it's just stored in memory) and you can just build a packet based on the memory addresses.

Combine that with a static send buffer and you have yourself no need to ever assign new byte arrays for these new packets and completely skip all that overhead/memory usage.
03/11/2011 01:06 _tao4229_#5
doing something like *(ptr + x) = value directly translates to one assembly instruction (or at least less than using mathematical operators to imitate it).

also doing something like
struct packet {
short type;
short size;
}
p.type = 5;
p.size = 4;
is literally the same thing as *(ptr + offset) = ... at the most basic level. However it should be noted that in C# structs are stored on the stack (well struct s = new stuct()...), and allocating a byte[] is done on the heap.

Fixing a byte[] down into a byte* however isn't the fastest or best operation.
03/11/2011 03:36 Arco.#6
Quote:
Originally Posted by _tao4229_ View Post
doing something like *(ptr + x) = value directly translates to one assembly instruction (or at least less than using mathematical operators to imitate it).

also doing something like
struct packet {
short type;
short size;
}
p.type = 5;
p.size = 4;
is literally the same thing as *(ptr + offset) = ... at the most basic level. However it should be noted that in C# structs are stored on the stack (well struct s = new stuct()...), and allocating a byte[] is done on the heap.

Fixing a byte[] down into a byte* however isn't the fastest or best operation.
Then what do you think would be the best operation?
03/11/2011 03:49 _tao4229_#7
As with everything, it's situational.
03/12/2011 15:45 Basser#8
If you handle them propertly, pointers are much faster. On the other hand, if you don't handle them correctly, they can cause all kinds of problems.

I would avoid them, the CLR is pretty damn fast, you may find that you'll never have to resort to unsafe code.

Whether to use pointers or not is the least important thing to worry a bout.

I also think it depends on how much you're going to use pointers, only for packet structures? You really want to go into that much complexity for only packet structures?

Oh and let me add when I would use pointers.
- In image processing.
- When using pinvoke calls.
- If data processing is the bottleneck of your application.

Conclusion:
Don't use pointers in .NET, at this point, there is no need for it in a private server. You'll server is going to be a I/O bound application, not CPU bound.
03/14/2011 02:24 lostsolder05#9
Quote:
Originally Posted by Basser View Post
If you handle them propertly, pointers are much faster. On the other hand, if you don't handle them correctly, they can cause all kinds of problems.

I would avoid them, the CLR is pretty damn fast, you may find that you'll never have to resort to unsafe code.

Whether to use pointers or not is the least important thing to worry a bout.

I also think it depends on how much you're going to use pointers, only for packet structures? You really want to go into that much complexity for only packet structures?

Oh and let me add when I would use pointers.
- In image processing.
- When using pinvoke calls.
- If data processing is the bottleneck of your application.

Conclusion:
Don't use pointers in .NET, at this point, there is no need for it in a private server. You'll server is going to be a I/O bound application, not CPU bound.
It depends on what your using the pointers for. If it's a single pointer it's not going to make a significant difference. It'd advice staying away from pointers though considering you made this thread.