Skip to content

Commit

Permalink
optimize the performance for check Generator class_exists, 5x increase.
Browse files Browse the repository at this point in the history
  • Loading branch information
breath-co2 committed Apr 4, 2018
1 parent 424e455 commit 3027684
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 13 deletions.
2 changes: 1 addition & 1 deletion src/Hprose/Future/CallableWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CallableWrapper extends Wrapper {
public function __invoke() {
$obj = $this->obj;
return all(func_get_args())->then(function($args) use ($obj) {
if (class_exists("\\Generator")) {
if (HaveGenerator) {
return co(call_user_func_array($obj, $args));
}
return call_user_func_array($obj, $args);
Expand Down
4 changes: 1 addition & 3 deletions src/Hprose/Future/Wrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@

namespace Hprose\Future;

use ReflectionMethod;

class Wrapper {
protected $obj;
public function __construct($obj) {
Expand All @@ -31,7 +29,7 @@ public function __construct($obj) {
public function __call($name, array $arguments) {
$method = array($this->obj, $name);
return all($arguments)->then(function($args) use ($method, $name) {
if (class_exists("\\Generator")) {
if (HaveGenerator) {
return co(call_user_func_array($method, $args));
}
return call_user_func_array($method, $args);
Expand Down
12 changes: 5 additions & 7 deletions src/Hprose/HandlerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,12 @@ abstract class HandlerManager {
protected $invokeHandler;
protected $beforeFilterHandler;
protected $afterFilterHandler;
public static $haveGenerator;
public function __construct() {
$self = $this;
self::$haveGenerator = class_exists("\\Generator", false);
$this->defaultInvokeHandler = function(/*string*/ $name, array &$args, stdClass $context) use ($self) {
try {
$result = $self->invokeHandler($name, $args, $context);
if (HandlerManager::$haveGenerator) {
if (HaveGenerator) {
return Future\co($result);
}
else {
Expand All @@ -58,7 +56,7 @@ public function __construct() {
$this->defaultBeforeFilterHandler = function(/*string*/ $request, stdClass $context) use ($self) {
try {
$result = $self->beforeFilterHandler($request, $context);
if (HandlerManager::$haveGenerator) {
if (HaveGenerator) {
return Future\co($result);
}
else {
Expand All @@ -75,7 +73,7 @@ public function __construct() {
$this->defaultAfterFilterHandler = function(/*string*/ $request, stdClass $context) use ($self) {
try {
$result = $self->afterFilterHandler($request, $context);
if (HandlerManager::$haveGenerator) {
if (HaveGenerator) {
return Future\co($result);
}
else {
Expand Down Expand Up @@ -116,7 +114,7 @@ protected function getNextInvokeHandler(Closure $next, /*callable*/ $handler) {
try {
$array = array($name, &$args, $context, $next);
$result = call_user_func_array($handler, $array);
if (HandlerManager::$haveGenerator) {
if (HaveGenerator) {
return Future\co($result);
}
else {
Expand All @@ -135,7 +133,7 @@ protected function getNextFilterHandler(Closure $next, /*callable*/ $handler) {
return function(/*string*/ $request, stdClass $context) use ($next, $handler) {
try {
$result = call_user_func($handler, $request, $context, $next);
if (HandlerManager::$haveGenerator) {
if (HaveGenerator) {
return Future\co($result);
}
else {
Expand Down
2 changes: 1 addition & 1 deletion src/Hprose/Service.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ public function addFunction($func, $alias = '', array $options = array()) {
if (!array_key_exists($name, $this->calls)) {
$this->names[] = $alias;
}
if (class_exists("\\Generator")) {
if (HaveGenerator) {
if (is_array($func)) {
$f = new ReflectionMethod($func[0], $func[1]);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Hprose/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,5 @@
function deferred() {
return new Deferred();
}

define('HaveGenerator', class_exists("\\Generator", false));
2 changes: 1 addition & 1 deletion src/init.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

require_once __DIR__ . '/Throwable.php';
require_once __DIR__ . '/TypeError.php';
require_once __DIR__ . '/Hprose/functions.php';
require_once __DIR__ . '/Hprose/Future/functions.php';
require_once __DIR__ . '/Hprose/Promise/functions.php';
require_once __DIR__ . '/Hprose/functions.php';
require_once __DIR__ . '/functions.php';

// Deprecated
Expand Down

0 comments on commit 3027684

Please sign in to comment.