Skip to content

Commit bfef257

Browse files
authored
Merge pull request #183 from WyriHaximus-labs/function-name-look-up-performance-improvement
Improve performance by prefixing all global functions calls with \ to skip the look up and resolve process and go straight to the global function
2 parents a915115 + f54040f commit bfef257

12 files changed

+133
-133
lines changed

src/Connection.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function __construct($resource, LoopInterface $loop)
5050
// See https://bugs.php.net/bug.php?id=65137
5151
// https://bugs.php.net/bug.php?id=41631
5252
// https://github.com/reactphp/socket-client/issues/24
53-
$clearCompleteBuffer = PHP_VERSION_ID < 50608;
53+
$clearCompleteBuffer = \PHP_VERSION_ID < 50608;
5454

5555
// PHP < 7.1.4 (and PHP < 7.0.18) suffers from a bug when writing big
5656
// chunks of data over TLS streams at once.
@@ -60,7 +60,7 @@ public function __construct($resource, LoopInterface $loop)
6060
// affected versions. Please update your PHP version.
6161
// This applies to all streams because TLS may be enabled later on.
6262
// See https://github.com/reactphp/socket/issues/105
63-
$limitWriteChunks = (PHP_VERSION_ID < 70018 || (PHP_VERSION_ID >= 70100 && PHP_VERSION_ID < 70104));
63+
$limitWriteChunks = (\PHP_VERSION_ID < 70018 || (\PHP_VERSION_ID >= 70100 && \PHP_VERSION_ID < 70104));
6464

