Quellcode durchsuchen

Merge pull request #458 from joanhey/performance

Small Performance
walkor vor 6 Jahren
Ursprung
Commit
cb239e215f

+ 1 - 1
Connection/AsyncTcpConnection.php

@@ -296,7 +296,7 @@ class AsyncTcpConnection extends TcpConnection
         // Remove write listener.
         Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
 
-        if ($this->_status != self::STATUS_CONNECTING) {
+        if ($this->_status !== self::STATUS_CONNECTING) {
             return;
         }
 

+ 1 - 1
Connection/AsyncUdpConnection.php

@@ -121,7 +121,7 @@ class AsyncUdpConnection extends UdpConnection
             $parser      = $this->protocol;
             $send_buffer = $parser::encode($send_buffer, $this);
             if ($send_buffer === '') {
-                return null;
+                return;
             }
         }
         if ($this->connected === false) {

+ 7 - 9
Connection/TcpConnection.php

@@ -343,22 +343,20 @@ class TcpConnection extends ConnectionInterface
             $parser      = $this->protocol;
             $send_buffer = $parser::encode($send_buffer, $this);
             if ($send_buffer === '') {
-                return null;
+                return;
             }
         }
 
         if ($this->_status !== self::STATUS_ESTABLISHED ||
             ($this->transport === 'ssl' && $this->_sslHandshakeCompleted !== true)
         ) {
-            if ($this->_sendBuffer) {
-                if ($this->bufferIsFull()) {
-                    self::$statistics['send_fail']++;
-                    return false;
-                }
+            if ($this->_sendBuffer && $this->bufferIsFull()) {
+                self::$statistics['send_fail']++;
+                return false;
             }
             $this->_sendBuffer .= $send_buffer;
             $this->checkBufferWillFull();
-            return null;
+            return;
         }
 
         // Attempt to send data directly.
@@ -367,7 +365,7 @@ class TcpConnection extends ConnectionInterface
                 Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
                 $this->_sendBuffer = $send_buffer;
                 $this->checkBufferWillFull();
-                return null;
+                return;
             }
             \set_error_handler(function(){});
             $len = \fwrite($this->_socket, $send_buffer);
@@ -404,7 +402,7 @@ class TcpConnection extends ConnectionInterface
             Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
             // Check if the send buffer will be full.
             $this->checkBufferWillFull();
-            return null;
+            return;
         } else {
             if ($this->bufferIsFull()) {
                 self::$statistics['send_fail']++;

+ 1 - 1
Connection/UdpConnection.php

@@ -65,7 +65,7 @@ class UdpConnection extends ConnectionInterface
             $parser      = $this->protocol;
             $send_buffer = $parser::encode($send_buffer, $this);
             if ($send_buffer === '') {
-                return null;
+                return;
             }
         }
         return \strlen($send_buffer) === \stream_socket_sendto($this->_socket, $send_buffer, 0, $this->_remoteAddress);

+ 1 - 1
Events/Ev.php

@@ -72,7 +72,7 @@ class Ev implements EventInterface
                 return true;
             case self::EV_TIMER:
             case self::EV_TIMER_ONCE:
-                $repeat                             = $flag == self::EV_TIMER_ONCE ? 0 : $fd;
+                $repeat                             = $flag === self::EV_TIMER_ONCE ? 0 : $fd;
                 $param                              = array($func, (array)$args, $flag, $fd, self::$_timerId);
                 $event                              = new \EvTimer($fd, $repeat, array($this, 'timerCallback'), $param);
                 $this->_eventTimer[self::$_timerId] = $event;

+ 7 - 7
Events/Swoole.php

@@ -56,7 +56,7 @@ class Swoole implements EventInterface
                 return $res;
             case self::EV_TIMER:
             case self::EV_TIMER_ONCE:
-                $method = self::EV_TIMER == $flag ? 'tick' : 'after';
+                $method = self::EV_TIMER === $flag ? 'tick' : 'after';
                 if ($this->mapId > PHP_INT_MAX) {
                     $this->mapId = 0;
                 }
@@ -74,7 +74,7 @@ class Swoole implements EventInterface
                             }
                         }
                     });
