Skip to content

Commit 64a8c83

Browse files
committed
Remove name field from the zend_constant struct
As global constant names are case-sensitive now, we don't have to store them separately above the constant table.
1 parent b73b70f commit 64a8c83

13 files changed

+62
-76
lines changed

Zend/zend_builtin_functions.c

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,7 @@ ZEND_FUNCTION(define)
511511
register_constant:
512512
/* non persistent */
513513
ZEND_CONSTANT_SET_FLAGS(&c, 0, PHP_USER_CONSTANT);
514-
c.name = zend_string_copy(name);
515-
if (zend_register_constant(&c) == SUCCESS) {
514+
if (zend_register_constant(zend_string_copy(name), &c) == SUCCESS) {
516515
RETURN_TRUE;
517516
} else {
518517
RETURN_FALSE;
@@ -1480,6 +1479,7 @@ ZEND_FUNCTION(get_defined_constants)
14801479
zend_constant *val;
14811480
int module_number;
14821481
zval *modules, const_val;
1482+
zend_string *const_name;
14831483
char **module_names;
14841484
zend_module_entry *module;
14851485
int i = 1;
@@ -1494,12 +1494,7 @@ ZEND_FUNCTION(get_defined_constants)
14941494
} ZEND_HASH_FOREACH_END();
14951495
module_names[i] = "user";
14961496

1497-
ZEND_HASH_MAP_FOREACH_PTR(EG(zend_constants), val) {
1498-
if (!val->name) {
1499-
/* skip special constants */
1500-
continue;
1501-
}
1502-
1497+
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(EG(zend_constants), const_name, val) {
15031498
if (ZEND_CONSTANT_MODULE_NUMBER(val) == PHP_USER_CONSTANT) {
15041499
module_number = i;
15051500
} else if (ZEND_CONSTANT_MODULE_NUMBER(val) > i) {
@@ -1515,22 +1510,19 @@ ZEND_FUNCTION(get_defined_constants)
15151510
}
15161511

15171512
ZVAL_COPY_OR_DUP(&const_val, &val->value);
1518-
zend_hash_add_new(Z_ARRVAL(modules[module_number]), val->name, &const_val);
1513+
zend_hash_add_new(Z_ARRVAL(modules[module_number]), const_name, &const_val);
15191514
} ZEND_HASH_FOREACH_END();
15201515

15211516
efree(module_names);
15221517
efree(modules);
15231518
} else {
15241519
zend_constant *constant;
1520+
zend_string *const_name;
15251521
zval const_val;
15261522

1527-
ZEND_HASH_MAP_FOREACH_PTR(EG(zend_constants), constant) {
1528-
if (!constant->name) {
1529-
/* skip special constants */
1530-
continue;
1531-
}
1523+
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(EG(zend_constants), const_name, constant) {
15321524
ZVAL_COPY_OR_DUP(&const_val, &constant->value);
1533-
zend_hash_add_new(Z_ARRVAL_P(return_value), constant->name, &const_val);
1525+
zend_hash_add_new(Z_ARRVAL_P(return_value), const_name, &const_val);
15341526
} ZEND_HASH_FOREACH_END();
15351527
}
15361528
}

Zend/zend_constants.c

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,9 @@ void free_zend_constant(zval *zv)
4343

4444
if (!(ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT)) {
4545
zval_ptr_dtor_nogc(&c->value);
46-
if (c->name) {
47-
zend_string_release_ex(c->name, 0);
48-
}
4946
efree(c);
5047
} else {
5148
zval_internal_ptr_dtor(&c->value);
52-
if (c->name) {
53-
zend_string_release_ex(c->name, 1);
54-
}
5549
free(c);
5650
}
5751
}
@@ -126,53 +120,58 @@ void zend_shutdown_constants(void)
126120
ZEND_API void zend_register_null_constant(const char *name, size_t name_len, int flags, int module_number)
127121
{
128122
zend_constant c;
123+
zend_string *name_str;
129124

130125
ZVAL_NULL(&c.value);
131126
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
132-
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
133-
zend_register_constant(&c);
127+
name_str = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
128+
zend_register_constant(name_str, &c);
134129
}
135130

