Skip to content

Commit

Permalink
优化底层的细节,提升性能
Browse files Browse the repository at this point in the history
  • Loading branch information
bingcool committed Jun 14, 2019
1 parent 071764e commit 05a04f7
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 45 deletions.
3 changes: 2 additions & 1 deletion score/Core/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ public static function setApp($App) {
return true;
}
}else {
// process进程中,本身不产生协程,默认返回-1,但可以使用go()创建协程,创建应用单例,单例继承于ProcessController类
// process进程中,本身不产生协程,默认返回-1,可以通过设置第四个参数enable_coroutine = true启用协程
// 同时可以使用go()创建协程,创建应用单例,单例继承于ProcessController类
if($App instanceof \Swoolefy\Core\Process\ProcessController || $App instanceof \Swoolefy\Core\EventController) {
$cid = $App->coroutine_id;
if(isset(self::$app[$cid])) {
Expand Down
1 change: 0 additions & 1 deletion score/Core/EventApp.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,6 @@ public function __call(string $action, $args = []) {
$this->is_call = true;
}
}catch(\Throwable $t) {
self::__destruct();
throw new \Exception($t->getMessage());
}
}
Expand Down
18 changes: 12 additions & 6 deletions score/Core/Hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,33 @@ public static function addHook($type, $func, $prepend = false) {
* @param int $type
* @return void
*/
public static function callHook($type) {
$cid = CoroutineManager::getInstance()->getCoroutineId();
public static function callHook($type, $cid = null) {
if(empty($cid)) {
$cid = CoroutineManager::getInstance()->getCoroutineId();
}
if(isset(self::$hooks[$cid][$type])) {
foreach(self::$hooks[$cid][$type] as $func) {
$func();
}
}
// afterRequest钩子是目前应用实例生命周期最后执行的,这里直接将该协程的所有钩子函数都unset
// afterRequest钩子是目前应用实例生命周期最后执行的,直接将该协程的所有钩子函数都unset
if($type == self::HOOK_AFTER_REQUEST && isset(self::$hooks[$cid])) {
unset(self::$hooks[$cid]);
}
}

/**
* getHookCallable 获取所有的钩子函数
* @param int $cid
* @return array
*/
public static function getHookCallable($cid = null) {
if($cid) {
return self::$hooks[$cid];
if(empty($cid)) {
$cid = CoroutineManager::getInstance()->getCoroutineId();
}
return self::$hooks;
if(isset(self::$hooks[$cid])) {
return self::$hooks[$cid];
}
return null;
}
}
38 changes: 10 additions & 28 deletions score/Core/ProcessPools/PoolsManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ class PoolsManager {
'size' => self::PROCESS_NUM,
// 字段
'fields'=> [
//从4.3版本开始,底层对内存长度做了对齐处理。字符串长度必须为8的整数倍。如长度为18会自动对齐到24字节
['pid','int', 10],
['process_name','string', 50],
['process_name','string', 56],
]
]
];
Expand Down Expand Up @@ -82,33 +83,14 @@ public static function addProcessPools(string $processName, string $processClass
throw new \Exception("you can not add the same process : $processName", 1);
}

if(isset($args[0]) && $args[0] instanceof \Swoole\Channel) {

if(self::$worker_num != count($args)) {
throw new \Exception("args of channel object num must == worker_num", 1);
}

for($i = 0; $i < self::$worker_num; $i++) {
for($j = 0; $j < $process_num_bind_worker; $j++) {
try{
$process = new $processClass($processName.'@'.$i.'@'.$j, $async, [$args[$i]], $extend_data, $enable_coroutine);
$process->setBindWorkerId($i);
self::$processList[$key][$i][$j] = $process;
}catch (\Exception $e){
throw new \Exception($e->getMessage(), 1);
}
}
}
}else {
for($i = 0; $i < self::$worker_num; $i++) {
for($j = 0; $j < $process_num_bind_worker; $j++) {
try{
$process = new $processClass($processName.'@'.$i.'@'.$j, $async, $args, $extend_data, $enable_coroutine);
$process->setBindWorkerId($i);
self::$processList[$key][$i][$j] = $process;
}catch (\Exception $e){
throw new \Exception($e->getMessage(), 1);
}
for($i = 0; $i < self::$worker_num; $i++) {
for($j = 0; $j < $process_num_bind_worker; $j++) {
try{
$process = new $processClass($processName.'@'.$i.'@'.$j, $async, $args, $extend_data, $enable_coroutine);
$process->setBindWorkerId($i);
self::$processList[$key][$i][$j] = $process;
}catch (\Exception $e){
throw new \Exception($e->getMessage(), 1);
}
}
}
Expand Down
14 changes: 7 additions & 7 deletions score/Core/Table/TableManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,20 @@ public static function createTable(array $tables = []) {
if(isset($swoole_tables[$table_name])) {
continue;
}
$table = new \swoole_table($row['size']);
$table = new \Swoole\Table($row['size']);
foreach($row['fields'] as $p => $field) {
switch(strtolower($field[1])) {
case 'int':
case \swoole_table::TYPE_INT:
$table->column($field[0], \swoole_table::TYPE_INT, (int)$field[2]);
case \Swoole\Table::TYPE_INT:
$table->column($field[0], \Swoole\Table::TYPE_INT, (int)$field[2]);
break;
case 'string':
case \swoole_table::TYPE_STRING:
$table->column($field[0], \swoole_table::TYPE_STRING, (int)$field[2]);
case \Swoole\Table::TYPE_STRING:
$table->column($field[0], \Swoole\Table::TYPE_STRING, (int)$field[2]);
break;
case 'float':
case \swoole_table::TYPE_FLOAT:
$table->column($field[0], \swoole_table::TYPE_FLOAT, (int)$field[2]);
case \Swoole\Table::TYPE_FLOAT:
$table->column($field[0], \Swoole\Table::TYPE_FLOAT, (int)$field[2]);
break;
}
}
Expand Down
6 changes: 4 additions & 2 deletions score/Core/ZModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,10 @@ public static function getInstance(string $class = '', array $constructor = [])
* removeInstance 删除某个协程下的所有创建的model实例
* @return boolean
*/
public static function removeInstance() {
$cid = CoroutineManager::getInstance()->getCoroutineId();
public static function removeInstance($cid = null) {
if(empty($cid)) {
$cid = CoroutineManager::getInstance()->getCoroutineId();
}
if(isset(static::$_model_instances[$cid])) {
unset(static::$_model_instances[$cid]);
}
Expand Down

0 comments on commit 05a04f7

Please sign in to comment.