提升NexusPHP解析種子文件性能(優化benc.php)

https://blog.rhilip.info/archives/1064/

根據文章中的評測,NexusPHP(benc.php)的種子解析性能非常低,即Bencode解碼能力很低,(編碼能力與其他庫文件性能差不多),因此這裏對種子文件解碼部分代碼進行修改,並與NexusPHP做到最大兼容.

https://raw.githubusercontent.com/Rhilip/RidPT/master/framework/Bencode/Bencode.php

使用這個編碼庫,並進行一定的代碼修改
代碼如下:

/**
 *
 * Rewrite from : https://github.com/OPSnet/bencode-torrent/blob/master/src/Bencode.php
 *
 * Created by PhpStorm.
 * User: Rhilip
 * Date: 2019/2/2
 * Time: 20:00
 * 
 * Edit by PhpStorm.
 * Edit User: chenzhuyu
 * Edit Date: 2019/08/26
 * Edit Time: 09:00
 */
 
function bdec_file($path)
{
    try{
        return bdec_decode_new(file_get_contents($path, FILE_BINARY));
    }
    catch (Exception $e){
        return;
    }
   
}
function bdec_decode_new($data, &$pos = 0)
{
    $start_decode = ($pos === 0);
    if ($data[$pos] === 'd') {
        $pos++;
        $return = [];
        while ($data[$pos] !== 'e') {
             
            $key = bdec_decode_new($data, $pos)['value'];
            $value = bdec_decode_new($data, $pos);
            if ($key === null || $value === null) {
                break;
            }
            if (!is_string($key)) {


                throw new Exception('Invalid key type, must be string: ' . gettype($key));
            }
            $return[$key] = $value;

            
        }
        ksort($return);
        $return = array('type' => "dictionary", 'value' => $return,'strlen' => 0, 'string' => 0);
        
        $pos++;
    } elseif ($data[$pos] === 'l') {
        $pos++;
        $return = [];
        while ($data[$pos] !== 'e') {
            $value = bdec_decode_new($data, $pos);

                $return[]=$value;
        }
        $return = array('type' => "list", 'value' => $return,'strlen' => 0, 'string' => 0);
        $pos++;
    } elseif ($data[$pos] === 'i') {
        $pos++;
        $digits = strpos($data, 'e', $pos) - $pos;
        $return = substr($data, $pos, $digits);
        if ($return === '-0') {


            throw new Exception('Cannot have integer value -0');
        }
        $multiplier = 1;
        if ($return[0] === '-') {
            $multiplier = -1;
            $return = substr($return, 1);
        }
        if (!ctype_digit($return)) {


            throw new Exception('Cannot have non-digit values in integer number: ' . $return);
        }
        $return = $multiplier * ((int)$return);
        $pos += $digits + 1;
        $return = array('type' => "integer", 'value' => $return,'strlen' => 0, 'string' => 0);
    } else {
        $digits = strpos($data, ':', $pos) - $pos;
        $len = (int)substr($data, $pos, $digits);
        $pos += ($digits + 1);
        $return = substr($data, $pos, $len);
        $pos += $len;
        $return = array('type' => "string", 'value' => $return,'strlen' => 0, 'string' => 0);
    }
    if ($start_decode) {
        if ($pos !== strlen($data)) {
            throw new Exception('Could not fully decode bencode string');
        }
    }
    return $return;
}

將上述代碼追加到benc.php文件中,並覆蓋函數bdec_file,

takeupload.php中的

//屏蔽以提高性能
//$dict=bdec(benc($dict));

//$infohash = pack("H*", sha1($info["string"]));
//修改爲
$infohash = pack("H*", sha1(benc($info)));
//螞蟻PT的torrenthashcheck.php也進行相應的修改
//雖然可能都不知道這個是做什麼的

目前主要是種子文件解析會降低性能,因此只對bdec_file函數進行修改
其他功能,如字符串解碼,數據編碼等對性能影響很小,因此這裏不做修改

Xdebug插件也要關閉
opcache,wincache之類的加速器要確保開啓
gzip也用上

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章