6565
$this->input = new DuplexResourceStream(
6666
$resource,
@@ -120,7 +120,7 @@ public function close()
120120

121121
public function handleClose()
122122
{
123-
if (!is_resource($this->stream)) {
123+
if (!\is_resource($this->stream)) {
124124
return;
125125
}
126126

@@ -130,18 +130,18 @@ public function handleClose()
130130
// continuing to close the socket resource.
131131
// Underlying Stream implementation will take care of closing file
132132
// handle, so we otherwise keep this open here.
133-
@stream_socket_shutdown($this->stream, STREAM_SHUT_RDWR);
134-
stream_set_blocking($this->stream, false);
133+
@\stream_socket_shutdown($this->stream, \STREAM_SHUT_RDWR);
134+
\stream_set_blocking($this->stream, false);
135135
}
136136

137137
public function getRemoteAddress()
138138
{
139-
return $this->parseAddress(@stream_socket_get_name($this->stream, true));
139+
return $this->parseAddress(@\stream_socket_get_name($this->stream, true));
140140
}
141141

142142
public function getLocalAddress()
143143
{
144-
return $this->parseAddress(@stream_socket_get_name($this->stream, false));
144+
return $this->parseAddress(@\stream_socket_get_name($this->stream, false));
145145
}
146146

147147
private function parseAddress($address)
@@ -153,8 +153,8 @@ private function parseAddress($address)
153153
if ($this->unix) {
154154
// remove trailing colon from address for HHVM < 3.19: https://3v4l.org/5C1lo
155155
// note that technically ":" is a valid address, so keep this in place otherwise
156-
if (substr($address, -1) === ':' && defined('HHVM_VERSION_ID') && HHVM_VERSION_ID < 31900) {
157-
$address = (string)substr($address, 0, -1);
156+
if (\substr($address, -1) === ':' && \defined('HHVM_VERSION_ID') && \HHVM_VERSION_ID < 31900) {
157+
$address = (string)\substr($address, 0, -1);
158158
}
159159

160160
// work around unknown addresses should return null value: https://3v4l.org/5C1lo and https://bugs.php.net/bug.php?id=74556
@@ -167,10 +167,10 @@ private function parseAddress($address)
167167
}
168168

169169
// check if this is an IPv6 address which includes multiple colons but no square brackets
170-
$pos = strrpos($address, ':');
171-
if ($pos !== false && strpos($address, ':') < $pos && substr($address, 0, 1) !== '[') {
172-
$port = substr($address, $pos + 1);
173-
$address = '[' . substr($address, 0, $pos) . ']:' . $port;
170+
$pos = \strrpos($address, ':');
171+
if ($pos !== false && \strpos($address, ':') < $pos && \substr($address, 0, 1) !== '[') {
172+
$port = \substr($address, $pos + 1);
173+
$address = '[' . \substr($address, 0, $pos) . ']:' . $port;
174174
}
175175

176176
return ($this->encryptionEnabled ? 'tls' : 'tcp') . '://' . $address;

src/Connector.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ public function __construct(LoopInterface $loop, array $options = array())
4141
);
4242

4343
if ($options['timeout'] === true) {
44-
$options['timeout'] = (float)ini_get("default_socket_timeout");
44+
$options['timeout'] = (float)\ini_get("default_socket_timeout");
4545
}
4646

4747
if ($options['tcp'] instanceof ConnectorInterface) {
4848
$tcp = $options['tcp'];
4949
} else {
5050
$tcp = new TcpConnector(
5151
$loop,
52-
is_array($options['tcp']) ? $options['tcp'] : array()
52+
\is_array($options['tcp']) ? $options['tcp'] : array()
5353
);
5454
}
5555

@@ -62,7 +62,7 @@ public function __construct(LoopInterface $loop, array $options = array())
6262
} else {
6363
// try to load nameservers from system config or default to Google's public DNS
6464
$config = Config::loadSystemConfigBlocking();
65-
$server = $config->nameservers ? reset($config->nameservers) : '8.8.8.8';
65+
$server = $config->nameservers ? \reset($config->nameservers) : '8.8.8.8';
6666
}
6767

6868
$factory = new Factory();
@@ -94,7 +94,7 @@ public function __construct(LoopInterface $loop, array $options = array())
9494
$options['tls'] = new SecureConnector(
9595
$tcp,
9696
$loop,
97-
is_array($options['tls']) ? $options['tls'] : array()
97+
\is_array($options['tls']) ? $options['tls'] : array()
9898
);
9999
}
100100

@@ -120,12 +120,12 @@ public function __construct(LoopInterface $loop, array $options = array())
120120
public function connect($uri)
121121
{
122122
$scheme = 'tcp';
123-
if (strpos($uri, '://') !== false) {
124-
$scheme = (string)substr($uri, 0, strpos($uri, '://'));
123+
if (\strpos($uri, '://') !== false) {
124+
$scheme = (string)\substr($uri, 0, \strpos($uri, '://'));
125125
}
126126

127127
if (!isset($this->connectors[$scheme])) {
128-
return Promise\reject(new RuntimeException(
128+
return Promise\reject(new \RuntimeException(
129129
'No connector available for URI scheme "' . $scheme . '"'
130130
));
131131
}

src/DnsConnector.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,22 @@ public function __construct(ConnectorInterface $connector, Resolver $resolver)
2121

2222
public function connect($uri)
2323
{
24-
if (strpos($uri, '://') === false) {
25-
$parts = parse_url('tcp://' . $uri);
24+
if (\strpos($uri, '://') === false) {
25+
$parts = \parse_url('tcp://' . $uri);
2626
unset($parts['scheme']);
2727
} else {
28-
$parts = parse_url($uri);
28+
$parts = \parse_url($uri);
2929
}
3030

3131
if (!$parts || !isset($parts['host'])) {
32-
return Promise\reject(new InvalidArgumentException('Given URI "' . $uri . '" is invalid'));
32+
return Promise\reject(new \InvalidArgumentException('Given URI "' . $uri . '" is invalid'));
3333
}
3434

35-
$host = trim($parts['host'], '[]');
35+
$host = \trim($parts['host'], '[]');
3636
$connector = $this->connector;
3737

3838
// skip DNS lookup / URI manipulation if this URI already contains an IP
39-
if (false !== filter_var($host, FILTER_VALIDATE_IP)) {
39+
if (false !== \filter_var($host, \FILTER_VALIDATE_IP)) {
4040
return $connector->connect($uri);
4141
}
4242

@@ -55,7 +55,7 @@ function ($resolve, $reject) use (&$promise, &$resolved, $uri, $connector, $host
5555
$uri .= $parts['scheme'] . '://';
5656
}
5757

58-
if (strpos($ip, ':') !== false) {
58+
if (\strpos($ip, ':') !== false) {
5959
// enclose IPv6 addresses in square brackets before appending port
6060
$uri .= '[' . $ip . ']';
6161
} else {
@@ -80,9 +80,9 @@ function ($resolve, $reject) use (&$promise, &$resolved, $uri, $connector, $host
8080
// append original hostname as query if resolved via DNS and if
8181
// destination URI does not contain "hostname" query param already
8282
$args = array();
83-
parse_str(isset($parts['query']) ? $parts['query'] : '', $args);
83+
\parse_str(isset($parts['query']) ? $parts['query'] : '', $args);
8484
if ($host !== $ip && !isset($args['hostname'])) {
85-
$uri .= (isset($parts['query']) ? '&' : '?') . 'hostname=' . rawurlencode($host);
85+
$uri .= (isset($parts['query']) ? '&' : '?') . 'hostname=' . \rawurlencode($host);
8686
}
8787

8888
// append original fragment if known
@@ -92,14 +92,14 @@ function ($resolve, $reject) use (&$promise, &$resolved, $uri, $connector, $host
9292

9393
return $promise = $connector->connect($uri);
9494
}, function ($e) use ($uri, $reject) {
95-
$reject(new RuntimeException('Connection to ' . $uri .' failed during DNS lookup: ' . $e->getMessage(), 0, $e));
95+
$reject(new \RuntimeException('Connection to ' . $uri .' failed during DNS lookup: ' . $e->getMessage(), 0, $e));
9696
})->then($resolve, $reject);
9797
},
9898
function ($_, $reject) use (&$promise, &$resolved, $uri) {
9999
// cancellation should reject connection attempt
100100
// reject DNS resolution with custom reason, otherwise rely on connection cancellation below
101101
if ($resolved === null) {
102-
$reject(new RuntimeException('Connection to ' . $uri . ' cancelled during DNS lookup'));
102+
$reject(new \RuntimeException('Connection to ' . $uri . ' cancelled during DNS lookup'));
103103
}
104104

105105
// (try to) cancel pending DNS lookup / connection attempt

src/LimitingServer.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ public function close()
156156
public function handleConnection(ConnectionInterface $connection)
157157
{
158158
// close connection if limit exceeded
159-
if ($this->limit !== null && count($this->connections) >= $this->limit) {
160-
$this->handleError(new OverflowException('Connection closed because server reached connection limit'));
159+
if ($this->limit !== null && \count($this->connections) >= $this->limit) {
160+
$this->handleError(new \OverflowException('Connection closed because server reached connection limit'));
161161
$connection->close();
162162
return;
163163
}
@@ -169,7 +169,7 @@ public function handleConnection(ConnectionInterface $connection)
169169
});
170170

171171
// pause accepting new connections if limit exceeded
172-
if ($this->pauseOnLimit && !$this->autoPaused && count($this->connections) >= $this->limit) {
172+
if ($this->pauseOnLimit && !$this->autoPaused && \count($this->connections) >= $this->limit) {
173173
$this->autoPaused = true;
174174

175175
if (!$this->manuPaused) {
@@ -183,10 +183,10 @@ public function handleConnection(ConnectionInterface $connection)
183183
/** @internal */
184184
public function handleDisconnection(ConnectionInterface $connection)
185185
{
186-
unset($this->connections[array_search($connection, $this->connections)]);
186+
unset($this->connections[\array_search($connection, $this->connections)]);
187187

188188
// continue accepting new connection if below limit
189-
if ($this->autoPaused && count($this->connections) < $this->limit) {
189+
if ($this->autoPaused && \count($this->connections) < $this->limit) {
190190
$this->autoPaused = false;
191191

192192
if (!$this->manuPaused) {
@@ -196,7 +196,7 @@ public function handleDisconnection(ConnectionInterface $connection)
196196
}
197197

198198
/** @internal */
199-
public function handleError(Exception $error)
199+
public function handleError(\Exception $error)
200200
{
201201
$this->emit('error', array($error));
202202
}

src/SecureConnector.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ public function __construct(ConnectorInterface $connector, LoopInterface $loop,
2323

2424
public function connect($uri)
2525
{
26-
if (!function_exists('stream_socket_enable_crypto')) {
27-
return Promise\reject(new BadMethodCallException('Encryption not supported on your platform (HHVM < 3.8?)')); // @codeCoverageIgnore
26+
if (!\function_exists('stream_socket_enable_crypto')) {
27+
return Promise\reject(new \BadMethodCallException('Encryption not supported on your platform (HHVM < 3.8?)')); // @codeCoverageIgnore
2828
}
2929

30-
if (strpos($uri, '://') === false) {
30+
if (\strpos($uri, '://') === false) {
3131
$uri = 'tls://' . $uri;
3232
}
3333

34-
$parts = parse_url($uri);
34+
$parts = \parse_url($uri);
3535
if (!$parts || !isset($parts['scheme']) || $parts['scheme'] !== 'tls') {
36-
return Promise\reject(new InvalidArgumentException('Given URI "' . $uri . '" is invalid'));
36+
return Promise\reject(new \InvalidArgumentException('Given URI "' . $uri . '" is invalid'));
3737
}
3838

39-
$uri = str_replace('tls://', '', $uri);
39+
$uri = \str_replace('tls://', '', $uri);
4040
$context = $this->context;
4141

4242
$encryption = $this->streamEncryption;
@@ -47,12 +47,12 @@ public function connect($uri)
4747

4848
if (!$connection instanceof Connection) {
4949
$connection->close();
50-
throw new UnexpectedValueException('Base connector does not use internal Connection class exposing stream resource');
50+
throw new \UnexpectedValueException('Base connector does not use internal Connection class exposing stream resource');
5151
}
5252

5353
// set required SSL/TLS context options
5454
foreach ($context as $name => $value) {
55-
stream_context_set_option($connection->stream, 'ssl', $name, $value);
55+
\stream_context_set_option($connection->stream, 'ssl', $name, $value);
5656
}
5757

5858
// try to enable encryption

src/SecureServer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ final class SecureServer extends EventEmitter implements ServerInterface
117117
*/
118118
public function __construct(ServerInterface $tcp, LoopInterface $loop, array $context)
119119
{
120-
if (!function_exists('stream_socket_enable_crypto')) {
121-
throw new BadMethodCallException('Encryption not supported on your platform (HHVM < 3.8?)'); // @codeCoverageIgnore
120+
if (!\function_exists('stream_socket_enable_crypto')) {
121+
throw new \BadMethodCallException('Encryption not supported on your platform (HHVM < 3.8?)'); // @codeCoverageIgnore
122122
}
123123

124124
// default to empty passphrase to suppress blocking passphrase prompt
@@ -146,7 +146,7 @@ public function getAddress()
146146
return null;
147147
}
148148

149-
return str_replace('tcp://' , 'tls://', $address);
149+
return \str_replace('tcp://' , 'tls://', $address);
150150
}
151151

152152
public function pause()
@@ -168,13 +168,13 @@ public function close()
168168
public function handleConnection(ConnectionInterface $connection)
169169
{
170170
if (!$connection instanceof Connection) {
171-
$this->emit('error', array(new UnexpectedValueException('Base server does not use internal Connection class exposing stream resource')));
171+
$this->emit('error', array(new \UnexpectedValueException('Base server does not use internal Connection class exposing stream resource')));
172172
$connection->end();
173173
return;
174174
}
175175

176176
foreach ($this->context as $name => $value) {
177-
stream_context_set_option($connection->stream, 'ssl', $name, $value);
177+
\stream_context_set_option($connection->stream, 'ssl', $name, $value);
178178
}
179179

180180
$that = $this;

src/Server.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ public function __construct($uri, LoopInterface $loop, array $context = array())
2525
);
2626

2727
$scheme = 'tcp';
28-
$pos = strpos($uri, '://');
28+
$pos = \strpos($uri, '://');
2929
if ($pos !== false) {
30-
$scheme = substr($uri, 0, $pos);
30+
$scheme = \substr($uri, 0, $pos);
3131
}
3232

3333
if ($scheme === 'unix') {

src/StreamEncryption.php

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,28 @@ public function __construct(LoopInterface $loop, $server = true)
3232
// @link http://php.net/manual/en/migration56.openssl.php#migration56.openssl.crypto-method
3333
// @link https://3v4l.org/plbFn
3434
if ($server) {
35-
$this->method = STREAM_CRYPTO_METHOD_TLS_SERVER;
35+
$this->method = \STREAM_CRYPTO_METHOD_TLS_SERVER;
3636

37-
if (defined('STREAM_CRYPTO_METHOD_TLSv1_0_SERVER')) {
38-
$this->method |= STREAM_CRYPTO_METHOD_TLSv1_0_SERVER;
37+
if (\defined('STREAM_CRYPTO_METHOD_TLSv1_0_SERVER')) {
38+
$this->method |= \STREAM_CRYPTO_METHOD_TLSv1_0_SERVER;
3939
}
40-
if (defined('STREAM_CRYPTO_METHOD_TLSv1_1_SERVER')) {
41-
$this->method |= STREAM_CRYPTO_METHOD_TLSv1_1_SERVER;
40+
if (\defined('STREAM_CRYPTO_METHOD_TLSv1_1_SERVER')) {
41+
$this->method |= \STREAM_CRYPTO_METHOD_TLSv1_1_SERVER;
4242
}
43-
if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_SERVER')) {
44-
$this->method |= STREAM_CRYPTO_METHOD_TLSv1_2_SERVER;
43+
if (\defined('STREAM_CRYPTO_METHOD_TLSv1_2_SERVER')) {
44+
$this->method |= \STREAM_CRYPTO_METHOD_TLSv1_2_SERVER;
4545
}
4646
} else {
47-
$this->method = STREAM_CRYPTO_METHOD_TLS_CLIENT;
47+
$this->method = \STREAM_CRYPTO_METHOD_TLS_CLIENT;
4848

49-
if (defined('STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT')) {
50-
$this->method |= STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT;
49+
if (\defined('STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT')) {
50+
$this->method |= \STREAM_CRYPTO_METHOD_TLSv1_0_CLIENT;
5151
}
52-
if (defined('STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT')) {
53-
$this->method |= STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
52+
if (\defined('STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT')) {
53+
$this->method |= \STREAM_CRYPTO_METHOD_TLSv1_1_CLIENT;
5454
}
55-
if (defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
56-
$this->method |= STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
55+
if (\defined('STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT')) {
56+
$this->method |= \STREAM_CRYPTO_METHOD_TLSv1_2_CLIENT;
5757
}
5858
}
5959
}
@@ -77,7 +77,7 @@ public function toggle(Connection $stream, $toggle)
7777

7878
$deferred = new Deferred(function ($_, $reject) use ($toggle) {
7979
// cancelling this leaves this stream in an inconsistent state…
80-
$reject(new RuntimeException('Cancelled toggling encryption ' . $toggle ? 'on' : 'off'));
80+
$reject(new \RuntimeException('Cancelled toggling encryption ' . $toggle ? 'on' : 'off'));
8181
});
8282

8383
// get actual stream socket from stream instance
@@ -143,13 +143,13 @@ public function toggleCrypto($socket, Deferred $deferred, $toggle, $method)
143143

144144
if (\feof($socket) || $error === null) {
145145
// EOF or failed without error => connection closed during handshake
146-
$d->reject(new UnexpectedValueException(
146+
$d->reject(new \UnexpectedValueException(
147147
'Connection lost during TLS handshake',
148148
\defined('SOCKET_ECONNRESET') ? \SOCKET_ECONNRESET : 0
149149
));
150150
} else {
151151
// handshake failed with error message
152-
$d->reject(new UnexpectedValueException(
152+
$d->reject(new \UnexpectedValueException(
153153
'Unable to complete TLS handshake: ' . $error
154154
));
155155
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy