Skip to content

Commit 3727e26

Browse files
committed
Improved zend_hash_clean() and added new optimized zend_symtable_clean()
1 parent fb42d9d commit 3727e26

File tree

4 files changed

+62
-11
lines changed

4 files changed

+62
-11
lines changed

Zend/zend_execute.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1421,12 +1421,12 @@ ZEND_API void execute_internal(zend_execute_data *execute_data, zval *return_val
14211421
ZEND_API void zend_clean_and_cache_symbol_table(zend_array *symbol_table TSRMLS_DC) /* {{{ */
14221422
{
14231423
if (EG(symtable_cache_ptr) >= EG(symtable_cache_limit)) {
1424-
zend_hash_destroy(&symbol_table->ht);
1424+
zend_array_destroy(&symbol_table->ht TSRMLS_CC);
14251425
efree_size(symbol_table, sizeof(zend_array));
14261426
} else {
14271427
/* clean before putting into the cache, since clean
14281428
could call dtors, which could use cached hash */
1429-
zend_hash_clean(&symbol_table->ht);
1429+
zend_symtable_clean(&symbol_table->ht TSRMLS_CC);
14301430
*(++EG(symtable_cache_ptr)) = symbol_table;
14311431
}
14321432
}

Zend/zend_hash.c

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -989,21 +989,71 @@ ZEND_API void zend_array_destroy(HashTable *ht TSRMLS_DC)
989989

990990
ZEND_API void zend_hash_clean(HashTable *ht)
991991
{
992-
uint32_t idx;
993-
Bucket *p;
992+
Bucket *p, *end;
994993

995994
IS_CONSISTENT(ht);
996995

997-
for (idx = 0; idx < ht->nNumUsed; idx++) {
998-
p = ht->arData + idx;
999-
if (Z_TYPE(p->val) == IS_UNDEF) continue;
996+
if (ht->nNumUsed) {
997+
p = ht->arData;
998+
end = p + ht->nNumUsed;
1000999
if (ht->pDestructor) {
1001-
ht->pDestructor(&p->val);
1000+
if (ht->u.flags & HASH_FLAG_PACKED) {
1001+
do {
1002+
if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1003+
ht->pDestructor(&p->val);
1004+
}
1005+
} while (++p != end);
1006+
} else {
1007+
do {
1008+
if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1009+
ht->pDestructor(&p->val);
1010+
if (EXPECTED(p->key)) {
1011+
zend_string_release(p->key);
1012+
}
1013+
}
1014+
} while (++p != end);
1015+
}
1016+
} else {
1017+
if (!(ht->u.flags & HASH_FLAG_PACKED)) {
1018+
do {
1019+
if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1020+
if (EXPECTED(p->key)) {
1021+
zend_string_release(p->key);
1022+
}
1023+
}
1024+
} while (++p != end);
1025+
}
10021026
}
1003-
if (p->key) {
1004-
zend_string_release(p->key);
1027+
}
1028+
ht->nNumUsed = 0;
1029+
ht->nNumOfElements = 0;
1030+
ht->nNextFreeElement = 0;
1031+
ht->nInternalPointer = INVALID_IDX;
1032+
if (ht->nTableMask) {
1033+
if (!(ht->u.flags & HASH_FLAG_PACKED)) {
1034+
memset(ht->arHash, INVALID_IDX, ht->nTableSize * sizeof(uint32_t));
10051035
}
10061036
}
1037+
}
1038+
1039+
ZEND_API void zend_symtable_clean(HashTable *ht TSRMLS_DC)
1040+
{
1041+
Bucket *p, *end;
1042+
1043+
IS_CONSISTENT(ht);
1044+
1045+
if (ht->nNumUsed) {
1046+
p = ht->arData;
1047+
end = p + ht->nNumUsed;
1048+
do {
1049+
if (EXPECTED(Z_TYPE(p->val) != IS_UNDEF)) {
1050+
i_zval_ptr_dtor(&p->val ZEND_FILE_LINE_CC TSRMLS_CC);
1051+
if (EXPECTED(p->key)) {
1052+
zend_string_release(p->key);
1053+
}
1054+
}
1055+
} while (++p != end);
1056+
}
10071057
ht->nNumUsed = 0;
10081058
ht->nNumOfElements = 0;
10091059
ht->nNextFreeElement = 0;

Zend/zend_hash.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ ZEND_API int zend_hash_rehash(HashTable *ht);
217217

218218
ZEND_API void zend_array_dup(HashTable *target, HashTable *source);
219219
ZEND_API void zend_array_destroy(HashTable *ht TSRMLS_DC);
220+
ZEND_API void zend_symtable_clean(HashTable *ht TSRMLS_DC);
220221

221222
#if ZEND_DEBUG
222223
/* debug */

Zend/zend_variables.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ ZEND_API void _zval_ptr_dtor_wrapper(zval *zval_ptr)
298298
{
299299
TSRMLS_FETCH();
300300

301-
i_zval_ptr_dtor(zval_ptr ZEND_FILE_LINE_RELAY_CC TSRMLS_CC);
301+
i_zval_ptr_dtor(zval_ptr ZEND_FILE_LINE_CC TSRMLS_CC);
302302
}
303303

304304

0 commit comments

Comments
 (0)