php-bencode is a PHP Bencode extension which supports encoding, decoding and editing Bencode strings or files. The previous version of php-bencode, which supports PHP 5, needs PHP-CPP to run while the new one now requires no other external libraries.

The location of the source code remains the same, on both GitHub and my personal TsundereGit:
https://github.com/Frederick888/php-bencode
https://git.tsundere.moe/Frederick888/php-bencode

Written in C/C++, php-bencode gives an unbelievable performance boost for manipulations of Bencode. I’ve run a benchmark through different implements by decoding a Bencode file with 1M sub-nodes in my box (8 cores, 1G memory). The sample file can be downloaded here: Bencode Sample.

Obviously, the time needed to decode the same file is shortened dramatically by using php-bencode. The memory usage is a little higher than the pure PHP library with PHP 7 but the object array can give you real data safety rather than a plain array. (For example, if the original Bencode file contains an empty dictionary or list, data loss may be caused after decoding and re-encoding.) What’s more, because the memory usage gap between php-bencode and other pure PHP libraries goes up with the number of nodes, as the Bencode files we usually need to handle contain only less than 1,000 nodes, the gap can be just ignored.

Still, there’s much work to do. The previous version supported to get/set a node by path and search through the whole tree but they have not been implemented in the new one yet. Additionally, the new php-bencode supports only PHP 7 by now and I’m considering to support PHP 5.6 as well.
続きを読む