DAMN LOOK AT THE SPEED METER

This commit is contained in:
Daniil Gentili 2017-07-27 16:59:46 +02:00
parent d4975caa21
commit 5e9df575da
6 changed files with 77 additions and 85 deletions

View File

@ -44,7 +44,7 @@ class MTProto extends \Volatile
use \danog\MadelineProto\Wrappers\DialogHandler; use \danog\MadelineProto\Wrappers\DialogHandler;
use \danog\MadelineProto\Wrappers\Login; use \danog\MadelineProto\Wrappers\Login;
const V = 64; const V = 65;
const NOT_LOGGED_IN = 0; const NOT_LOGGED_IN = 0;
const WAITING_CODE = 1; const WAITING_CODE = 1;

View File

@ -27,7 +27,7 @@ trait PrettyException
foreach (array_reverse($this->getTrace()) as $k => $frame) { foreach (array_reverse($this->getTrace()) as $k => $frame) {
if (isset($frame['function']) && in_array($frame['function'], ['serialize_params', 'serialize_object'])) { if (isset($frame['function']) && in_array($frame['function'], ['serialize_params', 'serialize_object'])) {
if ($frame['args'][2] !== '') { if ($frame['args'][2] !== '') {
$this->tl_trace .= $tl ? "['".$frame['args'][2]."']" : "While serializing: \t".$frame['args'][2]; $this->tl_trace .= $tl ? "['".$frame['args'][2]."']" : "While serializing: \t".$frame['args'][2];
$tl = true; $tl = true;
} }
} else { } else {

View File

@ -338,7 +338,6 @@ trait TL
} }
$auto = false; $auto = false;
if ((!$this->is_array($object) || (isset($object['_']) && $this->constructors->find_by_predicate($object['_'])['type'] !== $type['type'])) && $this->in_array($type['type'], ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputPeer'])) { if ((!$this->is_array($object) || (isset($object['_']) && $this->constructors->find_by_predicate($object['_'])['type'] !== $type['type'])) && $this->in_array($type['type'], ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputPeer'])) {
$object = $this->get_info($object); $object = $this->get_info($object);
if (!isset($object[$type['type']])) { if (!isset($object[$type['type']])) {
@ -348,6 +347,7 @@ trait TL
} }
if (!isset($object['_'])) { if (!isset($object['_'])) {
$constructorData = $this->constructors->find_by_predicate($type['type'], $layer); $constructorData = $this->constructors->find_by_predicate($type['type'], $layer);
if ($constructorData === false) { if ($constructorData === false) {
throw new Exception('Predicate was not set!'); throw new Exception('Predicate was not set!');
} }

View File

@ -18,78 +18,75 @@ class TLConstructor extends \Volatile
use \danog\MadelineProto\Tools; use \danog\MadelineProto\Tools;
use TLParams; use TLParams;
public $id = []; public $by_id = [];
public $predicate = []; public $by_predicate_and_layer = [];
public $type = []; public $layers = [];
public $params = []; //public $type = [];
public $layer = []; //public $params = [];
public $key = 0; //public $layer = [];
//public $key = 0;
public function __sleep() public function __sleep()
{ {
return ['id', 'predicate', 'type', 'params', 'layer', 'key']; return ['by_predicate_and_layer', 'by_id', 'layers'];
} }
public function add($json_dict, $scheme_type) public function add($json_dict, $scheme_type)
{ {
$this->id[$this->key] = $json_dict['id']; $predicate = (string) ((($scheme_type === 'mtproto' && $json_dict['predicate'] === 'message') ? 'MT' : '').$json_dict['predicate']);
$this->predicate[$this->key] = (string) ((($scheme_type === 'mtproto' && $json_dict['predicate'] === 'message') ? 'MT' : '').$json_dict['predicate']); $this->by_id[$json_dict['id']] = ['predicate' => $predicate, 'params' => $json_dict['params'], 'type' => (($scheme_type === 'mtproto' && $json_dict['type'] === 'Message') ? 'MT' : '').$json_dict['type']];
$this->type[$this->key] = (($scheme_type === 'mtproto' && $json_dict['type'] === 'Message') ? 'MT' : '').$json_dict['type'];
$this->params[$this->key] = $json_dict['params'];
if ($scheme_type === 'secret') { if ($scheme_type === 'secret') {
$this->layer[$this->key] = $json_dict['layer']; $this->by_id[$json_dict['id']]['layer'] = $json_dict['layer'];
} $this->layers[$json_dict['layer']] = $json_dict['layer'];
$this->parse_params($this->key, $scheme_type === 'mtproto'); ksort($this->layers);
$this->key++; } else $json_dict['layer'] = '';
$this->by_predicate_and_layer[$json_dict['predicate'].$json_dict['layer']] = $json_dict['id'];
$this->parse_params($json_dict['id'], $scheme_type === 'mtproto');
} }
public function find_by_type($type) public function find_by_type($type)
{ {
$key = array_search($type, (array) $this->type, true); foreach ($this->by_id as $id => $constructor) {
if ($constructor['type'] === $type) {
return ($key === false) ? false : [ $constructor['id'] = $id;
'id' => $this->id[$key], $constructor['params'] = $this->array_cast_recursive($constructor['params']);
'predicate' => $this->predicate[$key], return $constructor;
'type' => $this->type[$key], }
'params' => $this->array_cast_recursive($this->params[$key]), }
]; return false;
} }
public function find_by_predicate($predicate, $layer = -1) public function find_by_predicate($predicate, $layer = -1)
{ {
if ($layer !== -1) { if ($layer !== -1) {
$newlayer = -1; foreach ($this->layers as $alayer) {
$keys = array_keys((array) $this->predicate, $predicate); if ($alayer <= $layer && isset($this->by_predicate_and_layer[$predicate.$alayer])) {
foreach ($keys as $k) { $chosenid = $this->by_predicate_and_layer[$predicate.$alayer];
if ($this->layer[$k] <= $layer && $this->layer[$k] > $newlayer) {
$key = $k;
$newlayer = $this->layer[$k];
}
if (!isset($key)) {
$key = $keys[0];
} }
} }
} else { if (!isset($chosenid)) return false;
$key = array_search($predicate, (array) $this->predicate, true); $constructor = $this->by_id[$chosenid];
$constructor['id'] = $chosenid;
$constructor['params'] = $this->array_cast_recursive($constructor['params']);
return $constructor;
} }
if (isset($this->by_predicate_and_layer[$predicate])) {
return ($key === false) ? false : [ $constructor = $this->by_id[$this->by_predicate_and_layer[$predicate]];
'id' => $this->id[$key], $constructor['id'] = $this->by_predicate_and_layer[$predicate];
'predicate' => $this->predicate[$key], $constructor['params'] = $this->array_cast_recursive($constructor['params']);
'type' => $this->type[$key], return $constructor;
'params' => $this->array_cast_recursive($this->params[$key]), }
]; return false;
} }
public function find_by_id($id) public function find_by_id($id)
{ {
$key = array_search($id, (array) $this->id, true); if (isset($this->by_id[$id])) {
$constructor = $this->by_id[$id];
return ($key === false) ? false : [ $constructor['id'] = $id;
'id' => $this->id[$key], $constructor['params'] = $this->array_cast_recursive($constructor['params']);
'predicate' => $this->predicate[$key], return $constructor;
'type' => $this->type[$key], }
'params' => $this->array_cast_recursive($this->params[$key]), return false;
];
} }
} }

View File

@ -17,54 +17,49 @@ class TLMethod extends \Volatile
use \danog\Serializable; use \danog\Serializable;
use \danog\MadelineProto\Tools; use \danog\MadelineProto\Tools;
use TLParams; use TLParams;
public $id = []; public $by_id = [];
public $method = []; public $by_method = [];
public $type = [];
public $params = [];
public $method_namespace = []; public $method_namespace = [];
public $key = 0;
public function __sleep() public function __sleep()
{ {
return ['id', 'method', 'type', 'params', 'method_namespace', 'key']; return ['by_id', 'by_method', 'method_namespace'];
} }
public function add($json_dict) public function add($json_dict)
{ {
$this->id[$this->key] = $json_dict['id']; $this->by_id[$json_dict['id']] = ['method' => $json_dict['method'], 'type' => $json_dict['type'], 'params' => $json_dict['params']];
$this->method[$this->key] = $json_dict['method']; $this->by_method[$json_dict['method']] = $json_dict['id'];
$this->type[$this->key] = $json_dict['type'];
$this->params[$this->key] = $json_dict['params'];
$namespace = explode('.', $json_dict['method']); $namespace = explode('.', $json_dict['method']);
if (isset($namespace[1])) { if (isset($namespace[1])) {
$this->method_namespace[$namespace[1]] = $namespace[0]; $this->method_namespace[$namespace[1]] = $namespace[0];
} }
$this->parse_params($this->key); $this->parse_params($json_dict['id']);
$this->key++;
} }
public function find_by_method($method)
{
$key = array_search($method, (array) $this->method, true);
return ($key === false) ? false : [
'id' => $this->id[$key],
'method' => $this->method[$key],
'type' => $this->type[$key],
'params' => $this->array_cast_recursive($this->params[$key]),
];
}
public function find_by_id($id) public function find_by_id($id)
{ {
$key = array_search($id, (array) $this->id, true); if (isset($this->by_id[$id])) {
$method = $this->by_id[$id];
return ($key === false) ? false : [ $method['id'] = $id;
'id' => $this->id[$key], $method['params'] = $this->array_cast_recursive($method['params']);
'method' => $this->method[$key], return $method;
'type' => $this->type[$key], }
'params' => $this->array_cast_recursive($this->params[$key]), return false;
];
} }
public function find_by_method($method_name)
{
if (isset($this->by_method[$method_name])) {
$method = $this->by_id[$this->by_method[$method_name]];
$method['id'] = $this->by_method[$method_name];
$method['params'] = $this->array_cast_recursive($method['params']);
return $method;
}
return false;
}
} }

