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.
php-bencode (decoding to objective array) start end usage duration 1463911318.9625 1463911319.2908 235060168 0.3283 1463911319.3859 1463911319.6992 235060168 0.3133 1463911319.7819 1463911320.1416 235060168 0.3598 1463911320.2392 1463911320.5557 235060168 0.3165 1463911320.6376 1463911320.9569 235060168 0.3194 1463911321.0616 1463911321.3757 235060168 0.3142 1463911321.4570 1463911321.7702 235060168 0.3132 1463911321.8663 1463911322.2208 235060168 0.3545 1463911322.3028 1463911322.6624 235060168 0.3595 Pure PHP Library (PHP 7.0.1, decoding to plain array) start end usage duration 1463911300.8234 1463911301.4810 191506800 0.6576 1463911301.4991 1463911302.1591 191506800 0.6600 1463911302.1837 1463911302.7704 191506800 0.5866 1463911302.7883 1463911303.4514 191506800 0.6631 1463911303.4758 1463911304.1296 191506800 0.6538 1463911304.1479 1463911304.7430 191506800 0.5951 1463911304.7675 1463911305.3909 191506800 0.6235 1463911305.4093 1463911306.0353 191506800 0.6259 1463911306.0667 1463911306.6656 191506800 0.5990 1463911306.6834 1463911307.3495 191506800 0.6661 Pure PHP Library (PHP 5.6.16, decoding to plain array) start end usage duration 1463911241.9759 1463911244.9670 255799264 2.9912 1463911245.1560 1463911247.8958 255705392 2.7398 1463911248.0932 1463911250.8363 255776296 2.7430 1463911251.0001 1463911253.8623 255738160 2.8623 1463911254.0313 1463911256.8981 255724336 2.8668 1463911257.0905 1463911259.8378 255707600 2.7473 1463911260.0006 1463911262.8360 255766880 2.8354 1463911262.9939 1463911265.8042 255790648 2.8103 1463911265.9561 1463911268.6946 255756520 2.7385 1463911268.8425 1463911271.7020 255781160 2.8595
<?php printf("start\tend\tusage\tduration\n"); for ($i = 0; $i < 20; $i++) { $time1 = microtime(true); if ($i >= 11) printf("%.4f\t", $time1); $broot = bitem::load('performance.dat'); $time2 = microtime(true); if ($i >= 11) printf("%.4f\t%d\t%.4f\n", $time2, memory_get_usage(), $time2 - $time1); unset($broot); }
<?php require 'torrent-bencode/src/Bhutanio/BEncode/BEncode.php'; printf("start\tend\tusage\tduration\n"); for ($i = 0; $i < 20; $i++) { $bcoder = new Bhutanio\BEncode\BEncode; $time1 = microtime(true); if ($i >= 10) printf("%.4f\t", $time1); $broot = $bcoder->bdecode_file('performance.dat'); $time2 = microtime(true); if ($i >= 10) printf("%.4f\t%d\t%.4f\n", $time2, memory_get_usage(), $time2 - $time1); unset($broot); unset($bcoder); }