Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance by prefixing all global functions calls with \ to skip the look up and resolve process and go straight to the global function #137

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/DuplexResourceStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,19 @@ final class DuplexResourceStream extends EventEmitter implements DuplexStreamInt

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

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

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

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

if ($buffer === null) {
Expand Down Expand Up @@ -140,8 +140,8 @@ public function close()
$this->buffer->close();
$this->removeAllListeners();

if (is_resource($this->stream)) {
fclose($this->stream);
if (\is_resource($this->stream)) {
\fclose($this->stream);
}
}

Expand Down Expand Up @@ -169,7 +169,7 @@ public function pipe(WritableStreamInterface $dest, array $options = array())
public function handleData($stream)
{
$error = null;
set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error) {
\set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error) {
$error = new \ErrorException(
$errstr,
0,
Expand All @@ -179,9 +179,9 @@ public function handleData($stream)
);
});

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

restore_error_handler();
\restore_error_handler();

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

if (isset($meta['stream_type']) && $meta['stream_type'] === 'STDIO') {
return true;
Expand Down
26 changes: 13 additions & 13 deletions src/ReadableResourceStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@ final class ReadableResourceStream extends EventEmitter implements ReadableStrea

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

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

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

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

$this->stream = $stream;
Expand Down Expand Up @@ -113,16 +113,16 @@ public function close()
$this->pause();
$this->removeAllListeners();

if (is_resource($this->stream)) {
fclose($this->stream);
if (\is_resource($this->stream)) {
\fclose($this->stream);
}
}

/** @internal */
public function handleData()
{
$error = null;
set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error) {
\set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error) {
$error = new \ErrorException(
$errstr,
0,
Expand All @@ -132,9 +132,9 @@ public function handleData()
);
});

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

restore_error_handler();
\restore_error_handler();

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

if (isset($meta['stream_type']) && $meta['stream_type'] === 'STDIO') {
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/ThroughStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ final class ThroughStream extends EventEmitter implements DuplexStreamInterface

public function __construct($callback = null)
{
if ($callback !== null && !is_callable($callback)) {
if ($callback !== null && !\is_callable($callback)) {
throw new InvalidArgumentException('Invalid transformation callback given');
}

Expand Down Expand Up @@ -128,7 +128,7 @@ public function write($data)

if ($this->callback !== null) {
try {
$data = call_user_func($this->callback, $data);
$data = \call_user_func($this->callback, $data);
} catch (\Exception $e) {
$this->emit('error', array($e));
$this->close();
Expand Down
2 changes: 1 addition & 1 deletion src/Util.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public static function forwardEvents($source, $target, array $events)
{
foreach ($events as $event) {
$source->on($event, function () use ($event, $target) {
$target->emit($event, func_get_args());
$target->emit($event, \func_get_args());
});
}
}
Expand Down
22 changes: 11 additions & 11 deletions src/WritableResourceStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ final class WritableResourceStream extends EventEmitter implements WritableStrea

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

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

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

Expand Down Expand Up @@ -96,16 +96,16 @@ public function close()
$this->emit('close');
$this->removeAllListeners();

if (is_resource($this->stream)) {
fclose($this->stream);
if (\is_resource($this->stream)) {
\fclose($this->stream);
}
}

/** @internal */
public function handleWrite()
{
$error = null;
set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error) {
\set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error) {
$error = array(
'message' => $errstr,
'number' => $errno,
Expand All @@ -115,12 +115,12 @@ public function handleWrite()
});

if ($this->writeChunkSize === -1) {
$sent = fwrite($this->stream, $this->data);
$sent = \fwrite($this->stream, $this->data);
} else {
$sent = fwrite($this->stream, $this->data, $this->writeChunkSize);
$sent = \fwrite($this->stream, $this->data, $this->writeChunkSize);
}

restore_error_handler();
\restore_error_handler();

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

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

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