While bugfixing the doc compress functions in tejpWriter, I got a big surprise. By substituting a pure C function, memfind2, with two Palm OS API functions: MemMove and StrStr, I not only saved code, but also made the doc compression run 14 times faster! Well, I knew it would run a lot faster but 14 times was more than I had expected, specially since the substituted code only is a part of the whole compression routine. Compressing a file which before took 14 minutes on my T5 now takes only 1 minute.
I substituted this function call:
pHit = memfind2(pPrevHit, pTestHead, pTestTail – pTestHead);
unsigned char *memfind2(unsigned char *t, unsigned char *m, Int16 m_len)
{
unsigned char c0, c1, c2, c3;
if (m_len == 1) {
c0 = m[0];
while (true) {
if (t[0] ==c0) {
return t;
}
t++;
}
} else if (m_len == 2) {
c0 = m[0];
c1 = m[1];
while (true) {
if (t[0] == c0 && t[1] == c1) {
return t;
}
t++;
}
} else if (m_len == 3) {
c0 = m[0];
c1 = m[1];
c2 = m[2];
while (true) {
if (t[0] == c0 && t[1] == c1 && t[2] == c2) {
return t;
}
t++;
}
} else if (m_len == 4) {
c0 = m[0];
c1 = m[1];
c2 = m[2];
c3 = m[3];
while (true) {
if (t[0] == c0 && t[1] == c1 && t[2] == c2 && t[3] == c3) {
return t;
}
t++;
}
} else {
c0 = m[0];
c1 = m[1];
c2 = m[2];
c3 = m[3];
while (true) {
if (t[0] == c0 && t[1] == c1 && t[2] == c2 && t[3] == c3) {
if (MemCmp(t, m, m_len) == 0) {
return t;
}
}
t++;
}
}
return 0;
}
with these functions from the Palm OS API:
MemMove(Buffer, pTestHead, pTestTail – pTestHead);
Buffer[pTestTail - pTestHead]=0;
pHit = StrStr(pPrevHit, Buffer);
Tam already wrote a great piece about this here, but used a tech jargon which could be comparable with ARM code, while I prefer an older an simpler language more in line with the old 68k assembler code. As the amateur I am, I ran Tam’s ARM code in my emulator to see if I could understand it, and this is what came out:
When you compile C-language for Palm you normally get old Motorola 68k assembler code. But newer Palm CPUs doesn’t speak that language—they speak ARM. This means the old 68k assembler code has to run in an emulator to transfer its instructions to ARM code which the CPU needs. And as you know, emulators are sloooow.
But the Palm OS API calls you make from your old 68k assembler code talks ARM straight with the CPU! So, if you want fast apps and can choose, use Palm OS API functions instead of pure C—language functions.