Use more compact representation for packed arrays.

- for packed arrays we store just an array of zvals without keys.
- the elements of packed array are accessible throuf as ht->arPacked[i]
  instead of ht->arData[i]
- in addition to general ZEND_HASH_FOREACH_* macros, we introduced similar
  familied for packed (ZEND_HASH_PACKED_FORECH_*) and real hashes
  (ZEND_HASH_MAP_FOREACH_*)
- introduced an additional family of macros to access elements of array
  (packed or real hashes) ZEND_ARRAY_ELEMET_SIZE, ZEND_ARRAY_ELEMET_EX,
  ZEND_ARRAY_ELEMET, ZEND_ARRAY_NEXT_ELEMENT, ZEND_ARRAY_PREV_ELEMENT
- zend_hash_minmax() prototype was changed to compare only values

Because of smaller data set, this patch may show performance improvement
on some apps and benchmarks that use packed arrays. (~1% on PHP-Parser)

TODO:
    - sapi/phpdbg needs special support for packed arrays (WATCH_ON_BUCKET).
    - zend_hash_sort_ex() may require converting packed arrays to hash.
This commit is contained in:
Dmitry Stogov 2021-11-03 15:18:26 +03:00
parent 0eb603e3bb
commit 90b7bde615
89 changed files with 3302 additions and 1664 deletions

View file

@ -299,8 +299,6 @@ static void zend_file_cache_serialize_hash(HashTable *ht,
void *buf,
serialize_callback_t func)
{
Bucket *p, *end;
if (HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED) {
ht->arData = NULL;
return;
@ -308,16 +306,33 @@ static void zend_file_cache_serialize_hash(HashTable *ht,
if (IS_SERIALIZED(ht->arData)) {
return;
}
SERIALIZE_PTR(ht->arData);
p = ht->arData;
UNSERIALIZE_PTR(p);
end = p + ht->nNumUsed;
while (p < end) {
if (Z_TYPE(p->val) != IS_UNDEF) {
SERIALIZE_STR(p->key);
func(&p->val, script, info, buf);
if (HT_IS_PACKED(ht)) {
zval *p, *end;
SERIALIZE_PTR(ht->arPacked);
p = ht->arPacked;
UNSERIALIZE_PTR(p);
end = p + ht->nNumUsed;
while (p < end) {
if (Z_TYPE_P(p) != IS_UNDEF) {
func(p, script, info, buf);
}
p++;
}
} else {
Bucket *p, *end;
SERIALIZE_PTR(ht->arData);
p = ht->arData;
UNSERIALIZE_PTR(p);
end = p + ht->nNumUsed;
while (p < end) {
if (Z_TYPE(p->val) != IS_UNDEF) {
SERIALIZE_STR(p->key);
func(&p->val, script, info, buf);
}
p++;
}
p++;
}
}
@ -1107,8 +1122,6 @@ static void zend_file_cache_unserialize_hash(HashTable *ht,
unserialize_callback_t func,
dtor_func_t dtor)
{
Bucket *p, *end;
ht->pDestructor = dtor;
if (HT_FLAGS(ht) & HASH_FLAG_UNINITIALIZED) {
if (EXPECTED(!file_cache_only)) {
@ -1122,14 +1135,29 @@ static void zend_file_cache_unserialize_hash(HashTable *ht,
return;
}
UNSERIALIZE_PTR(ht->arData);
p = ht->arData;
end = p + ht->nNumUsed;
while (p < end) {
if (Z_TYPE(p->val) != IS_UNDEF) {
UNSERIALIZE_STR(p->key);
func(&p->val, script, buf);
if (HT_IS_PACKED(ht)) {
zval *p, *end;
p = ht->arPacked;
end = p + ht->nNumUsed;
while (p < end) {
if (Z_TYPE_P(p) != IS_UNDEF) {
func(p, script, buf);
}
p++;
}
} else {
Bucket *p, *end;
p = ht->arData;
end = p + ht->nNumUsed;
while (p < end) {
if (Z_TYPE(p->val) != IS_UNDEF) {
UNSERIALIZE_STR(p->key);
func(&p->val, script, buf);
}
p++;
}
p++;
}
}