Skip to content

Commit 141409e

Browse files
committed
Implemented Opcache (memory-only) for new attribute structs.
1 parent 08b2528 commit 141409e

File tree

4 files changed

+81
-53
lines changed

4 files changed

+81
-53
lines changed

Zend/zend_attributes.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
zend_class_entry *zend_ce_php_attribute;
1313
zend_class_entry *zend_ce_php_compiler_attribute;
1414

15+
#define ZEND_ATTRIBUTE_SIZE(argc) (sizeof(zend_attribute) + sizeof(zval) * (argc) - sizeof(zval))
16+
1517
typedef struct _zend_attribute {
1618
zend_string *name;
1719
zend_string *lcname;
@@ -20,6 +22,20 @@ typedef struct _zend_attribute {
2022
zval argv[1];
2123
} zend_attribute;
2224

25+
static zend_always_inline void zend_attribute_release(zend_attribute *attr)
26+
{
27+
uint32_t i;
28+
29+
zend_string_release(attr->name);
30+
zend_string_release(attr->lcname);
31+
32+
for (i = 0; i < attr->argc; i++) {
33+
zval_ptr_dtor(&attr->argv[i]);
34+
}
35+
36+
efree(attr);
37+
}
38+
2339
static zend_always_inline zend_bool zend_has_attribute(HashTable *attributes, zend_string *name, uint32_t offset)
2440
{
2541
if (attributes) {

Zend/zend_compile.c

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5722,7 +5722,7 @@ static zend_attribute *zend_compile_attribute(zend_ast *ast, uint32_t offset) /*
57225722
ZEND_ASSERT(ast->kind == ZEND_AST_ATTRIBUTE);
57235723

57245724
zend_ast_list *list = ast->child[1] ? zend_ast_get_list(ast->child[1]) : NULL;
5725-
zend_attribute *attr = emalloc(sizeof(zend_attribute) + sizeof(zval) * (list ? list->children : 0));
5725+
zend_attribute *attr = emalloc(ZEND_ATTRIBUTE_SIZE(list ? list->children : 0));
57265726

57275727
attr->name = zend_resolve_class_name_ast(ast->child[0]);
57285728
attr->lcname = zend_string_tolower(attr->name);
@@ -5743,21 +5743,10 @@ static zend_attribute *zend_compile_attribute(zend_ast *ast, uint32_t offset) /*
57435743
}
57445744
/* }}} */
57455745

5746-
static void attribute_ptr_dtor(zval *v) /* {{{ */
5746+
static void attribute_ptr_dtor(zval *v)
57475747
{
5748-
zend_attribute *attr = Z_PTR_P(v);
5749-
uint32_t i;
5750-
5751-
zend_string_release(attr->name);
5752-
zend_string_release(attr->lcname);
5753-
5754-
for (i = 0; i < attr->argc; i++) {
5755-
zval_ptr_dtor(&attr->argv[i]);
5756-
}
5757-
5758-
efree(attr);
5748+
zend_attribute_release((zend_attribute *) Z_PTR_P(v));
57595749
}
5760-
/* }}} */
57615750

57625751
static zend_always_inline HashTable *create_attribute_array(uint32_t size) /* {{{ */
57635752
{

ext/opcache/zend_persist.c

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "zend_constants.h"
2929
#include "zend_operators.h"
3030
#include "zend_interfaces.h"
31+
#include "zend_attributes.h"
3132

3233
#ifdef HAVE_JIT
3334
# include "jit/zend_jit.h"
@@ -76,25 +77,6 @@
7677
} \
7778
} while (0)
7879

79-
#define zend_persist_attributes(attr) do { \
80-
HashTable *ptr = zend_shared_alloc_get_xlat_entry(attr); \
81-
if (ptr) { \
82-
(attr) = ptr; \
83-
} else { \
84-
Bucket *p; \
85-
zend_hash_persist(attr); \
86-
ZEND_HASH_FOREACH_BUCKET((attr), p) { \
87-
if (p->key) { \
88-
zend_accel_store_interned_string(p->key); \
89-
} \
90-
zend_persist_zval(&p->val); \
91-
} ZEND_HASH_FOREACH_END(); \
92-
(attr) = zend_shared_memdup_put_free((attr), sizeof(HashTable)); \
93-
GC_SET_REFCOUNT((attr), 2); \
94-
GC_TYPE_INFO(attr) = IS_ARRAY | (IS_ARRAY_IMMUTABLE << GC_FLAGS_SHIFT); \
95-
} \
96-
} while (0)
97-
9880
typedef void (*zend_persist_func_t)(zval*);
9981

10082
static void zend_persist_zval(zval *z);
@@ -277,6 +259,39 @@ static void zend_persist_zval(zval *z)
277259
}
278260
}
279261

262+
static HashTable *zend_persist_attributes(HashTable *attributes)
263+
{
264+
HashTable *ptr = zend_shared_alloc_get_xlat_entry(attributes);
265+
266+
if (!ptr) {
267+
uint32_t i;
268+
zval *v;
269+
270+
zend_hash_persist(attributes);
271+
272+
ZEND_HASH_FOREACH_VAL(attributes, v) {
273+
zend_attribute *attr = Z_PTR_P(v);
274+
zend_attribute *copy = zend_shared_memdup(attr, ZEND_ATTRIBUTE_SIZE(attr->argc));
275+
276+
zend_accel_store_interned_string(copy->name);
277+
zend_accel_store_interned_string(copy->lcname);
278+
279+
for (i = 0; i < copy->argc; i++) {
280+
zend_persist_zval(&copy->argv[i]);
281+
}
282+
283+
ZVAL_PTR(v, copy);
284+
efree(attr);
285+
} ZEND_HASH_FOREACH_END();
286+
287+
ptr = zend_shared_memdup_put_free(attributes, sizeof(HashTable));
288+
GC_SET_REFCOUNT(ptr, 2);
289+
GC_TYPE_INFO(ptr) = IS_ARRAY | (IS_ARRAY_IMMUTABLE << GC_FLAGS_SHIFT);
290+
}
291+
292+
return ptr;
293+
}
294+
280295
static void zend_persist_type(zend_type *type) {
281296
if (ZEND_TYPE_HAS_LIST(*type)) {
282297
zend_type *list_type;
@@ -395,7 +410,7 @@ static void zend_persist_op_array_ex(zend_op_array *op_array, zend_persistent_sc
395410
}
396411
}
397412
if (op_array->attributes) {
398-
zend_persist_attributes(op_array->attributes);
413+
op_array->attributes = zend_persist_attributes(op_array->attributes);
399414
}
400415

401416
if (op_array->try_catch_array) {
@@ -575,7 +590,7 @@ static void zend_persist_op_array_ex(zend_op_array *op_array, zend_persistent_sc
575590
}
576591

577592
if (op_array->attributes) {
578-
zend_persist_attributes(op_array->attributes);
593+
op_array->attributes = zend_persist_attributes(op_array->attributes);
579594
}
580595

581596
if (op_array->try_catch_array) {
@@ -721,7 +736,7 @@ static void zend_persist_property_info(zval *zv)
721736
}
722737
}
723738
if (prop->attributes) {
724-
zend_persist_attributes(prop->attributes);
739+
prop->attributes = zend_persist_attributes(prop->attributes);
725740
}
726741
zend_persist_type(&prop->type);
727742
}
@@ -763,7 +778,7 @@ static void zend_persist_class_constant(zval *zv)
763778
}
764779
}
765780
if (c->attributes) {
766-
zend_persist_attributes(c->attributes);
781+
c->attributes = zend_persist_attributes(c->attributes);
767782
}
768783
}
769784

@@ -852,7 +867,7 @@ static void zend_persist_class_entry(zval *zv)
852867
}
853868
}
854869
if (ce->info.user.attributes) {
855-
zend_persist_attributes(ce->info.user.attributes);
870+
ce->info.user.attributes = zend_persist_attributes(ce->info.user.attributes);
856871
}
857872
zend_hash_persist(&ce->properties_info);
858873
ZEND_HASH_FOREACH_BUCKET(&ce->properties_info, p) {

ext/opcache/zend_persist_calc.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "zend_extensions.h"
2626
#include "zend_shared_alloc.h"
2727
#include "zend_operators.h"
28+
#include "zend_attributes.h"
2829

2930
#define ADD_DUP_SIZE(m,s) ZCG(current_persistent_script)->size += zend_shared_memdup_size((void*)m, s)
3031
#define ADD_SIZE(m) ZCG(current_persistent_script)->size += ZEND_ALIGNED_SIZE(m)
@@ -53,21 +54,6 @@
5354
} \
5455
} while (0)
5556

56-
#define zend_persist_attributes_calc(attr) do { \
57-
if (!zend_shared_alloc_get_xlat_entry(attr)) { \
58-
Bucket *p; \
59-
zend_shared_alloc_register_xlat_entry((attr), (attr)); \
60-
ADD_SIZE(sizeof(HashTable)); \
61-
zend_hash_persist_calc(attr); \
62-
ZEND_HASH_FOREACH_BUCKET((attr), p) { \
63-
if (p->key) { \
64-
ADD_INTERNED_STRING(p->key); \
65-
} \
66-
zend_persist_zval_calc(&p->val); \
67-
} ZEND_HASH_FOREACH_END(); \
68-
} \
69-
} while (0)
70-
7157
static void zend_persist_zval_calc(zval *z);
7258

7359
static void zend_hash_persist_calc(HashTable *ht)
@@ -163,6 +149,28 @@ static void zend_persist_zval_calc(zval *z)
163149
}
164150
}
165151

152+
static void zend_persist_attributes_calc(HashTable *attributes)
153+
{
154+
if (!zend_shared_alloc_get_xlat_entry(attributes)) {
155+
zend_attribute *attr;
156+
uint32_t i;
157+
158+
zend_shared_alloc_register_xlat_entry(attributes, attributes);
159+
ADD_SIZE(sizeof(HashTable));
160+
zend_hash_persist_calc(attributes);
161+
162+
ZEND_HASH_FOREACH_PTR(attributes, attr) {
163+
ADD_SIZE(ZEND_ATTRIBUTE_SIZE(attr->argc));
164+
ADD_INTERNED_STRING(attr->name);
165+
ADD_INTERNED_STRING(attr->lcname);
166+
167+
for (i = 0; i < attr->argc; i++) {
168+
zend_persist_zval_calc(&attr->argv[i]);
169+
}
170+
} ZEND_HASH_FOREACH_END();
171+
}
172+
}
173+
166174
static void zend_persist_type_calc(zend_type *type)
167175
{
168176
if (ZEND_TYPE_HAS_LIST(*type)) {

0 commit comments

Comments
 (0)