Skip to content

Commit 658fe52

Browse files
authored
Merge pull request #28 from WyriHaximus-labs/function-name-look-up-performance-improvement
Prefix all global functions calls with \ to skip the look up and resolve process and go straight to the global function
2 parents b007b57 + 29030b1 commit 658fe52

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed

src/Buffer.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function send($data, $remoteAddress = null)
3737

3838
public function onWritable()
3939
{
40-
list($data, $remoteAddress) = array_shift($this->outgoing);
40+
list($data, $remoteAddress) = \array_shift($this->outgoing);
4141

4242
try {
4343
$this->handleWrite($data, $remoteAddress);
@@ -105,14 +105,14 @@ protected function handleWrite($data, $remoteAddress)
105105
if ($remoteAddress === null) {
106106
// do not use fwrite() as it obeys the stream buffer size and
107107
// packets are not to be split at 8kb
108-
$ret = @stream_socket_sendto($this->socket, $data);
108+
$ret = @\stream_socket_sendto($this->socket, $data);
109109
} else {
110-
$ret = @stream_socket_sendto($this->socket, $data, 0, $remoteAddress);
110+
$ret = @\stream_socket_sendto($this->socket, $data, 0, $remoteAddress);
111111
}
112112

113113
if ($ret < 0 || $ret === false) {
114-
$error = error_get_last();
115-
throw new Exception('Unable to send packet: ' . trim($error['message']));
114+
$error = \error_get_last();
115+
throw new Exception('Unable to send packet: ' . \trim($error['message']));
116116
}
117117
}
118118
}

src/Factory.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function __construct(LoopInterface $loop, Resolver $resolver = null)
2828
if ($resolver === null) {
2929
// try to load nameservers from system config or default to Google's public DNS
3030
$config = Config::loadSystemConfigBlocking();
31-
$server = $config->nameservers ? reset($config->nameservers) : '8.8.8.8';
31+
$server = $config->nameservers ? \reset($config->nameservers) : '8.8.8.8';
3232

3333
$factory = new DnsFactory();
3434
$resolver = $factory->create($server, $loop);
@@ -43,7 +43,7 @@ public function createClient($address)
4343
$loop = $this->loop;
4444

4545
return $this->resolveAddress($address)->then(function ($address) use ($loop) {
46-
$socket = @stream_socket_client($address, $errno, $errstr);
46+
$socket = @\stream_socket_client($address, $errno, $errstr);
4747
if (!$socket) {
4848
throw new Exception('Unable to create client socket: ' . $errstr, $errno);
4949
}
@@ -57,7 +57,7 @@ public function createServer($address)
5757
$loop = $this->loop;
5858

5959
return $this->resolveAddress($address)->then(function ($address) use ($loop) {
60-
$socket = @stream_socket_server($address, $errno, $errstr, STREAM_SERVER_BIND);
60+
$socket = @\stream_socket_server($address, $errno, $errstr, \STREAM_SERVER_BIND);
6161
if (!$socket) {
6262
throw new Exception('Unable to create server socket: ' . $errstr, $errno);
6363
}
@@ -68,18 +68,18 @@ public function createServer($address)
6868

6969
protected function resolveAddress($address)
7070
{
71-
if (strpos($address, '://') === false) {
71+
if (\strpos($address, '://') === false) {
7272
$address = 'udp://' . $address;
7373
}
7474

7575
// parse_url() does not accept null ports (random port assignment) => manually remove
7676
$nullport = false;
77-
if (substr($address, -2) === ':0') {
78-
$address = substr($address, 0, -2);
77+
if (\substr($address, -2) === ':0') {
78+
$address = \substr($address, 0, -2);
7979
$nullport = true;
8080
}
8181

82-
$parts = parse_url($address);
82+
$parts = \parse_url($address);
8383

8484
if (!$parts || !isset($parts['host'])) {
8585
return Promise\resolve($address);
@@ -90,12 +90,12 @@ protected function resolveAddress($address)
9090
}
9191

9292
// remove square brackets for IPv6 addresses
93-
$host = trim($parts['host'], '[]');
93+
$host = \trim($parts['host'], '[]');
9494

9595
return $this->resolveHost($host)->then(function ($host) use ($parts) {
9696
$address = $parts['scheme'] . '://';
9797

98-
if (isset($parts['port']) && strpos($host, ':') !== false) {
98+
if (isset($parts['port']) && \strpos($host, ':') !== false) {
9999
// enclose IPv6 address in square brackets if a port will be appended
100100
$host = '[' . $host . ']';
101101
}
@@ -113,7 +113,7 @@ protected function resolveAddress($address)
113113
protected function resolveHost($host)
114114
{
115115
// there's no need to resolve if the host is already given as an IP address
116-
if (false !== filter_var($host, FILTER_VALIDATE_IP)) {
116+
if (false !== \filter_var($host, \FILTER_VALIDATE_IP)) {
117117
return Promise\resolve($host);
118118
}
119119

src/Socket.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ public function __construct(LoopInterface $loop, $socket, Buffer $buffer = null)
3636

3737
public function getLocalAddress()
3838
{
39-
return $this->sanitizeAddress(@stream_socket_get_name($this->socket, false));
39+
return $this->sanitizeAddress(@\stream_socket_get_name($this->socket, false));
4040
}
4141

4242
public function getRemoteAddress()
4343
{
44-
return $this->sanitizeAddress(@stream_socket_get_name($this->socket, true));
44+
return $this->sanitizeAddress(@\stream_socket_get_name($this->socket, true));
4545
}
4646

4747
public function send($data, $remoteAddress = null)
@@ -103,17 +103,17 @@ private function sanitizeAddress($address)
103103
}
104104

105105
// this is an IPv6 address which includes colons but no square brackets
106-
$pos = strrpos($address, ':');
107-
if ($pos !== false && strpos($address, ':') < $pos && substr($address, 0, 1) !== '[') {
108-
$port = substr($address, $pos + 1);
109-
$address = '[' . substr($address, 0, $pos) . ']:' . $port;
106+
$pos = \strrpos($address, ':');
107+
if ($pos !== false && \strpos($address, ':') < $pos && \substr($address, 0, 1) !== '[') {
108+
$port = \substr($address, $pos + 1);
109+
$address = '[' . \substr($address, 0, $pos) . ']:' . $port;
110110
}
111111
return $address;
112112
}
113113

114114
protected function handleReceive(&$peerAddress)
115115
{
116-
$data = stream_socket_recvfrom($this->socket, $this->bufferSize, 0, $peerAddress);
116+
$data = \stream_socket_recvfrom($this->socket, $this->bufferSize, 0, $peerAddress);
117117

118118
if ($data === false) {
119119
// receiving data failed => remote side rejected one of our packets
@@ -130,6 +130,6 @@ protected function handleReceive(&$peerAddress)
130130

131131
protected function handleClose()
132132
{
133-
fclose($this->socket);
133+
\fclose($this->socket);
134134
}
135135
}

0 commit comments

Comments
 (0)