136131
ZEND_API void zend_register_bool_constant(const char *name, size_t name_len, bool bval, int flags, int module_number)
137132
{
138133
zend_constant c;
134+
zend_string *name_str;
139135

140136
ZVAL_BOOL(&c.value, bval);
141137
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
142-
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
143-
zend_register_constant(&c);
138+
name_str = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
139+
zend_register_constant(name_str, &c);
144140
}
145141

146142
ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zend_long lval, int flags, int module_number)
147143
{
148144
zend_constant c;
145+
zend_string *name_str;
149146

150147
ZVAL_LONG(&c.value, lval);
151148
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
152-
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
153-
zend_register_constant(&c);
149+
name_str = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
150+
zend_register_constant(name_str, &c);
154151
}
155152

156153

157154
ZEND_API void zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number)
158155
{
159156
zend_constant c;
157+
zend_string *name_str;
160158

161159
ZVAL_DOUBLE(&c.value, dval);
162160
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
163-
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
164-
zend_register_constant(&c);
161+
name_str = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
162+
zend_register_constant(name_str, &c);
165163
}
166164

167165

168166
ZEND_API void zend_register_stringl_constant(const char *name, size_t name_len, const char *strval, size_t strlen, int flags, int module_number)
169167
{
170168
zend_constant c;
169+
zend_string *name_str;
171170

172171
ZVAL_STR(&c.value, zend_string_init_interned(strval, strlen, flags & CONST_PERSISTENT));
173172
ZEND_CONSTANT_SET_FLAGS(&c, flags, module_number);
174-
c.name = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
175-
zend_register_constant(&c);
173+
name_str = zend_string_init_interned(name, name_len, flags & CONST_PERSISTENT);
174+
zend_register_constant(name_str, &c);
176175
}
177176

178177

@@ -545,25 +544,22 @@ static void* zend_hash_add_constant(HashTable *ht, zend_string *key, zend_consta
545544
return ret;
546545
}
547546

548-
ZEND_API zend_result zend_register_constant(zend_constant *c)
547+
ZEND_API zend_result zend_register_constant(zend_string *name, zend_constant *c)
549548
{
550549
zend_string *lowercase_name = NULL;
551-
zend_string *name;
552550
zend_result ret = SUCCESS;
553551
bool persistent = (ZEND_CONSTANT_FLAGS(c) & CONST_PERSISTENT) != 0;
554552

555553
#if 0
556554
printf("Registering constant for module %d\n", c->module_number);
557555
#endif
558556

559-
const char *slash = strrchr(ZSTR_VAL(c->name), '\\');
557+
const char *slash = strrchr(ZSTR_VAL(name), '\\');
560558
if (slash) {
561-
lowercase_name = zend_string_init(ZSTR_VAL(c->name), ZSTR_LEN(c->name), persistent);
562-
zend_str_tolower(ZSTR_VAL(lowercase_name), slash - ZSTR_VAL(c->name));
559+
lowercase_name = zend_string_init(ZSTR_VAL(name), ZSTR_LEN(name), persistent);
560+
zend_str_tolower(ZSTR_VAL(lowercase_name), slash - ZSTR_VAL(name));
563561
lowercase_name = zend_new_interned_string(lowercase_name);
564562
name = lowercase_name;
565-
} else {
566-
name = c->name;
567563
}
568564

569565
/* Check if the user is trying to define any special constant */
@@ -572,7 +568,6 @@ ZEND_API zend_result zend_register_constant(zend_constant *c)
572568
|| zend_hash_add_constant(EG(zend_constants), name, c) == NULL
573569
) {
574570
zend_error(E_WARNING, "Constant %s already defined", ZSTR_VAL(name));
575-
zend_string_release(c->name);
576571
if (!persistent) {
577572
zval_ptr_dtor_nogc(&c->value);
578573
}

Zend/zend_constants.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232

3333
typedef struct _zend_constant {
3434
zval value;
35-
zend_string *name;
3635
} zend_constant;
3736

3837
#define ZEND_CONSTANT_FLAGS(c) \
@@ -84,7 +83,7 @@ ZEND_API void zend_register_long_constant(const char *name, size_t name_len, zen
8483
ZEND_API void zend_register_double_constant(const char *name, size_t name_len, double dval, int flags, int module_number);
8584
ZEND_API void zend_register_string_constant(const char *name, size_t name_len, const char *strval, int flags, int module_number);
8685
ZEND_API void zend_register_stringl_constant(const char *name, size_t name_len, const char *strval, size_t strlen, int flags, int module_number);
87-
ZEND_API zend_result zend_register_constant(zend_constant *c);
86+
ZEND_API zend_result zend_register_constant(zend_string *name, zend_constant *c);
8887
#ifdef ZTS
8988
void zend_copy_constants(HashTable *target, HashTable *source);
9089
#endif

Zend/zend_execute.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4961,7 +4961,7 @@ static zend_always_inline zend_result _zend_quick_get_constant(
49614961
if (!check_defined_only) {
49624962
ZVAL_COPY_OR_DUP(EX_VAR(opline->result.var), &c->value);
49634963
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
4964-
zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(c->name));
4964+
zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(Z_STR_P(key)));
49654965
return SUCCESS;
49664966
}
49674967
}

