# Performance Tuning Base64 encoding

I've been trying to get a relatively optimized version of base64 encoding that works against files. However, I've made several attempts and haven't gotten anything much faster than what I have here. On the posted implementation, I started from what was posted on http://www.swissdelphicenter.ch/torry/showcode.php?id=1524 . I have to think there's a way to do it without tossing values around in a, so I tried that by processing three bytes at a time and then processing the rest, but I didn't end up with anything faster. So, does anyone have any ideas on how to improve it any more from a speed perspective?

(Relevant code below, I slid some global definitions into the first procedure so people could see those. Codes64 is the string I'm using to encode the data with, not reproduced here for brevity)

procedure TMyMimeEncoder.Enc64Buffer(inbuf, outbuf: pointer; inbufsize:  longint;
var outbufsize: longint);
// run on the buffer read in by the file (inbuf).  Outbuf is the output.
type
PByteArray1 = ^TByteArray1;
TByteArray1 = array[1..buffersize] of Byte;
var
a, b: longint;
inproc: PByteArray1 absolute inbuf;
outproc: PChar;
i: longint;
begin
outproc := outbuf;
outbufsize := 0;
for i := 1 to inbufsize do
begin
b := (b shl 8) + inproc^[i];
a := a + 8;
if a = 12 then
begin
a := a - 6;
outproc^ := Codes64[(b shr a)];
b := b and ((1 shl a) - 1);
inc(outproc, 1);
inc(outbufsize, 1);
end;
a := a - 6;
outproc^ := Codes64[(b shr a)];
b := b and ((1 shl a) - 1);
inc(outproc, 1);
inc(outbufsize, 1);
end;
end;

function TMyMimeEncoder.Enc64Final: String;
// run at the end of file to finish the encoding.
begin
Result := '';
if a = 4 then
Result := Codes64[(b shl 2) + 1] + '='
else
if a = 2 then
Result := Codes64[(b shl 4) + 1] + '==';
end;

-
I have a different version of this now with much smaller numbers of instructions. I figured out the main issue is with the output buffer assigns (my guess about 20% of the total run when I comment those lines out). If anyone has any ideas in that realm, I'd appreciate them. –  Glenn1234 Jan 2 '13 at 0:16