View File

@ -16,7 +16,7 @@ trait TLParams
{ {
public function parse_params($key, $mtproto = false) public function parse_params($key, $mtproto = false)
{ {
foreach ($this->params[$key] as $kkey => $param) { foreach ($this->by_id[$key]['params'] as $kkey => $param) {
if (preg_match('/^flags\.\d*\?/', $param['type'])) { if (preg_match('/^flags\.\d*\?/', $param['type'])) {
$flag = explode('?', explode('flags.', $param['type'])[1]); $flag = explode('?', explode('flags.', $param['type'])[1]);
$param['pow'] = pow(2, $flag[0]); $param['pow'] = pow(2, $flag[0]);
@ -36,7 +36,7 @@ trait TLParams
} }
$param['type'] = (($mtproto && $param['type'] === 'Message') ? 'MT' : '').$param['type']; $param['type'] = (($mtproto && $param['type'] === 'Message') ? 'MT' : '').$param['type'];
$param['type'] = (($mtproto && $param['type'] === '%Message') ? '%MTMessage' : $param['type']); $param['type'] = (($mtproto && $param['type'] === '%Message') ? '%MTMessage' : $param['type']);
$this->params[$key][$kkey] = $param; $this->by_id[$key]['params'][$kkey] = $param;
} }
} }
} }