Skip to content

Commit d4aba32

Browse files
committed
Cache negative defined() results
1 parent ef85100 commit d4aba32

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed

Zend/zend_execute.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,23 @@ ZEND_API int ZEND_FASTCALL zend_do_fcall_overloaded(zend_execute_data *call, zva
371371
(slot)[1] = (ptr); \
372372
} while (0)
373373

374+
#define CACHE_SPECIAL (1<<0)
375+
376+
#define IS_SPECIAL_CACHE_VAL(ptr) \
377+
(((uintptr_t)(ptr)) & CACHE_SPECIAL)
378+
379+
#define ENCODE_SPECIAL_CACHE_NUM(num) \
380+
((void*)((((uintptr_t)(num)) << 1) | CACHE_SPECIAL))
381+
382+
#define DECODE_SPECIAL_CACHE_NUM(ptr) \
383+
(((uintptr_t)(ptr)) >> 1)
384+
385+
#define ENCODE_SPECIAL_CACHE_PTR(ptr) \
386+
((void*)(((uintptr_t)(ptr)) | CACHE_SPECIAL))
387+
388+
#define DECODE_SPECIAL_CACHE_PTR(ptr) \
389+
((void*)(((uintptr_t)(ptr)) & ~CACHE_SPECIAL))
390+
374391
#define SKIP_EXT_OPLINE(opline) do { \
375392
while (UNEXPECTED((opline)->opcode >= ZEND_EXT_STMT \
376393
&& (opline)->opcode <= ZEND_TICKS)) { \

Zend/zend_vm_def.h

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5024,8 +5024,9 @@ ZEND_VM_HANDLER(99, ZEND_FETCH_CONSTANT, UNUSED|CONST_FETCH, CONST, CACHE_SLOT)
50245024
USE_OPLINE
50255025
zend_constant *c;
50265026

5027-
if (EXPECTED(CACHED_PTR(opline->extended_value))) {
5028-
c = CACHED_PTR(opline->extended_value);
5027+
c = CACHED_PTR(opline->extended_value);
5028+
if (EXPECTED(c != NULL) && EXPECTED(!IS_SPECIAL_CACHE_VAL(c))) {
5029+
/* pass */
50295030
} else if (UNEXPECTED((c = zend_quick_get_constant(RT_CONSTANT(opline, opline->op2) + 1, opline->op1.num)) == NULL)) {
50305031
SAVE_OPLINE();
50315032

@@ -7640,14 +7641,25 @@ ZEND_VM_HANDLER(122, ZEND_DEFINED, CONST, ANY, CACHE_SLOT)
76407641
zend_constant *c;
76417642
int result;
76427643

7643-
if (EXPECTED(CACHED_PTR(opline->extended_value))) {
7644-
result = 1;
7645-
} else if ((c = zend_quick_get_constant(RT_CONSTANT(opline, opline->op1), 0)) == NULL) {
7646-
result = 0;
7647-
} else {
7648-
CACHE_PTR(opline->extended_value, c);
7649-
result = 1;
7650-
}
7644+
c = CACHED_PTR(opline->extended_value);
7645+
do {
7646+
if (EXPECTED(c != NULL)) {
7647+
if (!IS_SPECIAL_CACHE_VAL(c)) {
7648+
result = 1;
7649+
break;
7650+
} else if (EXPECTED(zend_hash_num_elements(EG(zend_constants)) == DECODE_SPECIAL_CACHE_NUM(c))) {
7651+
result = 0;
7652+
break;
7653+
}
7654+
}
7655+
if ((c = zend_quick_get_constant(RT_CONSTANT(opline, opline->op1), 0)) == NULL) {
7656+
CACHE_PTR(opline->extended_value, ENCODE_SPECIAL_CACHE_NUM(zend_hash_num_elements(EG(zend_constants))));
7657+
result = 0;
7658+
} else {
7659+
CACHE_PTR(opline->extended_value, c);
7660+
result = 1;
7661+
}
7662+
} while (0);
76517663
ZEND_VM_SMART_BRANCH(result, 0);
76527664
ZVAL_BOOL(EX_VAR(opline->result.var), result);
76537665
ZEND_VM_NEXT_OPCODE();

Zend/zend_vm_execute.h

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3946,14 +3946,25 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_DEFINED_SPEC_CONST_HANDLER(ZEN
39463946
zend_constant *c;
39473947
int result;
39483948

3949-
if (EXPECTED(CACHED_PTR(opline->extended_value))) {
3950-
result = 1;
3951-
} else if ((c = zend_quick_get_constant(RT_CONSTANT(opline, opline->op1), 0)) == NULL) {
3952-
result = 0;
3953-
} else {
3954-
CACHE_PTR(opline->extended_value, c);
3955-
result = 1;
3956-
}
3949+
c = CACHED_PTR(opline->extended_value);
3950+
do {
3951+
if (EXPECTED(c != NULL)) {
3952+
if (!IS_SPECIAL_CACHE_VAL(c)) {
3953+
result = 1;
3954+
break;
3955+
} else if (EXPECTED(zend_hash_num_elements(EG(zend_constants)) == DECODE_SPECIAL_CACHE_NUM(c))) {
3956+
result = 0;
3957+
break;
3958+
}
3959+
}
3960+
if ((c = zend_quick_get_constant(RT_CONSTANT(opline, opline->op1), 0)) == NULL) {
3961+
CACHE_PTR(opline->extended_value, ENCODE_SPECIAL_CACHE_NUM(zend_hash_num_elements(EG(zend_constants))));
3962+
result = 0;
3963+
} else {
3964+
CACHE_PTR(opline->extended_value, c);
3965+
result = 1;
3966+
}
3967+
} while (0);
39573968
ZEND_VM_SMART_BRANCH(result, 0);
39583969
ZVAL_BOOL(EX_VAR(opline->result.var), result);
39593970
ZEND_VM_NEXT_OPCODE();
@@ -32040,8 +32051,9 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_FETCH_CONSTANT_SPEC_UNUSED_CON
3204032051
USE_OPLINE
3204132052
zend_constant *c;
3204232053

32043-
if (EXPECTED(CACHED_PTR(opline->extended_value))) {
32044-
c = CACHED_PTR(opline->extended_value);
32054+
c = CACHED_PTR(opline->extended_value);
32055+
if (EXPECTED(c != NULL) && EXPECTED(!IS_SPECIAL_CACHE_VAL(c))) {
32056+
/* pass */
3204532057
} else if (UNEXPECTED((c = zend_quick_get_constant(RT_CONSTANT(opline, opline->op2) + 1, opline->op1.num)) == NULL)) {
3204632058
SAVE_OPLINE();
3204732059

0 commit comments

Comments
 (0)