title: Why are the Delphi zlib and zip libraries so slow under 64 bit?
body: <p>Whilst benchmarking a real-world application I came across a surprising performance characteristic relating to the zlib and zip libraries that ship with Delphi. </p>
  
  <p>My real-world application exports .xlsx files. This file format is a collection of XML files wrapped in a ZIP container file. The .xlsx export code generates the XML files and then feeds them to the Delphi ZIP library. Once I had optimised the XML file generation to the point where the ZIP creation was the bottleneck I discovered, to my surprise, that 64 bit code was significantly slower than 32 bit code.</p>
  
  <p>In order to study this further I created this test program:</p>
  
  <pre><code>program zlib_perf;
  
  {$APPTYPE CONSOLE}
  
  uses
    System.SysUtils, System.Classes, System.Diagnostics, System.Zip;
  
  const
    LoremIpsum =
      'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod '+
      'tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, '+
      'quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo '+
      'consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse '+
      'cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat '+
      'non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
  
  function GetTestStream: TStream;
  var
    Bytes: TBytes;
  begin
    Result := TMemoryStream.Create;
    // fill the stream with 500MB of lorem ipsum
    Bytes := TEncoding.UTF8.GetBytes(LoremIpsum);
    while Result.Size &lt; 500*1024*1024 do
      Result.WriteBuffer(Pointer(Bytes)^, Length(Bytes));
  end;
  
  procedure DoTest;
  var
    DataStream, ZipStream: TStream;
    Stopwatch: TStopwatch;
    Zip: TZipFile;
  begin
    DataStream := GetTestStream;
    try
      ZipStream := TMemoryStream.Create;
      try
        Zip := TZipFile.Create;
        try
          Zip.Open(ZipStream, zmWrite);
  
          Stopwatch := TStopwatch.StartNew;
          DataStream.Position := 0;
          Zip.Add(DataStream, 'foo');
          Writeln(Stopwatch.ElapsedMilliseconds);
        finally
          Zip.Free;
        end;
      finally
        ZipStream.Free;
      end;
    finally
      DataStream.Free;
    end;
  end;
  
  begin
    DoTest;
  end.
  </code></pre>
  
  <p>I compiled the program under both XE2 and XE7, for both 32 and 64 bit, and with default release configuration compiler options. My test machine runs Windows 7 x64 on an Intel Xeon E5530.</p>
  
  <p>Here are the results:</p>
  
  <pre>
  Compiler  Target  Time (ms)
       XE2   Win32       8586
       XE2   Win64      18908
       XE7   Win32       8583
       XE7   Win64      19304
  </pre>
  
  <p>I compressed the same file using the Explorer shell ZIP functionality and my rough stop watch timing was 8 seconds so the 32 bit times above seem reasonable.</p>
  
  <p>Since the compression algorithm used by the above code is zlib (Delphi's ZIP code supports only store and deflate), my belief is that the zlib library used by Delphi is at the root of this issue. Why is Delphi's zlib library so slow under 64 bit?</p>