Zend/zend_execute_API.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,9 +294,6 @@ ZEND_API void zend_shutdown_executor_values(bool fast_shutdown)
294294
break;
295295
}
296296
zval_ptr_dtor_nogc(&c->value);
297-
if (c->name) {
298-
zend_string_release_ex(c->name, 0);
299-
}
300297
efree(c);
301298
zend_string_release_ex(key, 0);
302299
} ZEND_HASH_MAP_FOREACH_END_DEL();

Zend/zend_vm_def.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8078,6 +8078,7 @@ ZEND_VM_HANDLER(143, ZEND_DECLARE_CONST, CONST, CONST)
80788078
{
80798079
USE_OPLINE
80808080
zval *name;
8081+
zend_string *name_str;
80818082
zval *val;
80828083
zend_constant c;
80838084

@@ -8096,9 +8097,9 @@ ZEND_VM_HANDLER(143, ZEND_DECLARE_CONST, CONST, CONST)
80968097
}
80978098
/* non persistent, case sensitive */
80988099
ZEND_CONSTANT_SET_FLAGS(&c, 0, PHP_USER_CONSTANT);
8099-
c.name = zend_string_copy(Z_STR_P(name));
8100+
name_str = zend_string_copy(Z_STR_P(name));
81008101

8101-
if (zend_register_constant(&c) == FAILURE) {
8102+
if (zend_register_constant(name_str, &c) == FAILURE) {
81028103
}
81038104

81048105
FREE_OP1();

Zend/zend_vm_execute.h

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

ext/opcache/ZendAccelerator.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -731,9 +731,6 @@ static void accel_copy_permanent_strings(zend_new_interned_string_func_t new_int
731731
p->key = new_interned_string(p->key);
732732
}
733733
c = (zend_constant*)Z_PTR(p->val);
734-
if (c->name) {
735-
c->name = new_interned_string(c->name);
736-
}
737734
if (Z_TYPE(c->value) == IS_STRING) {
738735
ZVAL_STR(&c->value, new_interned_string(Z_STR(c->value)));
739736
}

ext/opcache/jit/zend_jit_vm_helpers.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ static zend_always_inline zend_constant* _zend_quick_get_constant(
282282

283283
if (!check_defined_only) {
284284
if (ZEND_CONSTANT_FLAGS(c) & CONST_DEPRECATED) {
285-
zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(c->name));
285+
zend_error(E_DEPRECATED, "Constant %s is deprecated", ZSTR_VAL(Z_STR_P(key)));
286286
if (EG(exception)) {
287287
return NULL;
288288
}

ext/reflection/php_reflection.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,11 +1040,13 @@ static void _extension_string(smart_str *str, zend_module_entry *module, char *i
10401040
{
10411041
smart_str str_constants = {0};
10421042
zend_constant *constant;
1043+
zend_string *name;
10431044
int num_constants = 0;
10441045

1045-
ZEND_HASH_MAP_FOREACH_PTR(EG(zend_constants), constant) {
1046+
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(EG(zend_constants), name, constant) {
1047+
10461048
if (ZEND_CONSTANT_MODULE_NUMBER(constant) == module->module_number) {
1047-
_const_string(&str_constants, ZSTR_VAL(constant->name), &constant->value, indent);
1049+
_const_string(&str_constants, ZSTR_VAL(name), &constant->value, indent);
10481050
num_constants++;
10491051
}
10501052
} ZEND_HASH_FOREACH_END();
@@ -5992,18 +5994,19 @@ ZEND_METHOD(ReflectionExtension, getConstants)
59925994
reflection_object *intern;
59935995
zend_module_entry *module;
59945996
zend_constant *constant;
5997+
zend_string *name;
59955998

59965999
if (zend_parse_parameters_none() == FAILURE) {
59976000
RETURN_THROWS();
59986001
}
59996002
GET_REFLECTION_OBJECT_PTR(module);
60006003

60016004
array_init(return_value);
6002-
ZEND_HASH_MAP_FOREACH_PTR(EG(zend_constants), constant) {
6005+
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(EG(zend_constants), name, constant) {
60036006
if (module->module_number == ZEND_CONSTANT_MODULE_NUMBER(constant)) {
60046007
zval const_val;
60056008
ZVAL_COPY_OR_DUP(&const_val, &constant->value);
6006-
zend_hash_update(Z_ARRVAL_P(return_value), constant->name, &const_val);
6009+
zend_hash_update(Z_ARRVAL_P(return_value), name, &const_val);
60076010
}
60086011
} ZEND_HASH_FOREACH_END();
60096012
}

sapi/cli/php_cli.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -558,16 +558,16 @@ static void cli_register_file_handles(void)
558558
php_stream_to_zval(s_err, &ec.value);
559559

560560
Z_CONSTANT_FLAGS(ic.value) = 0;
561-
ic.name = zend_string_init_interned("STDIN", sizeof("STDIN")-1, 0);
562-
zend_register_constant(&ic);
561+
zend_string *stdin_name = zend_string_init_interned("STDIN", sizeof("STDIN")-1, 0);
562+
zend_register_constant(stdin_name, &ic);
563563

564564
Z_CONSTANT_FLAGS(oc.value) = 0;
565-
oc.name = zend_string_init_interned("STDOUT", sizeof("STDOUT")-1, 0);
566-
zend_register_constant(&oc);
565+
zend_string *stdout_name = zend_string_init_interned("STDOUT", sizeof("STDOUT")-1, 0);
566+
zend_register_constant(stdout_name, &oc);
567567

568568
Z_CONSTANT_FLAGS(ec.value) = 0;
569-
ec.name = zend_string_init_interned("STDERR", sizeof("STDERR")-1, 0);
570-
zend_register_constant(&ec);
569+
zend_string *stderr_name = zend_string_init_interned("STDERR", sizeof("STDERR")-1, 0);
570+
zend_register_constant(stderr_name, &ec);
571571
}
572572

573573
static const char *param_mode_conflict = "Either execute direct code, process stdin or use a file.\n";

sapi/phpdbg/phpdbg.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -912,21 +912,21 @@ void phpdbg_register_file_handles(void) /* {{{ */
912912

913913
ic.value = zin;
914914
Z_CONSTANT_FLAGS(ic.value) = 0;
915-
ic.name = zend_string_init(ZEND_STRL("STDIN"), 0);
916-
zend_hash_del(EG(zend_constants), ic.name);
917-
zend_register_constant(&ic);
915+
zend_string *stdin_name = zend_string_init_interned(ZEND_STRL("STDIN"), 0);
916+
zend_hash_del(EG(zend_constants), stdin_name);
917+
zend_register_constant(stdin_name, &ic);
918918

919919
oc.value = zout;
920920
Z_CONSTANT_FLAGS(oc.value) = 0;
921-
oc.name = zend_string_init(ZEND_STRL("STDOUT"), 0);
922-
zend_hash_del(EG(zend_constants), oc.name);
923-
zend_register_constant(&oc);
921+
zend_string *stdout_name = zend_string_init_interned(ZEND_STRL("STDOUT"), 0);
922+
zend_hash_del(EG(zend_constants), stdout_name);
923+
zend_register_constant(stdout_name, &oc);
924924

925925
ec.value = zerr;
926926
Z_CONSTANT_FLAGS(ec.value) = 0;
927-
ec.name = zend_string_init(ZEND_STRL("STDERR"), 0);
928-
zend_hash_del(EG(zend_constants), ec.name);
929-
zend_register_constant(&ec);
927+
zend_string *stderr_name = zend_string_init_interned(ZEND_STRL("STDERR"), 0);
928+
zend_hash_del(EG(zend_constants), stderr_name);
929+
zend_register_constant(stderr_name, &ec);
930930
}
931931
/* }}} */
932932

0 commit comments

Comments
 (0)