Skip to content

Commit

Permalink
Performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Jun 28, 2023
1 parent 2413110 commit 33f8ade
Show file tree
Hide file tree
Showing 44 changed files with 540 additions and 182 deletions.
8 changes: 4 additions & 4 deletions src/AnnotationsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ private function filterParams(array $params, string $type, ?string $method = nul
{
$newParams = [];
foreach ($params as $param) {
if (\in_array($param['name'], ['flags', 'flags2', 'random_id', 'random_bytes'])) {
if (\in_array($param['name'], ['flags', 'flags2', 'random_id', 'random_bytes'], true)) {
continue;
}
if ($method) {
Expand All @@ -263,7 +263,7 @@ private function filterParams(array $params, string $type, ?string $method = nul
$param['name'] = 'decrypted_message';
$param['type'] = 'DecryptedMessage';
}
if ($type === 'DecryptedMessageMedia' && \in_array($param['name'], ['key', 'iv'])) {
if ($type === 'DecryptedMessageMedia' && \in_array($param['name'], ['key', 'iv'], true)) {
continue;
}
if ($param['name'] === 'chat_id' && $method !== 'messages.discardEncryption') {
Expand All @@ -274,7 +274,7 @@ private function filterParams(array $params, string $type, ?string $method = nul
$param['type'] = 'Vector t';
$param['subtype'] = 'int';
}
if (\in_array($param['type'], ['int', 'long', 'string', 'bytes'])) {
if (\in_array($param['type'], ['int', 'long', 'string', 'bytes'], true)) {
$param['pow'] = 'optional';
}
$param['array'] = isset($param['subtype']);
Expand Down Expand Up @@ -331,7 +331,7 @@ private function createInternalClasses(): void
continue;
}
[$namespace, $method] = \explode('.', $data['method']);
if (!\in_array($namespace, $this->TL->getMethodNamespaces())) {
if (!\in_array($namespace, $this->TL->getMethodNamespaces(), true)) {
continue;
}
if (isset($this->blacklist[$data['method']])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Broadcast/Progress.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function jsonSerialize(): mixed
{
return \get_object_vars($this);
}
public function __toString()
public function __toString(): string
{
return "Progress for {$this->broadcastId}: {$this->percent}%, status {$this->status->value}, sent to {$this->successCount} peers, failed sending to {$this->failCount} peers, {$this->pendingCount} peers left.";
}
Expand Down
8 changes: 4 additions & 4 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ public function getCtx(): ConnectionContext
*/
public function isHttp(): bool
{
return \in_array($this->ctx->getStreamName(), [HttpStream::class, HttpsStream::class]);
return \in_array($this->ctx->getStreamName(), [HttpStream::class, HttpsStream::class], true);
}
/**
* Check if is a media connection.
Expand Down Expand Up @@ -320,7 +320,7 @@ private function methodAbstractions(string &$method, array &$arguments): ?Deferr
} else {
$arguments['channel'] = $content;
}
} elseif ($method === 'messages.sendMessage' && isset($arguments['peer']['_']) && \in_array($arguments['peer']['_'], ['inputEncryptedChat', 'updateEncryption', 'updateEncryptedChatTyping', 'updateEncryptedMessagesRead', 'updateNewEncryptedMessage', 'encryptedMessage', 'encryptedMessageService'])) {
} elseif ($method === 'messages.sendMessage' && isset($arguments['peer']['_']) && \in_array($arguments['peer']['_'], ['inputEncryptedChat', 'updateEncryption', 'updateEncryptedChatTyping', 'updateEncryptedMessagesRead', 'updateNewEncryptedMessage', 'encryptedMessage', 'encryptedMessageService'], true)) {
$method = 'messages.sendEncrypted';
$arguments = ['peer' => $arguments['peer'], 'message' => $arguments];
if (!isset($arguments['message']['_'])) {
Expand Down Expand Up @@ -359,7 +359,7 @@ private function methodAbstractions(string &$method, array &$arguments): ?Deferr
}
$arguments['queuePromise'] = new DeferredFuture;
return $arguments['queuePromise'];
} elseif (\in_array($method, ['messages.addChatUser', 'messages.deleteChatUser', 'messages.editChatAdmin', 'messages.editChatPhoto', 'messages.editChatTitle', 'messages.getFullChat', 'messages.exportChatInvite', 'messages.editChatAdmin', 'messages.migrateChat']) && isset($arguments['chat_id']) && (!\is_numeric($arguments['chat_id']) || $arguments['chat_id'] < 0)) {
} elseif (\in_array($method, ['messages.addChatUser', 'messages.deleteChatUser', 'messages.editChatAdmin', 'messages.editChatPhoto', 'messages.editChatTitle', 'messages.getFullChat', 'messages.exportChatInvite', 'messages.editChatAdmin', 'messages.migrateChat'], true) && isset($arguments['chat_id']) && (!\is_numeric($arguments['chat_id']) || $arguments['chat_id'] < 0)) {
$res = $this->API->getInfo($arguments['chat_id']);
if ($res['type'] !== 'chat') {
throw new Exception('chat_id is not a chat id (only normal groups allowed, not supergroups)!');
Expand All @@ -376,7 +376,7 @@ private function methodAbstractions(string &$method, array &$arguments): ?Deferr
}
} elseif ($method === 'photos.uploadProfilePhoto') {
if (isset($arguments['file'])) {
if (\is_array($arguments['file']) && !\in_array($arguments['file']['_'], ['inputFile', 'inputFileBig'])) {
if (\is_array($arguments['file']) && !\in_array($arguments['file']['_'], ['inputFile', 'inputFileBig'], true)) {
$method = 'photos.uploadProfilePhoto';
$arguments['id'] = $arguments['file'];
}
Expand Down
10 changes: 5 additions & 5 deletions src/DataCenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,10 +302,10 @@ public function generateContexts(int $dc_number, ?ConnectContext $context = null
UdpBufferedStream::class =>
[[DefaultStream::class, []], [UdpBufferedStream::class, []]],
};
if ($this->settings->getObfuscated() && !\in_array($default[2][0], [HttpsStream::class, HttpStream::class])) {
if ($this->settings->getObfuscated() && !\in_array($default[2][0], [HttpsStream::class, HttpStream::class], true)) {
$default = [[DefaultStream::class, []], [BufferedRawStream::class, []], [ObfuscatedStream::class, []], \end($default)];
}
if ($this->settings->getTransport() && !\in_array($default[2][0], [HttpsStream::class, HttpStream::class])) {
if ($this->settings->getTransport() && !\in_array($default[2][0], [HttpsStream::class, HttpStream::class], true)) {
switch ($this->settings->getTransport()) {
case DefaultStream::class:
if ($this->settings->getObfuscated()) {
Expand Down Expand Up @@ -335,7 +335,7 @@ public function generateContexts(int $dc_number, ?ConnectContext $context = null
$proxyCombos = [];
foreach ($this->settings->getProxies() as $proxy => $extras) {
foreach ($extras as $extra) {
if ($proxy === ObfuscatedStream::class && \in_array(\strlen($extra['secret']), [17, 34])) {
if ($proxy === ObfuscatedStream::class && \in_array(\strlen($extra['secret']), [17, 34], true)) {
$combos[] = [[DefaultStream::class, []], [BufferedRawStream::class, []], [$proxy, $extra], [IntermediatePaddedStream::class, []]];
}
foreach ($combos as $orig) {
Expand All @@ -354,7 +354,7 @@ public function generateContexts(int $dc_number, ?ConnectContext $context = null
[$first, $second] = [\array_slice($orig, 0, 2), \array_slice($orig, 2)];
$first[] = [$proxy, $extra];
$combo = \array_merge($first, $second);
} elseif (\in_array($orig[1][0], [WsStream::class, WssStream::class])) {
} elseif (\in_array($orig[1][0], [WsStream::class, WssStream::class], true)) {
[$first, $second] = [\array_slice($orig, 0, 1), \array_slice($orig, 1)];
$first[] = [BufferedRawStream::class, []];
$first[] = [$proxy, $extra];
Expand Down Expand Up @@ -433,7 +433,7 @@ public function generateContexts(int $dc_number, ?ConnectContext $context = null
if ($stream[0] === DefaultStream::class && $stream[1] === []) {
$stream[1] = $useDoH ? new DoHConnector($this->dohWrapper, $ctx) : $this->dohWrapper->dnsConnector;
}
if (\in_array($stream[0], [WsStream::class, WssStream::class]) && $stream[1] === []) {
if (\in_array($stream[0], [WsStream::class, WssStream::class], true) && $stream[1] === []) {
$stream[1] = $this->dohWrapper->webSocketConnector;
}
/** @var array{0: class-string, 1: mixed} $stream */
Expand Down
2 changes: 1 addition & 1 deletion src/DataCenterConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,7 @@ public function getExtra(): MTProto
*/
public function isHttp(): bool
{
return \in_array($this->ctx->getStreamName(), [HttpStream::class, HttpsStream::class]);
return \in_array($this->ctx->getStreamName(), [HttpStream::class, HttpsStream::class], true);
}
/**
* Check if is connected directly by IP address.
Expand Down
2 changes: 1 addition & 1 deletion src/DoHWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function generateContexts(string $uri, ConnectContext $context = null): a
[$first, $second] = [\array_slice($orig, 0, 2), \array_slice($orig, 2)];
$first[] = [$proxy, $extra];
$combo = \array_merge($first, $second);
} elseif (\in_array($orig[1][0], [WsStream::class, WssStream::class])) {
} elseif (\in_array($orig[1][0], [WsStream::class, WssStream::class], true)) {
[$first, $second] = [\array_slice($orig, 0, 1), \array_slice($orig, 1)];
$first[] = [BufferedRawStream::class, []];
$first[] = [$proxy, $extra];
Expand Down
2 changes: 1 addition & 1 deletion src/DocsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ public function mkDocs(): void
$header = '';
if (!isset($this->settings['td'])) {
foreach (self::DEFAULT_TEMPLATES as $template => $types) {
if (\in_array($type, $types)) {
if (\in_array($type, $types, true)) {
$header .= $this->template($template, $type);
}
}
Expand Down
32 changes: 16 additions & 16 deletions src/DocsBuilder/Constructors.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,20 @@ public function mkConstructors(): void
if (!isset($this->types[$php_type])) {
$this->types[$php_type] = ['constructors' => [], 'methods' => []];
}
if (!\in_array($data, $this->types[$php_type]['constructors'])) {
if (!\in_array($data, $this->types[$php_type]['constructors'], true)) {
$this->types[$php_type]['constructors'][] = $data;
}
$params = '';
foreach ($data['params'] as $param) {
if (\in_array($param['name'], ['flags', 'flags2', 'random_id', 'random_bytes'])) {
if (\in_array($param['name'], ['flags', 'flags2', 'random_id', 'random_bytes'], true)) {
continue;
}
if ($type === 'EncryptedMessage' && $param['name'] === 'bytes' && !isset($this->settings['td'])) {
$param['name'] = 'decrypted_message';
$param['type'] = 'DecryptedMessage';
}
$type_or_subtype = isset($param['subtype']) ? 'subtype' : 'type';
$type_or_bare_type = \ctype_upper(Tools::end(\explode('.', $param[$type_or_subtype]))[0]) || \in_array($param[$type_or_subtype], ['!X', 'X', 'bytes', 'true', 'false', 'double', 'string', 'Bool', 'int53', 'int', 'long', 'int128', 'int256', 'int512']) ? 'types' : 'constructors';
$type_or_bare_type = \ctype_upper(Tools::end(\explode('.', $param[$type_or_subtype]))[0]) || \in_array($param[$type_or_subtype], ['!X', 'X', 'bytes', 'true', 'false', 'double', 'string', 'Bool', 'int53', 'int', 'long', 'int128', 'int256', 'int512'], true) ? 'types' : 'constructors';
$param[$type_or_subtype] = \str_replace(['true', 'false'], ['Bool', 'Bool'], $param[$type_or_subtype]);
if (\preg_match('/%/', $param[$type_or_subtype])) {
$param[$type_or_subtype] = $this->TL->getConstructors($this->td)->findByType(\str_replace('%', '', $param[$type_or_subtype]))['predicate'];
Expand Down Expand Up @@ -113,22 +113,22 @@ public function mkConstructors(): void
$hasreplymarkup = false;
$hasentities = false;
foreach ($data['params'] as $param) {
if (\in_array($param['name'], ['flags', 'flags2', 'random_id', 'random_bytes'])) {
if (\in_array($param['name'], ['flags', 'flags2', 'random_id', 'random_bytes'], true)) {
continue;
}
if ($type === 'EncryptedMessage' && $param['name'] === 'bytes' && !isset($this->settings['td'])) {
$param['name'] = 'decrypted_message';
$param['type'] = 'DecryptedMessage';
}
if ($type === 'DecryptedMessageMedia' && \in_array($param['name'], ['key', 'iv'])) {
if ($type === 'DecryptedMessageMedia' && \in_array($param['name'], ['key', 'iv'], true)) {
unset(Lang::$lang['en']['object_'.$constructor.'_param_'.$param['name'].'_type_'.$param['type']]);
continue;
}
$ptype = $param[isset($param['subtype']) ? 'subtype' : 'type'];
if (\preg_match('/%/', $ptype)) {
$ptype = $this->TL->getConstructors($this->td)->findByType(\str_replace('%', '', $ptype))['predicate'];
}
$type_or_bare_type = (\ctype_upper(Tools::end(\explode('_', $ptype))[0]) || \in_array($ptype, ['!X', 'X', 'bytes', 'true', 'false', 'double', 'string', 'Bool', 'int53', 'int', 'long', 'int128', 'int256', 'int512'])) && $ptype !== 'MTmessage' ? 'types' : 'constructors';
$type_or_bare_type = (\ctype_upper(Tools::end(\explode('_', $ptype))[0]) || \in_array($ptype, ['!X', 'X', 'bytes', 'true', 'false', 'double', 'string', 'Bool', 'int53', 'int', 'long', 'int128', 'int256', 'int512'], true)) && $ptype !== 'MTmessage' ? 'types' : 'constructors';
if (\substr($ptype, -1) === '>') {
$ptype = \substr($ptype, 0, -1);
}
Expand All @@ -138,25 +138,25 @@ public function mkConstructors(): void
$ptype = 'Bool';
}
$human_ptype = $ptype;
if (\strpos($type, 'Input') === 0 && \in_array($ptype, ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputDialogPeer', 'DialogPeer', 'NotifyPeer', 'InputNotifyPeer', 'InputPeer']) && !isset($this->settings['td'])) {
if (\strpos($type, 'Input') === 0 && \in_array($ptype, ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputDialogPeer', 'DialogPeer', 'NotifyPeer', 'InputNotifyPeer', 'InputPeer'], true) && !isset($this->settings['td'])) {
$human_ptype = 'Username, chat ID, Update, Message or '.$ptype;
}
if (\strpos($type, 'Input') === 0 && \in_array($ptype, ['InputMedia', 'InputDocument', 'InputPhoto']) && !isset($this->settings['td'])) {
if (\strpos($type, 'Input') === 0 && \in_array($ptype, ['InputMedia', 'InputDocument', 'InputPhoto'], true) && !isset($this->settings['td'])) {
$human_ptype = 'MessageMedia, Message, Update or '.$ptype;
}
if (\in_array($ptype, ['InputMessage']) && !isset($this->settings['td'])) {
if (\in_array($ptype, ['InputMessage'], true) && !isset($this->settings['td'])) {
$human_ptype = 'Message ID or '.$ptype;
}
if (\in_array($ptype, ['InputEncryptedChat']) && !isset($this->settings['td'])) {
if (\in_array($ptype, ['InputEncryptedChat'], true) && !isset($this->settings['td'])) {
$human_ptype = 'Secret chat ID, Update, EncryptedMessage or '.$ptype;
}
if (\in_array($ptype, ['InputFile']) && !isset($this->settings['td'])) {
if (\in_array($ptype, ['InputFile'], true) && !isset($this->settings['td'])) {
$human_ptype = 'File path or '.$ptype;
}
if (\in_array($ptype, ['InputEncryptedFile']) && !isset($this->settings['td'])) {
if (\in_array($ptype, ['InputEncryptedFile'], true) && !isset($this->settings['td'])) {
$human_ptype = 'File path or '.$ptype;
}
$table .= '|'.StrTools::markdownEscape($param['name']).'|'.(isset($param['subtype']) ? 'Array of ' : '').'['.StrTools::markdownEscape($human_ptype).'](/API_docs/'.$type_or_bare_type.'/'.$ptype.'.md) | '.(isset($param['pow']) || $this->TL->getConstructors($this->td)->findByPredicate(\lcfirst($param['type']).'Empty') || $data['type'] === 'InputMedia' && $param['name'] === 'mime_type' || $data['type'] === 'DocumentAttribute' && \in_array($param['name'], ['w', 'h', 'duration']) ? 'Optional' : 'Yes').'|';
$table .= '|'.StrTools::markdownEscape($param['name']).'|'.(isset($param['subtype']) ? 'Array of ' : '').'['.StrTools::markdownEscape($human_ptype).'](/API_docs/'.$type_or_bare_type.'/'.$ptype.'.md) | '.(isset($param['pow']) || $this->TL->getConstructors($this->td)->findByPredicate(\lcfirst($param['type']).'Empty') || $data['type'] === 'InputMedia' && $param['name'] === 'mime_type' || $data['type'] === 'DocumentAttribute' && \in_array($param['name'], ['w', 'h', 'duration'], true) ? 'Optional' : 'Yes').'|';
if (!isset($this->TL->getDescriptionsRef()['constructors'][$constructor]['params'][$param['name']])) {
$this->addToLang('object_'.$constructor.'_param_'.$param['name'].'_type_'.$param['type']);
if (isset($this->TL->getDescriptionsRef()['constructors'][$constructor]['description'])) {
Expand All @@ -168,9 +168,9 @@ public function mkConstructors(): void
$table .= $this->TL->getDescriptionsRef()['constructors'][$constructor]['params'][$param['name']].'|';
}
$table .= PHP_EOL;
$pptype = \in_array($ptype, ['string', 'bytes']) ? "'".$ptype."'" : $ptype;
$ppptype = \in_array($ptype, ['string']) ? '"'.$ptype.'"' : $ptype;
$ppptype = \in_array($ptype, ['bytes']) ? '{"_": "bytes", "bytes":"base64 encoded '.$ptype.'"}' : $ppptype;
$pptype = \in_array($ptype, ['string', 'bytes'], true) ? "'".$ptype."'" : $ptype;
$ppptype = \in_array($ptype, ['string'], true) ? '"'.$ptype.'"' : $ptype;
$ppptype = \in_array($ptype, ['bytes'], true) ? '{"_": "bytes", "bytes":"base64 encoded '.$ptype.'"}' : $ppptype;
$params .= ", '".$param['name']."' => ";
$params .= isset($param['subtype']) ? '['.$pptype.', '.$pptype.']' : $pptype;
$lua_params .= ', '.$param['name'].'=';
Expand Down
Loading

0 comments on commit 33f8ade

Please sign in to comment.