NexusPHP解析シードファイルのパフォーマンスの向上(benc.phpの最適化)

23842 ワード

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をオンにしても使用できます.