Skip to content

Commit f16ca72

Browse files
authored
Merge pull request #137 from WyriHaximus-secret-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 fdd0140 + aea4ab4 commit f16ca72

File tree

5 files changed

+40
-40
lines changed

5 files changed

+40
-40
lines changed

src/DuplexResourceStream.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,19 +37,19 @@ final class DuplexResourceStream extends EventEmitter implements DuplexStreamInt
3737

3838
public function __construct($stream, LoopInterface $loop, $readChunkSize = null, WritableStreamInterface $buffer = null)
3939
{
40-
if (!is_resource($stream) || get_resource_type($stream) !== "stream") {
40+
if (!\is_resource($stream) || \get_resource_type($stream) !== "stream") {
4141
throw new InvalidArgumentException('First parameter must be a valid stream resource');
4242
}
4343

4444
// ensure resource is opened for reading and wrting (fopen mode must contain "+")
45-
$meta = stream_get_meta_data($stream);
46-
if (isset($meta['mode']) && $meta['mode'] !== '' && strpos($meta['mode'], '+') === false) {
45+
$meta = \stream_get_meta_data($stream);
46+
if (isset($meta['mode']) && $meta['mode'] !== '' && \strpos($meta['mode'], '+') === false) {
4747
throw new InvalidArgumentException('Given stream resource is not opened in read and write mode');
4848
}
4949

5050
// this class relies on non-blocking I/O in order to not interrupt the event loop
5151
// e.g. pipes on Windows do not support this: https://bugs.php.net/bug.php?id=47918
52-
if (stream_set_blocking($stream, 0) !== true) {
52+
if (\stream_set_blocking($stream, 0) !== true) {
5353
throw new \RuntimeException('Unable to set stream resource to non-blocking mode');
5454
}
5555

@@ -61,8 +61,8 @@ public function __construct($stream, LoopInterface $loop, $readChunkSize = null,
6161
// triggered), so we can ignore platforms not supporting this (HHVM).
6262
// Pipe streams (such as STDIN) do not seem to require this and legacy
6363
// PHP versions cause SEGFAULTs on unbuffered pipe streams, so skip this.
64-
if (function_exists('stream_set_read_buffer') && !$this->isLegacyPipe($stream)) {
65-
stream_set_read_buffer($stream, 0);
64+
if (\function_exists('stream_set_read_buffer') && !$this->isLegacyPipe($stream)) {
65+
\stream_set_read_buffer($stream, 0);
6666
}
6767

6868
if ($buffer === null) {
@@ -140,8 +140,8 @@ public function close()
140140
$this->buffer->close();
141141
$this->removeAllListeners();
142142

143-
if (is_resource($this->stream)) {
144-
fclose($this->stream);
143+
if (\is_resource($this->stream)) {
144+
\fclose($this->stream);
145145
}
146146
}
147147

@@ -169,7 +169,7 @@ public function pipe(WritableStreamInterface $dest, array $options = array())
169169
public function handleData($stream)
170170
{
171171
$error = null;
172-
set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error) {
172+
\set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error) {
173173
$error = new \ErrorException(
174174
$errstr,
175175
0,
@@ -179,9 +179,9 @@ public function handleData($stream)
179179
);
180180
});
181181

182-
$data = stream_get_contents($stream, $this->bufferSize);
182+
$data = \stream_get_contents($stream, $this->bufferSize);
183183

184-
restore_error_handler();
184+
\restore_error_handler();
185185

186186
if ($error !== null) {
187187
$this->emit('error', array(new \RuntimeException('Unable to read from stream: ' . $error->getMessage(), 0, $error)));
@@ -212,8 +212,8 @@ public function handleData($stream)
212212
*/
213213
private function isLegacyPipe($resource)
214214
{
215-
if (PHP_VERSION_ID < 50428 || (PHP_VERSION_ID >= 50500 && PHP_VERSION_ID < 50512)) {
216-
$meta = stream_get_meta_data($resource);
215+
if (\PHP_VERSION_ID < 50428 || (\PHP_VERSION_ID >= 50500 && \PHP_VERSION_ID < 50512)) {
216+
$meta = \stream_get_meta_data($resource);
217217

218218
if (isset($meta['stream_type']) && $meta['stream_type'] === 'STDIO') {
219219
return true;

src/ReadableResourceStream.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,19 @@ final class ReadableResourceStream extends EventEmitter implements ReadableStrea
4040

4141
public function __construct($stream, LoopInterface $loop, $readChunkSize = null)
4242
{
43-
if (!is_resource($stream) || get_resource_type($stream) !== "stream") {
43+
if (!\is_resource($stream) || \get_resource_type($stream) !== "stream") {
4444
throw new InvalidArgumentException('First parameter must be a valid stream resource');
4545
}
4646

4747
// ensure resource is opened for reading (fopen mode must contain "r" or "+")
48-
$meta = stream_get_meta_data($stream);
49-
if (isset($meta['mode']) && $meta['mode'] !== '' && strpos($meta['mode'], 'r') === strpos($meta['mode'], '+')) {
48+
$meta = \stream_get_meta_data($stream);
49+
if (isset($meta['mode']) && $meta['mode'] !== '' && \strpos($meta['mode'], 'r') === \strpos($meta['mode'], '+')) {
5050
throw new InvalidArgumentException('Given stream resource is not opened in read mode');
5151
}
5252

5353
// this class relies on non-blocking I/O in order to not interrupt the event loop
5454
// e.g. pipes on Windows do not support this: https://bugs.php.net/bug.php?id=47918
55-
if (stream_set_blocking($stream, 0) !== true) {
55+
if (\stream_set_blocking($stream, 0) !== true) {
5656
throw new \RuntimeException('Unable to set stream resource to non-blocking mode');
5757
}
5858

@@ -64,8 +64,8 @@ public function __construct($stream, LoopInterface $loop, $readChunkSize = null)
6464
// triggered), so we can ignore platforms not supporting this (HHVM).
6565
// Pipe streams (such as STDIN) do not seem to require this and legacy
6666
// PHP versions cause SEGFAULTs on unbuffered pipe streams, so skip this.
67-
if (function_exists('stream_set_read_buffer') && !$this->isLegacyPipe($stream)) {
68-
stream_set_read_buffer($stream, 0);
67+
if (\function_exists('stream_set_read_buffer') && !$this->isLegacyPipe($stream)) {
68+
\stream_set_read_buffer($stream, 0);
6969
}
7070

7171
$this->stream = $stream;
@@ -113,16 +113,16 @@ public function close()
113113
$this->pause();
114114
$this->removeAllListeners();
115115

116-
if (is_resource($this->stream)) {
117-
fclose($this->stream);
116+
if (\is_resource($this->stream)) {
117+
\fclose($this->stream);
118118
}
119119
}
120120

121121
/** @internal */
122122
public function handleData()
123123
{
124124
$error = null;
125-
set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error) {
125+
\set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error) {
126126
$error = new \ErrorException(
127127
$errstr,
128128
0,
@@ -132,9 +132,9 @@ public function handleData()
132132
);
133133
});
134134

135-
$data = stream_get_contents($this->stream, $this->bufferSize);
135+
$data = \stream_get_contents($this->stream, $this->bufferSize);
136136

137-
restore_error_handler();
137+
\restore_error_handler();
138138

139139
if ($error !== null) {
140140
$this->emit('error', array(new \RuntimeException('Unable to read from stream: ' . $error->getMessage(), 0, $error)));
@@ -165,8 +165,8 @@ public function handleData()
165165
*/
166166
private function isLegacyPipe($resource)
167167
{
168-
if (PHP_VERSION_ID < 50428 || (PHP_VERSION_ID >= 50500 && PHP_VERSION_ID < 50512)) {
169-
$meta = stream_get_meta_data($resource);
168+
if (\PHP_VERSION_ID < 50428 || (\PHP_VERSION_ID >= 50500 && \PHP_VERSION_ID < 50512)) {
169+
$meta = \stream_get_meta_data($resource);
170170

171171
if (isset($meta['stream_type']) && $meta['stream_type'] === 'STDIO') {
172172
return true;

src/ThroughStream.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ final class ThroughStream extends EventEmitter implements DuplexStreamInterface
8484

8585
public function __construct($callback = null)
8686
{
87-
if ($callback !== null && !is_callable($callback)) {
87+
if ($callback !== null && !\is_callable($callback)) {
8888
throw new InvalidArgumentException('Invalid transformation callback given');
8989
}
9090

@@ -128,7 +128,7 @@ public function write($data)
128128

129129
if ($this->callback !== null) {
130130
try {
131-
$data = call_user_func($this->callback, $data);
131+
$data = \call_user_func($this->callback, $data);
132132
} catch (\Exception $e) {
133133
$this->emit('error', array($e));
134134
$this->close();

src/Util.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static function forwardEvents($source, $target, array $events)
6868
{
6969
foreach ($events as $event) {
7070
$source->on($event, function () use ($event, $target) {
71-
$target->emit($event, func_get_args());
71+
$target->emit($event, \func_get_args());
7272
});
7373
}
7474
}

src/WritableResourceStream.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ final class WritableResourceStream extends EventEmitter implements WritableStrea
1919

2020
public function __construct($stream, LoopInterface $loop, $writeBufferSoftLimit = null, $writeChunkSize = null)
2121
{
22-
if (!is_resource($stream) || get_resource_type($stream) !== "stream") {
22+
if (!\is_resource($stream) || \get_resource_type($stream) !== "stream") {
2323
throw new \InvalidArgumentException('First parameter must be a valid stream resource');
2424
}
2525

2626
// ensure resource is opened for writing (fopen mode must contain either of "waxc+")
27-
$meta = stream_get_meta_data($stream);
28-
if (isset($meta['mode']) && $meta['mode'] !== '' && strtr($meta['mode'], 'waxc+', '.....') === $meta['mode']) {
27+
$meta = \stream_get_meta_data($stream);
28+
if (isset($meta['mode']) && $meta['mode'] !== '' && \strtr($meta['mode'], 'waxc+', '.....') === $meta['mode']) {
2929
throw new \InvalidArgumentException('Given stream resource is not opened in write mode');
3030
}
3131

3232
// this class relies on non-blocking I/O in order to not interrupt the event loop
3333
// e.g. pipes on Windows do not support this: https://bugs.php.net/bug.php?id=47918
34-
if (stream_set_blocking($stream, 0) !== true) {
34+
if (\stream_set_blocking($stream, 0) !== true) {
3535
throw new \RuntimeException('Unable to set stream resource to non-blocking mode');
3636
}
3737

@@ -96,16 +96,16 @@ public function close()
9696
$this->emit('close');
9797
$this->removeAllListeners();
9898

99-
if (is_resource($this->stream)) {
100-
fclose($this->stream);
99+
if (\is_resource($this->stream)) {
100+
\fclose($this->stream);
101101
}
102102
}
103103

104104
/** @internal */
105105
public function handleWrite()
106106
{
107107
$error = null;
108-
set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error) {
108+
\set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error) {
109109
$error = array(
110110
'message' => $errstr,
111111
'number' => $errno,
@@ -115,12 +115,12 @@ public function handleWrite()
115115
});
116116

117117
if ($this->writeChunkSize === -1) {
118-
$sent = fwrite($this->stream, $this->data);
118+
$sent = \fwrite($this->stream, $this->data);
119119
} else {
120-
$sent = fwrite($this->stream, $this->data, $this->writeChunkSize);
120+
$sent = \fwrite($this->stream, $this->data, $this->writeChunkSize);
121121
}
122122

123-
restore_error_handler();
123+
\restore_error_handler();
124124

125125
// Only report errors if *nothing* could be sent.
126126
// Any hard (permanent) error will fail to send any data at all.
@@ -147,7 +147,7 @@ public function handleWrite()
147147
}
148148

149149
$exceeded = isset($this->data[$this->softLimit - 1]);
150-
$this->data = (string) substr($this->data, $sent);
150+
$this->data = (string) \substr($this->data, $sent);
151151

152152
// buffer has been above limit and is now below limit
153153
if ($exceeded && !isset($this->data[$this->softLimit - 1])) {

0 commit comments

Comments
 (0)