Quote:
Originally Posted by Y u k i
PHP Code:
private static byte[] ReadPassword(byte[] raw) { byte[] Pass = new byte[16]; for (int x = 0x14, y = 0; x < 0x24; ++x, ++y) { Pass[y] = raw[x]; y++; } return Pass; }
|
You can have both variables in the for-loop (not an optimization).
Post-incrementation will do a copy while pre-incrementation won't.
There is no point to do more optimization than that for 16 bytes, and for something that happens 1/1000th of time in the server.
Array.Copy / Buffer.Copy might be more optimized than your for-loop, but I doubt it will change anything.
In C++, if you would not matter accessing the raw data directly, I would just do « uint8_t* ptr = &raw[0x14] » and use it directly. No copy.
Quote:
Originally Posted by Super Aids
[...]
Here is an actual optimization:
Code:
[DllImport("msvcrt.dll", EntryPoint = "memcpy", CallingConvention = CallingConvention.Cdecl, SetLastError = false)]
private static extern unsafe void* memcpy(void* dest, void* src, uint size);
public static byte[] ReadPassord(byte[] raw)
{
byte[] Pass = new byte[16];
unsafe
{
fixed (byte* src = raw, dest = Pass)
{
memcpy(dest, ((void*)(src + 0x14)), 16);
}
}
return Pass;
}
|
Not sure that adding the overhead of the native call and marshalling is really faster for 16 bytes...
Btw. More optimized memcpy than byte per byte in C#.
Code:
/// <summary>
/// Copies the values of num bytes from the location pointed by source directly to the memory block
/// pointed by destination.
///
/// The underlying type of the objects pointed by both the source and destination pointers are irrelevant
/// for this function; The result is a binary copy of the data.
/// </summary>
/// <param name="dest">Pointer to the destination array where the content is to be copied.</param>
/// <param name="src">Pointer to the source of data to be copied.</param>
/// <param name="num">Number of bytes to copy.</param>
/// <returns>The destination is returned.</returns>
public static void* memcpy(void* dest, void* src, size_t num)
{
//The use of Int32 on both x86 and x64 is the best solution to get the best speed.
//Probably due to the aligment of the data.
Int32 amount = num / sizeof(size_t);
for (Int32 i = 0; i < amount; ++i)
((size_t*)dest)[i] = ((size_t*)src)[i];
amount = num % sizeof(size_t);
Int32 pos = num - amount;
for (Int32 i = 0; i < amount; ++i)
(((Byte*)dest) + pos)[i] = (((Byte*)src) + pos)[i];
return dest;
}