Skip to content

Commit f51602e

Browse files
committed
[optimization] php&c stack reuse
1 parent 9a2da60 commit f51602e

File tree

6 files changed

+37
-21
lines changed

6 files changed

+37
-21
lines changed

coroutine/Coroutine.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,11 @@ Coroutine::Coroutine(run_func func,ZendFunction *args)
99
{
1010
callback = args;
1111
ctx = new Context(func,static_cast<void *>(this));
12-
creator = (void***)tsrm_get_ls_cache();
12+
creator = args->creator;
1313
}
1414
/**
1515
* 获取一个G
16-
* 1. 从本地空闲free_stack中获取一个
17-
* 2. 没有空闲的则new一个新的G
16+
* 从本地空闲free_stack中获取一个
1817
* @return Corouine*
1918
*/
2019
Coroutine* Coroutine::getg(ZendFunction* fn)
@@ -32,7 +31,11 @@ Coroutine* Coroutine::getg(ZendFunction* fn)
3231
co->ctx->reset();
3332
co->callback = fn;
3433
co->gstatus = Gidle;
34+
co->creator = fn->creator;
35+
}else{
36+
co = new Coroutine(PHPCoroutine::run,fn);
3537
}
38+
co->gstatus = Gidle;
3639
return co;
3740
}
3841
/**
@@ -43,8 +46,8 @@ long Coroutine::run()
4346
{
4447
Debug("G run: start push g to global queue g:%x ctx:%x", this, ctx);
4548
// 投递到 proc 线程去执行该协程
46-
if(proc == nullptr)Error("proc not init");
47-
proc->gogo(ctx);
49+
// if(proc == nullptr)Error("proc not init");
50+
// proc->gogo(ctx);
4851
return 1;
4952
}
5053
/**

coroutine/PHPCoroutine.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@
1111
long PHPCoroutine::go(zend_function *func,zval *argv,uint32_t argc)
1212
{
1313
ZendFunction *call = new ZendFunction(func,argv,argc);
14-
Coroutine* ctx = Coroutine::getg(call);
15-
if(ctx){
16-
GO_ZG(runq)->push(ctx);
17-
return 1;
18-
}
19-
ctx = new Coroutine(run,call);
20-
proc->gogo(ctx->ctx);
14+
// Coroutine* ctx = Coroutine::getg(call);
15+
// if(ctx){
16+
// GO_ZG(runq)->push(ctx);
17+
// return 1;
18+
// }
19+
// ctx = new Coroutine(run,call);
20+
// proc->gogo(ctx->ctx);
21+
proc->gogo(call);
2122
// return ctx->run();
2223
return 1;
2324
}

runtime/Proc.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ vector<M> allm;
1212
* 投递一个协程给多个线程调度执行
1313
* @param ctx
1414
*/
15-
void Proc::gogo(Context* ctx)
15+
void Proc::gogo(ZendFunction* ctx)
1616
{
1717
assert(!stop);
1818
unique_lock<mutex> lock(queue_mu);
@@ -72,8 +72,8 @@ void Proc::prepare_shutdown()
7272
*/
7373
void Proc::schedule()
7474
{
75-
Context* ctx;
76-
Coroutine* co;
75+
Coroutine* co;
76+
ZendFunction* fn;
7777
queue<Coroutine*> *runq = GO_ZG(runq);
7878
for(;;){
7979
{
@@ -86,9 +86,9 @@ void Proc::schedule()
8686
else continue;
8787

8888
if(!this->tasks.empty()){
89-
ctx = move(this->tasks.front());
89+
fn = move(this->tasks.front());
9090
this->tasks.pop();
91-
co = static_cast<Coroutine *>(ctx->func_data);
91+
co = Coroutine::getg(fn);
9292
}else{
9393
co = move(runq->front());
9494
runq->pop();
@@ -104,7 +104,7 @@ void Proc::schedule()
104104
//恢复被暂停的G
105105
else co->resume();
106106
//G运行结束 销毁栈
107-
if(ctx->is_end)
107+
if(co->ctx->is_end)
108108
{
109109
Debug("coroutine end: start close");
110110
co->close();

runtime/Proc.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ class Proc
3737
public:
3838
Proc(size_t threads);
3939
~Proc();
40-
// void gogo(ZendFunction *call);
41-
void gogo(Context *ctx);
40+
void gogo(ZendFunction *call);
41+
// void gogo(Context *ctx);
4242
void preapre_start();
4343
void schedule();
4444
void prepare_shutdown();
@@ -48,7 +48,8 @@ class Proc
4848
size_t threads;
4949
size_t start_threads = 0;
5050
condition_variable cond;
51-
queue<Context *> tasks;
51+
// queue<Context *> tasks;
52+
queue<ZendFunction *> tasks;
5253
time_point now;
5354
bool ready;
5455

runtime/ZendFunction.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,21 @@ void ZendFunction::freehash(zval *zval_ptr)
1313
// }
1414
zval_ptr_dtor(zval_ptr);
1515
}
16+
/**
17+
*
18+
* @param func
19+
* @param argv
20+
* @param argc
21+
*/
1622
ZendFunction::ZendFunction(zend_function *func,zval *argv,uint32_t argc):argv(argv),argc(argc),is_new(1)
1723
{
1824
arena_checkpoint = zend_arena_checkpoint(CG(arena));;
1925
this->func = copy_function(func);
26+
creator = (void***)tsrm_get_ls_cache();
2027
}
28+
/**
29+
* free all the env
30+
*/
2131
ZendFunction::~ZendFunction()
2232
{
2333
zend_op_array *op = &func->op_array;

runtime/ZendFunction.h

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ class ZendFunction
1818
zval *argv;
1919
uint32_t argc;
2020
int is_new = 0;
21+
void*** creator;
2122
public:
2223
ZendFunction(zend_function *func,zval *argv,uint32_t argc);
2324
~ZendFunction();

0 commit comments

Comments
 (0)