C++;HipHop PHP编译器的输出看起来像什么? 是否足够干净,可以丢弃PHP和手工优化C++代码? < P>简短答案: NO>强/ > > < /P>

C++;HipHop PHP编译器的输出看起来像什么? 是否足够干净,可以丢弃PHP和手工优化C++代码? < P>简短答案: NO>强/ > > < /P>,php,performance,programming-languages,build,language-design,Php,Performance,Programming Languages,Build,Language Design,因为我花了一些时间让HipHop开始工作,所以我决定在这里分享我的成果,这样我就不会觉得自己完全是在浪费时间 以下是我的PHP输入: <? class test { function loop() { for($i=0; $i<10; ++$i) { echo("i = $i\n"); } } } $t = new test(); $t->loop(); ?> &中尉? 课堂测试{ 函数循环(){ 对于($i=0;$iloop(); ?> 这里是C++输

因为我花了一些时间让HipHop开始工作,所以我决定在这里分享我的成果,这样我就不会觉得自己完全是在浪费时间

以下是我的PHP输入:

<? class test { function loop() { for($i=0; $i<10; ++$i) { echo("i = $i\n"); } } } $t = new test(); $t->loop(); ?> &中尉? 课堂测试{ 函数循环(){ 对于($i=0;$iloop(); ?>

这里是C++输出…< /P> #include <php/hello.h> #include <cpp/ext/ext.h> namespace HPHP { /////////////////////////////////////////////////////////////////////////////// /* preface starts */ /* preface finishes */ /* SRC: hello.php line 3 */ Variant c_test::os_get(const char *s, int64 hash) { return c_ObjectData::os_get(s, hash); } Variant &c_test::os_lval(const char *s, int64 hash) { return c_ObjectData::os_lval(s, hash); } void c_test::o_get(ArrayElementVec &props) const { c_ObjectData::o_get(props); } bool c_test::o_exists(CStrRef s, int64 hash) const { return c_ObjectData::o_exists(s, hash); } Variant c_test::o_get(CStrRef s, int64 hash) { return c_ObjectData::o_get(s, hash); } Variant c_test::o_set(CStrRef s, int64 hash, CVarRef v,bool forInit /* = false */) { return c_ObjectData::o_set(s, hash, v, forInit); } Variant &c_test::o_lval(CStrRef s, int64 hash) { return c_ObjectData::o_lval(s, hash); } Variant c_test::os_constant(const char *s) { return c_ObjectData::os_constant(s); } IMPLEMENT_CLASS(test) ObjectData *c_test::cloneImpl() { c_test *obj = NEW(c_test)(); cloneSet(obj); return obj; } void c_test::cloneSet(c_test *clone) { ObjectData::cloneSet(clone); } Variant c_test::o_invoke(const char *s, CArrRef params, int64 hash, bool fatal) { if (hash < 0) hash = hash_string_i(s); switch (hash & 1) { case 1: HASH_GUARD(0x0EA59CD1566F5709LL, loop) { return (t_loop(), null); } break; default: break; } return c_ObjectData::o_invoke(s, params, hash, fatal); } Variant c_test::o_invoke_few_args(const char *s, int64 hash, int count, CVarRef a0, CVarRef a1, CVarRef a2, CVarRef a3, CVarR ef a4, CVarRef a5) { if (hash < 0) hash = hash_string_i(s); switch (hash & 1) { case 1: HASH_GUARD(0x0EA59CD1566F5709LL, loop) { return (t_loop(), null); } break; default: break; } return c_ObjectData::o_invoke_few_args(s, hash, count, a0, a1, a2, a3, a4, a5); } Variant c_test::os_invoke(const char *c, const char *s, CArrRef params, int64 hash, bool fatal) { return c_ObjectData::os_invoke(c, s, params, hash, fatal); } Variant cw_test$os_get(const char *s) { return c_test::os_get(s, -1); } Variant &cw_test$os_lval(const char *s) { return c_test::os_lval(s, -1); } Variant cw_test$os_constant(const char *s) { return c_test::os_constant(s); } Variant cw_test$os_invoke(const char *c, const char *s, CArrRef params, bool fatal /* = true */) { return c_test::os_invoke(c, s, params, -1, fatal); } void c_test::init() { } /* SRC: hello.php line 4 */ void c_test::t_loop() { INSTANCE_METHOD_INJECTION(test, test::loop); int64 v_i = 0; { LOOP_COUNTER(1); for ((v_i = 0LL); less(v_i, 10LL); ++v_i) { LOOP_COUNTER_CHECK(1); { echo((LINE(6,concat3("i = ", toString(v_i), "\n")))); } } } } /* function */ Object co_test(CArrRef params, bool init /* = true */) { return Object(p_test(NEW(c_test)())->dynCreate(params, init)); } Variant pm_php$hello_php(bool incOnce /* = false */, LVariableTable* variables /* = NULL */) { FUNCTION_INJECTION(run_init::hello.php); { DECLARE_GLOBAL_VARIABLES(g); bool &alreadyRun = g->run_pm_php$hello_php; if (alreadyRun) { if (incOnce) return true;} else alreadyRun = true; if (!variables) variables = g; } DECLARE_GLOBAL_VARIABLES(g); LVariableTable *gVariables __attribute__((__unused__)) = get_variable_table(); Variant &v_t __attribute__((__unused__)) = (variables != gVariables) ? variables->get("t") : g->GV(t); (v_t = ((Object)(LINE(11,p_test(p_test(NEWOBJ(c_test)())->create()))))); LINE(12,v_t.o_invoke_few_args("loop", 0x0EA59CD1566F5709LL, 0)); return true; } /* function */ /////////////////////////////////////////////////////////////////////////////// } #包括 #包括 名称空间HPHP{ /////////////////////////////////////////////////////////////////////////////// /*序言开始*/ /*序言结束*/ /*SRC:hello.php第3行*/ 变量c_测试::os_get(const char*s,int64散列){ 返回c_ObjectData::os_get(s,hash); } 变量和c_测试::os_lval(const char*s,int64散列){ 返回c_ObjectData::os_lval(s,hash); } 无效c_测试::o_获取(ArrayElementVec&props)常数{ c_ObjectData::o_get(props); } 布尔c_测试::o_存在(CStrRef s,int64散列)常量{ 返回c_ObjectData::o_存在(s,散列); } 变量c_测试::o_get(CStrRef s,int64散列){ 返回c_ObjectData::o_get(s,hash); } 变量c_测试::o_集(CStrRef s,int64散列,CVarRef v,bool forInit/*=false*/){ 返回c_ObjectData::o_集(s,hash,v,forInit); } 变量和c_测试::o_lval(CStrRef s,int64散列){ 返回c_ObjectData::o_lval(s,散列); } 变量c_测试::os_常量(const char*s){ 返回c_ObjectData::os_常量; } 实现类(测试) ObjectData*c_测试::cloneImpl(){ c_测试*obj=新(c_测试)(); cloneSet(obj); 返回obj; } 无效c_测试::克隆集(c_测试*克隆){ ObjectData::cloneSet(克隆); } 变量c_测试::o_调用(const char*s、CArrRef参数、int64哈希、bool fatal){ 如果(hash<0)hash=hash\u string\u i(s); 开关(散列和1){ 案例1: 哈希保护(0x0EA59CD1566F5709LL,循环){ 返回(t_loop(),null); } 打破 违约: 打破 } 返回c_ObjectData::o_invoke(s、params、hash、fatal); } 变量c_测试::o_调用几个参数(const char*s、int64 hash、int count、CVarRef a0、CVarRef a1、CVarRef a2、CVarRef a3、CVarR ef a4,CVarRef a5){ 如果(hash<0)hash=hash\u string\u i(s); 开关(散列和1){ 案例1: 哈希保护(0x0EA59CD1566F5709LL,循环){ 返回(t_loop(),null); } 打破 违约: 打破 } 返回c_ObjectData::o_invoke_几个参数(s、散列、计数、a0、a1、a2、a3、a4、a5); } 变量c_测试::os_调用(常量字符*c、常量字符*s、CArrRef参数、int64哈希、bool致命){ 返回c_ObjectData::os_invoke(c、s、params、hash、fatal); } 变量cw_测试$os_获取(常量字符*s){ 返回c_测试::os_get(s,-1); } 变量和连续测试$os\U lval(常量字符*s){ 返回c_测试::os_lval(s,-1); } 变量cw_测试$os_常量(常量字符*s){ 返回c_测试::os_常量; } 变量cw_测试$os_调用(常量字符*c,常量字符*s,CArrRef参数,bool fatal/*=true*/){ 返回c_test::os_invoke(c,s,params,-1,致命); } void c_测试::init(){ } /*SRC:hello.php第4行*/ 无效c_测试::t_循环(){ 实例\方法\注入(测试,测试::循环); int64 v_i=0; { 环路计数器(1); 对于((v_i=0LL);小于(v_i,10LL);+v_i){ 回路计数器检查(1); { 回声((第6行,concat3(“i=”,toString(v_i),“\n”))); } } } }/*函数*/ 对象co_测试(CArrRef参数,bool init/*=true*/){ 返回对象(p_测试(NEW(c_测试)()->dynCreate(params,init)); } 变量pm\u php$hello\u php(bool incoce/*=false*/,LVariableTable*变量/*=NULL*/){ 函数\注入(运行\初始化::hello.php); { 声明全局变量(g); bool&alreadyRun=g->run\u pm\u php$hello\u php; if(alreadyRun){if(incoce)返回true;} 否则alreadyRun=true; 如果(!variables)variables=g; } 声明全局变量(g); LVariableTable*gVariables_uuuu属性_uuu((uuu未使用的_uuu))=get_variable_table(); 变量&v_t___属性(未使用的变量)=(变量!=gVariables)?变量->获取(“t”):g->GV(t); (v_t=((对象)(第11行,p_测试(p_测试(NEWOBJ(c_测试)))->create())); 行(12,v_t.o_invoke_几个参数(“循环”,0x0EA59CD1566F5709LL,0)); 返回true; }/*函数*/ /////////////////////////////////////////////////////////////////////////////// } “我花了好几秒钟才找到循环……

< Stime>”,“从头开始写的C++程序员编写的代码和PHP代码的大小几乎相同(使用STL)。”>/P> 一个从头开始编写的G-WAN ANSI C脚本程序员会编写以下代码(效率更高):

static inline void loop(xbuf_t *reply) 
{
  int i = 0; 
  while(i < 10)
    xbuf_xcat(reply, "i = %d\n", i++);
}

int main(int argc, char *argv[])
{
   xbuf_t *reply = get_reply(argv);
   loop(reply);
   return 200;
}
静态内联无效循环(xbuf\u t*回复)
{
int i=0;
而(i<10)
xbuf_xcat(回复“i=%d\n”,i++);
}
int main(int argc,char*argv[])
{
xbuf_t*reply=get_reply(argv);
循环(回复);
返回200;
}
好奇者将对每个实现进行基准测试。真的好奇者将检查内存使用情况

甚至使用预先编译的C++的LeLtTPD或NGIX比G-WAN的C脚本慢。


思想食粮…

这似乎“正常”,因为似乎有很多代码来模拟php功能,所以您看到生成的大部分代码都是“绑定”代码(但这不是正确的词)对于这样的代码,你不应该感到惊讶,因为它是由完全不同的基于范式的语言生成的。从零开始编写的C++程序员将编写的代码与PHP代码段几乎相同大小(使用STL)。(但可以肯定的是,查看一些宏定义是必要的)。在我看来,HipHop开发人员并没有特意让输出可读。他们可以使用继承、模板和宏来减少显式“绑定”的数量代码。您是如何获得CPP输出的?我已经尝试过HHVM,但无法从中打赌CPP文件