php的数组和spl固定数组
php固定数组隶属于php标准库(spl)的一种数据结构。和php普通数组相比,固定数组只能用整形定义其下标,并且如名字所示,是固定长度,它的优点是比普通数组占用的内存少,而且更快速,具体原因下文会做分析,先做一个简单的测试,将10w个a放入到数组中。
define(max, 100000);//simple arrayfunction simple_arr(){ $i = max; $arr = array(); while ($i--) $arr[$i]= 'a';}// fix arrayfunction fix_arr(){ $i = max; $arr = new splfixedarray(max); while ($i--) $arr[$i]= 'a'; }//fix array with setfunction fix_set_arr(){ $i = max; $arr = new splfixedarray(max); while ($i--) $arr->offsetset($i, a);}
时间消耗
一般数组:0.084696054458618
固定数组:0.048405885696411
固定数组调用offsetset方法复制:0.27650499343872
内存消耗
一般数组:9324672
固定数组:4800464
固定数组调用offsetset方法复制:4800344
空间消耗对比
从空间和时间的效率来看,固定数组的消耗都比一般数组少了很可观。固定数组通过扩展中的内置函数offsetset赋值比通过下标赋值时间慢的多,这个因为用扩展中的内置方法斤数组赋值,php内部需要多一次函数表的查询。
在空间方面,对一般数组,php内部是通过hashtable来存储,hashtable中的每一个槽位对应数组中的一个值,在php源码zend/zend_hash.h中定义了hash相关的结构体定义和函数。
typedef struct bucket { ulong h; /* used for numeric indexing */ uint nkeylength; void *pdata; void *pdataptr; struct bucket *plistnext; struct bucket *plistlast; struct bucket *pnext; struct bucket *plast; const char *arkey;} bucket;typedef struct _hashtable { uint ntablesize; uint ntablemask; uint nnumofelements; ulong nnextfreeelement; bucket *pinternalpointer; /* used for element traversal */ bucket *plisthead; bucket *plisttail; bucket **arbuckets; dtor_func_t pdestructor; zend_bool persistent; unsigned char napplycount; zend_bool bapplyprotection;#if zend_debug int inconsistent;#endif} hashtable
如上面代码所示,一个10个元素的php数组所站的空间是sizeof(hashtable) + 10 * size(bucket) + 元素本身占用空间,这是代码层面的算术,其实在php内部会复杂一点,hashtable的ntablesize永远是2^n,所以即使是10个元素,php内部通过 简单算法能实现占用2^4,也即16个槽位,所以实际占用空间是sizeof(hashtable) + 16 * sizeof(bucket) + 元素本身占用空间。(空间的计算只考虑下标是整数的情况下)
而对应固定数组直接通过用户传人的size大小初始化数组,如下面代码所示:同样10个元素的数组,所需要的空间只有10* 元素本身占用空间。
static void spl_fixedarray_init(spl_fixedarray *array, long size tsrmls_dc) /* {{{ */{ if (size > 0) { array->size = 0; /* reset size in case ecalloc() fails */ array->elements = ecalloc(size, sizeof(zval *)); array->size = size; } else { array->elements = null; array->size = 0; }}
时间方面对比
对于固定数组来说,对内存的申请一步到位了,当内存不够时候会报错,当内存用不完时,也就浪费在那里。
对于普通数组,因为是动态分配数组空间,由于预先不知道要有多少元素,php初始化空数组的的时候,默认8个槽位,但槽位不够的时候,会再分配*2的空间,当元素元素的数量大于hashtbale中的ntablesize的时候,会resize和rehash hashtable,在resize和rehash的过程中,时间的消耗相当可观了。
static int zend_hash_do_resize(hashtable *ht){ bucket **t;#ifdef zend_signals tsrmls_fetch();#endif is_consistent(ht); if ((ht->ntablesize 0) { /* let's double the table size */ t = (bucket **) perealloc_recoverable(ht->arbuckets, (ht->ntablesize persistent); if (t) { handle_block_interruptions(); ht->arbuckets = t; ht->ntablesize = (ht->ntablesize ntablemask = ht->ntablesize - 1; zend_hash_rehash(ht); handle_unblock_interruptions(); return success; } return failure; } return success;}zend_api int zend_hash_rehash(hashtable *ht){ bucket *p; uint nindex; is_consistent(ht); if (unexpected(ht->nnumofelements == 0)) { return success; } memset(ht->arbuckets, 0, ht->ntablesize * sizeof(bucket *)); p = ht->plisthead; while (p != null) { nindex = p->h & ht->ntablemask; connect_to_bucket_dllist(p, ht->arbuckets[nindex]); ht->arbuckets[nindex] = p; p = p->plistnext; } return success;}
通过对比发现php普通数组的内存和时间消耗和固定数组相比大部分都用在了符号表上。php内部实现的一个重要思想是通过空间来换取时间,用hashtable能够快速定位到数据的元素,它甚至把hashtable设计成双向链表,又因为php数组空间是动态分配的,而内部是用c实现的,c语言对数组空间分配只有固定分配,为了实现让用户感觉php数组是动态分配空间的,只能通过resize和rehash实现,所以和固定数组比,相同数量的元素赋值,时间就变慢了。总之,普通数组和固定数组各有优劣,不能说固定数组时间和空间消耗低就是后,具体的情况需要考虑到具体的业务场景。