-                if ($flag == self::EV_TIMER_ONCE) {
+                if ($flag === self::EV_TIMER_ONCE) {
                     $this->_timerOnceMap[$mapId] = $timer_id;
                     $this->_timer[$timer_id] = $mapId;
                 } else {
@@ -85,7 +85,7 @@ class Swoole implements EventInterface
             case self::EV_WRITE:
                 $fd_key = (int) $fd;
                 if (! isset($this->_fd[$fd_key])) {
-                    if ($flag == self::EV_READ) {
+                    if ($flag === self::EV_READ) {
                         $res = Event::add($fd, $func, null, SWOOLE_EVENT_READ);
                         $fd_type = SWOOLE_EVENT_READ;
                     } else {
@@ -98,14 +98,14 @@ class Swoole implements EventInterface
                 } else {
                     $fd_val = $this->_fd[$fd_key];
                     $res = true;
-                    if ($flag == self::EV_READ) {
-                        if (($fd_val & SWOOLE_EVENT_READ) != SWOOLE_EVENT_READ) {
+                    if ($flag === self::EV_READ) {
+                        if (($fd_val & SWOOLE_EVENT_READ) !== SWOOLE_EVENT_READ) {
                             $res = Event::set($fd, $func, null,
                                 SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE);
                             $this->_fd[$fd_key] |= SWOOLE_EVENT_READ;
                         }
                     } else {
-                        if (($fd_val & SWOOLE_EVENT_WRITE) != SWOOLE_EVENT_WRITE) {
+                        if (($fd_val & SWOOLE_EVENT_WRITE) !== SWOOLE_EVENT_WRITE) {
                             $res = Event::set($fd, null, $func,
                                 SWOOLE_EVENT_READ | SWOOLE_EVENT_WRITE);
                             $this->_fd[$fd_key] |= SWOOLE_EVENT_WRITE;
@@ -147,7 +147,7 @@ class Swoole implements EventInterface
                 $fd_key = (int) $fd;
                 if (isset($this->_fd[$fd_key])) {
                     $fd_val = $this->_fd[$fd_key];
-                    if ($flag == self::EV_READ) {
+                    if ($flag === self::EV_READ) {
                         $flag_remove = ~ SWOOLE_EVENT_READ;
                     } else {
                         $flag_remove = ~ SWOOLE_EVENT_WRITE;

+ 11 - 11
Protocols/Http.php

@@ -161,7 +161,7 @@ class Http
                     $_SERVER['CONTENT_LENGTH'] = $value;
                     break;
                 case 'UPGRADE':
-					if($value=='websocket'){
+					if($value === 'websocket'){
 						$connection->protocol = "\\Workerman\\Protocols\\Websocket";
 						return \Workerman\Protocols\Websocket::input($recv_buffer,$connection);
 					}
@@ -189,7 +189,7 @@ class Http
         }
 
         // Parse other HTTP action parameters
-        if ($_SERVER['REQUEST_METHOD'] != 'GET' && $_SERVER['REQUEST_METHOD'] != "POST") {
+        if ($_SERVER['REQUEST_METHOD'] !== 'GET' && $_SERVER['REQUEST_METHOD'] !== "POST") {
             $data = array();
             if ($_SERVER['CONTENT_TYPE'] === "application/x-www-form-urlencoded") {
                 \parse_str($http_body, $data);
@@ -279,7 +279,7 @@ class Http
      */
     public static function header($content, $replace = true, $http_response_code = 0)
     {
-        if (PHP_SAPI != 'cli') {
+        if (PHP_SAPI !== 'cli') {
             return $http_response_code ? \header($content, $replace, $http_response_code) : \header($content, $replace);
         }
         if (\strpos($content, 'HTTP') === 0) {
@@ -319,7 +319,7 @@ class Http
      */
     public static function headerRemove($name)
     {
-        if (PHP_SAPI != 'cli') {
+        if (PHP_SAPI !== 'cli') {
             \header_remove($name);
             return;
         }
@@ -347,7 +347,7 @@ class Http
         $secure = false,
         $HTTPOnly = false
     ) {
-        if (PHP_SAPI != 'cli') {
+        if (PHP_SAPI !== 'cli') {
             return \setcookie($name, $value, $maxage, $path, $domain, $secure, $HTTPOnly);
         }
         return self::header(
@@ -379,7 +379,7 @@ class Http
      */
     public static function sessionId($id = null)
     {
-        if (PHP_SAPI != 'cli') {
+        if (PHP_SAPI !== 'cli') {
             return $id ? \session_id($id) : \session_id();
         }
         if (static::sessionStarted() && HttpCache::$instance->sessionFile) {
@@ -397,7 +397,7 @@ class Http
      */
     public static function sessionName($name = null)
     {
-        if (PHP_SAPI != 'cli') {
+        if (PHP_SAPI !== 'cli') {
             return $name ? \session_name($name) : \session_name();
         }
         $session_name = HttpCache::$sessionName;
@@ -416,7 +416,7 @@ class Http
      */
     public static function sessionSavePath($path = null)
     {
-        if (PHP_SAPI != 'cli') {
+        if (PHP_SAPI !== 'cli') {
             return $path ? \session_save_path($path) : \session_save_path();
         }
         if ($path && \is_dir($path) && \is_writable($path) && !static::sessionStarted()) {
@@ -444,7 +444,7 @@ class Http
      */
     public static function sessionStart()
     {
-        if (PHP_SAPI != 'cli') {
+        if (PHP_SAPI !== 'cli') {
             return \session_start();
         }
 
@@ -493,7 +493,7 @@ class Http
      */
     public static function sessionWriteClose()
     {
-        if (PHP_SAPI != 'cli') {
+        if (PHP_SAPI !== 'cli') {
             \session_write_close();
             return true;
         }
@@ -514,7 +514,7 @@ class Http
      */
     public static function end($msg = '')
     {
-        if (PHP_SAPI != 'cli') {
+        if (PHP_SAPI !== 'cli') {
             exit($msg);
         }
         if ($msg) {

+ 1 - 1
WebServer.php

@@ -217,7 +217,7 @@ class WebServer extends Worker
                     include $workerman_file;
                 } catch (\Exception $e) {
                     // Jump_exit?
-                    if ($e->getMessage() != 'jump_exit') {
+                    if ($e->getMessage() !== 'jump_exit') {
                         Worker::safeEcho($e);
                     }
                 }

+ 16 - 16
Worker.php

@@ -519,8 +519,8 @@ class Worker
     protected static function checkSapiEnv()
     {
         // Only for cli.
-        if (\php_sapi_name() != "cli") {
-            exit("only run in command line mode \n");
+        if (\php_sapi_name() !== 'cli') {
+            exit('only run in command line mode \n');
         }
         if (DIRECTORY_SEPARATOR === '\\') {
             self::$_OS = OS_TYPE_WINDOWS;
@@ -586,7 +586,7 @@ class Worker
     {
         $fd = \fopen(static::$_startFile, 'r');
         if (!$fd || !flock($fd, LOCK_EX)) {
-            static::log("Workerman[".static::$_startFile."] already running");
+            static::log("Workerman[".static::$_startFile."] already running.");
             exit;
         }
     }
@@ -622,7 +622,7 @@ class Worker
             if (empty($worker->user)) {
                 $worker->user = static::getCurrentUser();
             } else {
-                if (\posix_getuid() !== 0 && $worker->user != static::getCurrentUser()) {
+                if (\posix_getuid() !== 0 && $worker->user !== static::getCurrentUser()) {
                     static::log('Warning: You must have the root privileges to change uid and gid.');
                 }
             }
@@ -635,7 +635,7 @@ class Worker
 
             // Get column mapping for UI
             foreach(static::getUiColumns() as $column_name => $prop){
-                !isset($worker->{$prop}) && $worker->{$prop}= 'NNNN';
+                !isset($worker->{$prop}) && $worker->{$prop} = 'NNNN';
                 $prop_length = \strlen($worker->{$prop});
                 $key = '_max' . \ucfirst(\strtolower($column_name)) . 'NameLength';
                 static::$$key = \max(static::$$key, $prop_length);
@@ -684,7 +684,7 @@ class Worker
     {
         foreach (static::$_workers as $worker_id => $worker) {
             $new_id_map = array();
-            $worker->count = $worker->count <= 0 ? 1 : $worker->count;
+            $worker->count = $worker->count < 1 ? 1 : $worker->count;
             for($key = 0; $key < $worker->count; $key++) {
                 $new_id_map[$key] = isset(static::$_idMap[$worker_id][$key]) ? static::$_idMap[$worker_id][$key] : 0;
             }
@@ -715,10 +715,10 @@ class Worker
             return;
         }
         if (static::$_OS !== OS_TYPE_LINUX) {
-            static::safeEcho("----------------------- WORKERMAN -----------------------------\r\n");
-            static::safeEcho('Workerman version:'. static::VERSION. "          PHP version:". PHP_VERSION. "\r\n");
-            static::safeEcho("------------------------ WORKERS -------------------------------\r\n");
-            static::safeEcho("worker               listen                              processes status\r\n");
+            static::safeEcho('----------------------- WORKERMAN -----------------------------\r\n');
+            static::safeEcho('Workerman version:'. static::VERSION. '          PHP version:'. PHP_VERSION. '\r\n');
+            static::safeEcho('------------------------ WORKERS -------------------------------\r\n');
+            static::safeEcho('worker               listen                              processes status\r\n');
             return;
         }
 
@@ -735,7 +735,7 @@ class Worker
         foreach(static::getUiColumns() as $column_name => $prop){
             $key = '_max' . \ucfirst(\strtolower($column_name)) . 'NameLength';
             //just keep compatible with listen name 
-            $column_name == 'socket' && $column_name = 'listen';
+            $column_name === 'socket' && $column_name = 'listen';
             $title.= "<w>{$column_name}</w>"  .  \str_pad('', static::$$key + static::UI_SAFE_LENGTH - \strlen($column_name));
         }
         $title && static::safeEcho($title . PHP_EOL);
@@ -759,7 +759,7 @@ class Worker
         if (static::$daemonize) {
             static::safeEcho("Input \"php $argv[0] stop\" to stop. Start success.\n\n");
         } else {
-            static::safeEcho("Press Ctrl+C to stop. Start success.\n");
+            static::safeEcho('Press Ctrl+C to stop. Start success.\n');
         }
     }
 
@@ -850,7 +850,7 @@ class Worker
 
         // Get master process PID.
         $master_pid      = \is_file(static::$pidFile) ? \file_get_contents(static::$pidFile) : 0;
-        $master_is_alive = $master_pid && \posix_kill($master_pid, 0) && \posix_getpid() != $master_pid;
+        $master_is_alive = $master_pid && \posix_kill($master_pid, 0) && \posix_getpid() !== $master_pid;
         // Master is still alive?
         if ($master_is_alive) {
             if ($command === 'start') {
@@ -1534,7 +1534,7 @@ class Worker
         }
 
         // Set uid and gid.
-        if ($uid != \posix_getuid() || $gid != \posix_getgid()) {
+        if ($uid !== \posix_getuid() || $gid !== \posix_getgid()) {
             if (!\posix_setgid($gid) || !\posix_initgroups($user_info['name'], $gid) || !\posix_setuid($uid)) {
                 static::log("Warning: change gid or uid fail.");
             }
@@ -2007,7 +2007,7 @@ class Worker
      */
     public static function checkErrors()
     {
-        if (static::STATUS_SHUTDOWN != static::$_status) {
+        if (static::STATUS_SHUTDOWN !== static::$_status) {
             $error_msg = static::$_OS === OS_TYPE_LINUX ? 'Worker['. \posix_getpid() .'] process terminated' : 'Worker process terminated';
             $errors    = error_get_last();
             if ($errors && ($errors['type'] === E_ERROR ||
@@ -2479,7 +2479,7 @@ class Worker
                     if(\method_exists($parser,'input')){
                         while($recv_buffer !== ''){
                             $len = $parser::input($recv_buffer, $connection);
-                            if($len == 0)
+                            if($len === 0)
                                 return true;
                             $package = \substr($recv_buffer,0,$len);
                             $recv_buffer = \substr($recv_buffer,$len);