函数名的长度会影响性能
php函数的实现原理:
php实现了一个典型的动态语言执行过程:拿到一段代码后,经过词法解析、语法解析等阶段后,
源程序会被翻译成一个个指令(opcodes),然后ZEND虚拟机顺次执行这些指令完成操作。Php本身是用c实现的,
因此最终调用的也都是c的函数,实际上,我们可以把php看做是一个c开发的软件。
通过上面描述不难看出,php中函数的执行也是被翻译成了opcodes来调用,每次函数调用实际上是执行了一条或多条指令。对于每一个函数,zend都通过以下的数据结构来描述
typedef union _zend_function {
zend_uchar type;
struct {
zend_uchar type;
char *function_name;
zend_class_entry *scope;
zend_uint fn_flags;
union _zend_function *prototype;
zend_uint num_args;
zend_uint required_num_args;
zend_arg_info *arg_info;
zend_bool pass_rest_by_reference;
unsigned char return_reference;
} common;
zend_op_array op_array;
zend_internal_function internal_function;
} zend_function;
typedef struct _zend_function_state {
HashTable *function_symbol_table;
zend_function *function;
void *reserved[ZEND_MAX_RESERVED_RESOURCES];
} zend_function_state;
其中type标明了函数的类型:用户函数、内置函数、重载函数。Common中包含函数的基本信息,包括函数名,参数信息,
函数标志(普通函数、静态方法、抽象方法)等内容。另外,对于用户函数,还有一个函数符号表,记录了内部变量等,这个将在后面详述。
Zend维护了一个全局function_table,这是一个大的hash表。函数调用的时候会首先根据函数名从表中找到对应的zend_function。
当进行函数调用时候,虚拟机会根据type的不同决定调用方法
函数调用的时候zend会先在一个全局的funtion_table中通过函数名查询相关信息,
function_table是一个哈希表。必然的,名字越长查询所需要的时间就越多。