Zanmimljivo kako radi frameork substring operaciju ;)
Code (csharp):
private unsafe string InternalSubString(int startIndex, int length, bool fAlwaysCopy)
{
if (((startIndex == 0) && (length == this.Length)) && !fAlwaysCopy)
{
return this;
}
string str = FastAllocateString(length);
fixed (char* chRef = &str.m_firstChar)
{
fixed (char* chRef2 = &this.m_firstChar)
{
wstrcpy(chRef, chRef2 + startIndex, length);
}
}
return str;
}
private static unsafe void wstrcpy(char* dmem, char* smem, int charCount)
{
if (charCount > 0)
{
if ((((int) dmem) & 2) != 0)
{
dmem[0] = smem[0];
dmem++;
smem++;
charCount--;
}
while (charCount >= 8)
{
*((int*) dmem) = *((uint*) smem);
*((int*) (dmem + 2)) = *((uint*) (smem + 2));
*((int*) (dmem + 4)) = *((uint*) (smem + 4));
*((int*) (dmem + 6)) = *((uint*) (smem + 6));
dmem += 8;
smem += 8;
charCount -= 8;
}
if ((charCount & 4) != 0)
{
*((int*) dmem) = *((uint*) smem);
*((int*) (dmem + 2)) = *((uint*) (smem + 2));
dmem += 4;
smem += 4;
}
if ((charCount & 2) != 0)
{
*((int*) dmem) = *((uint*) smem);
dmem += 2;
smem += 2;
}
if ((charCount & 1) != 0)
{
dmem[0] = smem[0];
}
}
}
Izgleda rogobatno ali JIT odlicno optimizuje ove operacije u 64bit/32bit/16bit move i jednu 8 bt operaiju po potrebi. Mada mi nije bas najjasnije zasto jednostavno nisu iskoristi platform dependant internal memcpy implementaciju?