Skip to content

Commit 4df89a3

Browse files
committed
Eliminate useless checks
1 parent cb464a5 commit 4df89a3

File tree

2 files changed

+55
-13
lines changed

2 files changed

+55
-13
lines changed

Zend/zend_hash.h

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,14 +1099,14 @@ static zend_always_inline void *zend_hash_get_current_data_ptr_ex(HashTable *ht,
10991099
__fill_ht->nInternalPointer = 0; \
11001100
} while (0)
11011101

1102-
static zend_always_inline zval *_zend_hash_append(HashTable *ht, zend_string *key, zval *zv)
1102+
static zend_always_inline zval *_zend_hash_append_ex(HashTable *ht, zend_string *key, zval *zv, int interned)
11031103
{
11041104
uint32_t idx = ht->nNumUsed++;
11051105
uint32_t nIndex;
11061106
Bucket *p = ht->arData + idx;
11071107

11081108
ZVAL_COPY_VALUE(&p->val, zv);
1109-
if (!ZSTR_IS_INTERNED(key)) {
1109+
if (!interned && !ZSTR_IS_INTERNED(key)) {
11101110
HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
11111111
zend_string_addref(key);
11121112
zend_string_hash_val(key);
@@ -1116,19 +1116,23 @@ static zend_always_inline zval *_zend_hash_append(HashTable *ht, zend_string *ke
11161116
nIndex = (uint32_t)p->h | ht->nTableMask;
11171117
Z_NEXT(p->val) = HT_HASH(ht, nIndex);
11181118
HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
1119-
ht->nNumUsed = idx + 1;
11201119
ht->nNumOfElements++;
11211120
return &p->val;
11221121
}
11231122

1124-
static zend_always_inline zval *_zend_hash_append_ptr(HashTable *ht, zend_string *key, void *ptr)
1123+
static zend_always_inline zval *_zend_hash_append(HashTable *ht, zend_string *key, zval *zv)
1124+
{
1125+
return _zend_hash_append_ex(ht, key, zv, 0);
1126+
}
1127+
1128+
static zend_always_inline zval *_zend_hash_append_ptr_ex(HashTable *ht, zend_string *key, void *ptr, int interned)
11251129
{
11261130
uint32_t idx = ht->nNumUsed++;
11271131
uint32_t nIndex;
11281132
Bucket *p = ht->arData + idx;
11291133

11301134
ZVAL_PTR(&p->val, ptr);
1131-
if (!ZSTR_IS_INTERNED(key)) {
1135+
if (!interned && !ZSTR_IS_INTERNED(key)) {
11321136
HT_FLAGS(ht) &= ~HASH_FLAG_STATIC_KEYS;
11331137
zend_string_addref(key);
11341138
zend_string_hash_val(key);
@@ -1138,11 +1142,15 @@ static zend_always_inline zval *_zend_hash_append_ptr(HashTable *ht, zend_string
11381142
nIndex = (uint32_t)p->h | ht->nTableMask;
11391143
Z_NEXT(p->val) = HT_HASH(ht, nIndex);
11401144
HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
1141-
ht->nNumUsed = idx + 1;
11421145
ht->nNumOfElements++;
11431146
return &p->val;
11441147
}
11451148

1149+
static zend_always_inline zval *_zend_hash_append_ptr(HashTable *ht, zend_string *key, void *ptr)
1150+
{
1151+
return _zend_hash_append_ptr_ex(ht, key, ptr, 0);
1152+
}
1153+
11461154
static zend_always_inline void _zend_hash_append_ind(HashTable *ht, zend_string *key, zval *ptr)
11471155
{
11481156
uint32_t idx = ht->nNumUsed++;
@@ -1160,7 +1168,6 @@ static zend_always_inline void _zend_hash_append_ind(HashTable *ht, zend_string
11601168
nIndex = (uint32_t)p->h | ht->nTableMask;
11611169
Z_NEXT(p->val) = HT_HASH(ht, nIndex);
11621170
HT_HASH(ht, nIndex) = HT_IDX_TO_HASH(idx);
1163-
ht->nNumUsed = idx + 1;
11641171
ht->nNumOfElements++;
11651172
}
11661173

ext/opcache/zend_accelerator_util_funcs.c

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ static void zend_accel_function_hash_copy_from_shm(HashTable *target, HashTable
533533
goto failure;
534534
}
535535
} else {
536-
_zend_hash_append_ptr(target, p->key, ARENA_REALLOC(Z_PTR(p->val)));
536+
_zend_hash_append_ptr_ex(target, p->key, ARENA_REALLOC(Z_PTR(p->val)), 1);
537537
}
538538
}
539539
target->nInternalPointer = 0;
@@ -556,7 +556,7 @@ static void zend_accel_function_hash_copy_from_shm(HashTable *target, HashTable
556556
}
557557
}
558558

559-
static void zend_accel_class_hash_copy(HashTable *target, HashTable *source, unique_copy_ctor_func_t pCopyConstructor)
559+
static void zend_accel_class_hash_copy(HashTable *target, HashTable *source)
560560
{
561561
Bucket *p, *end;
562562
zval *t;
@@ -587,9 +587,44 @@ static void zend_accel_class_hash_copy(HashTable *target, HashTable *source, uni
587587
}
588588
} else {
589589
t = _zend_hash_append_ptr(target, p->key, Z_PTR(p->val));
590-
if (pCopyConstructor) {
591-
pCopyConstructor(&Z_PTR_P(t));
590+
}
591+
}
592+
target->nInternalPointer = 0;
593+
return;
594+
}
595+
596+
static void zend_accel_class_hash_copy_from_shm(HashTable *target, HashTable *source)
597+
{
598+
Bucket *p, *end;
599+
zval *t;
600+
601+
zend_hash_extend(target, target->nNumUsed + source->nNumUsed, 0);
602+
p = source->arData;
603+
end = p + source->nNumUsed;
604+
for (; p != end; p++) {
605+
ZEND_ASSERT(Z_TYPE(p->val) != IS_UNDEF);
606+
ZEND_ASSERT(p->key);
607+
t = zend_hash_find_ex(target, p->key, 1);
608+
if (UNEXPECTED(t != NULL)) {
609+
if (EXPECTED(ZSTR_LEN(p->key) > 0) && EXPECTED(ZSTR_VAL(p->key)[0] == 0)) {
610+
/* Mangled key - ignore and wait for runtime */
611+
continue;
612+
} else if (UNEXPECTED(!ZCG(accel_directives).ignore_dups)) {
613+
zend_class_entry *ce1 = Z_PTR(p->val);
614+
if (!(ce1->ce_flags & ZEND_ACC_ANON_CLASS)) {
615+
CG(in_compilation) = 1;
616+
zend_set_compiled_filename(ce1->info.user.filename);
617+
CG(zend_lineno) = ce1->info.user.line_start;
618+
zend_error(E_ERROR,
619+
"Cannot declare %s %s, because the name is already in use",
620+
zend_get_object_type(ce1), ZSTR_VAL(ce1->name));
621+
return;
622+
}
623+
continue;
592624
}
625+
} else {
626+
t = _zend_hash_append_ptr_ex(target, p->key, Z_PTR(p->val), 1);
627+
zend_class_copy_ctor((zend_class_entry**)&Z_PTR_P(t));
593628
}
594629
}
595630
target->nInternalPointer = 0;
@@ -771,7 +806,7 @@ zend_op_array* zend_accel_load_script(zend_persistent_script *persistent_script,
771806

772807
/* Copy all the necessary stuff from shared memory to regular memory, and protect the shared script */
773808
if (zend_hash_num_elements(&persistent_script->script.class_table) > 0) {
774-
zend_accel_class_hash_copy(CG(class_table), &persistent_script->script.class_table, (unique_copy_ctor_func_t) zend_class_copy_ctor);
809+
zend_accel_class_hash_copy_from_shm(CG(class_table), &persistent_script->script.class_table);
775810
}
776811
/* we must first to copy all classes and then prepare functions, since functions may try to bind
777812
classes - which depend on pre-bind class entries existent in the class table */
@@ -799,7 +834,7 @@ zend_op_array* zend_accel_load_script(zend_persistent_script *persistent_script,
799834
zend_accel_function_hash_copy(CG(function_table), &persistent_script->script.function_table);
800835
}
801836
if (zend_hash_num_elements(&persistent_script->script.class_table) > 0) {
802-
zend_accel_class_hash_copy(CG(class_table), &persistent_script->script.class_table, NULL);
837+
zend_accel_class_hash_copy(CG(class_table), &persistent_script->script.class_table);
803838
}
804839
}
805840

0 commit comments

Comments
 (0)