Thrift::XS - Faster Thrift binary protocol encoding and decoding


    use Thrift;
    use Thrift::Socket;
    use Thrift::FramedTransport;
    use Thrift::XS::BinaryProtocol;
    use MyThriftInterface;
    my $socket    = Thrift::Socket->new( $host, $port );
    my $transport = Thrift::FramedTransport->new($socket);
    my $protocol  = Thrift::XS::BinaryProtocol->new($transport);
    my $client    = MyThriftInterface->new($protocol);
    $client->api_call( @args );


Thrift::XS provides faster versions of Thrift::BinaryProtocol and Thrift::MemoryBuffer.

Thrift compact protocol support is also available, just replace Thrift::XS::BinaryProtocol with Thrift::XS::CompactProtocol.

To use, simply replace your Thrift initialization code with the appropriate Thrift::XS version.


For the best performance, you need to use a custom socket layer and both Thrift::XS::MemoryBuffer and one of Thrift::XS::BinaryProtocol or Thrift::XS::CompactProtocol. If using the standard BufferedTransport, FramedTransport, or HttpClient modules, performance will not be as good as it could be. In particular, HttpClient is incredibly bad, making a lot of very small (1-4 byte) sysread() and print() calls. A future version of this module will probably provide XS implementations of these other modules to help with this problem.

Here is a breakdown of the performance improvements of the various low-level methods. A given Thrift API call will make many write and read method calls, so your results will be some average of these numbers. For detailed numbers and to run your own benchmarks, see the bench/ script.

    XS::MemoryBuffer write + read: 6x faster
        writeMessageBegin + readMessageBegin: 12.0x
        complex struct/field write+read:       6.6x
        writeMapBegin + readMapBegin:         24.0x
        writeListBegin + readListBegin:       20.0x
        writeSetBegin + readSetBegin:         21.0x
        writeBool + readBool:                 13.5x
        writeByte + readByte:                 13.9x
        writeI16 + readI16:                   14.4x
        writeI32 + readI32:                   12.9x
        writeI64 + readI64:                   29.4x
        writeDouble + readDouble:             13.5x
        writeString + readString:              7.5x
        writeMessageBegin + readMessageBegin: 11.6x
        complex struct/field write+read:       6.2x
        writeMapBegin + readMapBegin:         18.7x
        writeListBegin + readListBegin:       14.1x
        writeSetBegin + readSetBegin:         13.3x
        writeBool + readBool:                 13.2x
        writeByte + readByte:                 13.9x
        writeI16 + readI16:                    9.0x
        writeI32 + readI32:                    7.5x
        writeI64 + readI64:                   10.0x
        writeDouble + readDouble:             13.5x
        writeString + readString:              7.4x


Wang Lam, <>, for patches and additional tests.


Thrift Home

Thrift Perl code

AnyEvent::Cassandra, example usage of this module. This module is not yet on CPAN, but will be available soon.


Andy Grundman, <>


Copyright 2011 Andy Grundman

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.