Skip to content

Commit 603a4ac

Browse files
authored
Merge pull request #4 from koolkode/Opcache
PHP 8 Opcache API
2 parents 6efec1b + 97bcede commit 603a4ac

File tree

3 files changed

+74
-106
lines changed

3 files changed

+74
-106
lines changed

ext/opcache/zend_file_cache.c

Lines changed: 29 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,25 @@ static int zend_file_cache_flock(int fd, int type)
158158
} \
159159
} while (0)
160160

161+
#define SERIALIZE_ATTRIBUTES(attr) do { \
162+
if ((attr) && !IS_SERIALIZED(attr)) { \
163+
HashTable *ht; \
164+
SERIALIZE_PTR(attr); \
165+
ht = (attr); \
166+
UNSERIALIZE_PTR(ht); \
167+
zend_file_cache_serialize_hash(ht, script, info, buf, zend_file_cache_serialize_zval); \
168+
} \
169+
} while (0)
170+
171+
#define UNSERIALIZE_ATTRIBUTES(attr) do { \
172+
if ((attr) && !IS_UNSERIALIZED(attr)) { \
173+
HashTable *ht; \
174+
UNSERIALIZE_PTR(attr); \
175+
ht = (attr); \
176+
zend_file_cache_unserialize_hash(ht, script, buf, zend_file_cache_unserialize_zval, ZVAL_PTR_DTOR); \
177+
} \
178+
} while (0)
179+
161180
static const uint32_t uninitialized_bucket[-HT_MIN_MASK] =
162181
{HT_INVALID_IDX, HT_INVALID_IDX};
163182

@@ -421,14 +440,7 @@ static void zend_file_cache_serialize_op_array(zend_op_array *op_arra
421440
SERIALIZE_PTR(op_array->live_range);
422441
SERIALIZE_PTR(op_array->scope);
423442
SERIALIZE_STR(op_array->doc_comment);
424-
if (op_array->attributes && !IS_SERIALIZED(op_array->attributes)) {
425-
HashTable *ht;
426-
427-
SERIALIZE_PTR(op_array->attributes);
428-
ht = op_array->attributes;
429-
UNSERIALIZE_PTR(ht);
430-
zend_file_cache_serialize_hash(ht, script, info, buf, zend_file_cache_serialize_zval);
431-
}
443+
SERIALIZE_ATTRIBUTES(op_array->attributes);
432444
SERIALIZE_PTR(op_array->try_catch_array);
433445
SERIALIZE_PTR(op_array->prototype);
434446
return;
@@ -555,6 +567,7 @@ static void zend_file_cache_serialize_op_array(zend_op_array *op_arra
555567
SERIALIZE_PTR(op_array->live_range);
556568
SERIALIZE_PTR(op_array->scope);
557569
SERIALIZE_STR(op_array->doc_comment);
570+
SERIALIZE_ATTRIBUTES(op_array->attributes);
558571
SERIALIZE_PTR(op_array->try_catch_array);
559572
SERIALIZE_PTR(op_array->prototype);
560573

@@ -599,15 +612,7 @@ static void zend_file_cache_serialize_prop_info(zval *zv,
599612
if (prop->doc_comment) {
600613
SERIALIZE_STR(prop->doc_comment);
601614
}
602-
603-
if (prop->attributes) {
604-
HashTable *ht;
605-
606-
SERIALIZE_PTR(prop->attributes);
607-
ht = prop->attributes;
608-
UNSERIALIZE_PTR(ht);
609-
zend_file_cache_serialize_hash(ht, script, info, buf, zend_file_cache_serialize_zval);
610-
}
615+
SERIALIZE_ATTRIBUTES(prop->attributes);
611616

612617
zend_file_cache_serialize_type(&prop->type, script, info, buf);
613618
}
@@ -636,14 +641,7 @@ static void zend_file_cache_serialize_class_constant(zval *z
636641
SERIALIZE_STR(c->doc_comment);
637642
}
638643

639-
if (c->attributes) {
640-
HashTable *ht;
641-
642-
SERIALIZE_PTR(c->attributes);
643-
ht = c->attributes;
644-
UNSERIALIZE_PTR(ht);
645-
zend_file_cache_serialize_hash(ht, script, info, buf, zend_file_cache_serialize_zval);
646-
}
644+
SERIALIZE_ATTRIBUTES(c->attributes);
647645
}
648646
}
649647
}
@@ -701,14 +699,7 @@ static void zend_file_cache_serialize_class(zval *zv,
701699
zend_file_cache_serialize_hash(&ce->constants_table, script, info, buf, zend_file_cache_serialize_class_constant);
702700
SERIALIZE_STR(ce->info.user.filename);
703701
SERIALIZE_STR(ce->info.user.doc_comment);
704-
if (ce->info.user.attributes && !IS_SERIALIZED(ce->info.user.attributes)) {
705-
HashTable *ht;
706-
707-
SERIALIZE_PTR(ce->info.user.attributes);
708-
ht = ce->info.user.attributes;
709-
UNSERIALIZE_PTR(ht);
710-
zend_file_cache_serialize_hash(ht, script, info, buf, zend_file_cache_serialize_zval);
711-
}
702+
SERIALIZE_ATTRIBUTES(ce->info.user.attributes);
712703
zend_file_cache_serialize_hash(&ce->properties_info, script, info, buf, zend_file_cache_serialize_prop_info);
713704

714705
if (ce->properties_info_table) {
@@ -1174,6 +1165,7 @@ static void zend_file_cache_unserialize_op_array(zend_op_array *op_arr
11741165
UNSERIALIZE_PTR(op_array->live_range);
11751166
UNSERIALIZE_PTR(op_array->scope);
11761167
UNSERIALIZE_STR(op_array->doc_comment);
1168+
UNSERIALIZE_ATTRIBUTES(op_array->attributes);
11771169
UNSERIALIZE_PTR(op_array->try_catch_array);
11781170
UNSERIALIZE_PTR(op_array->prototype);
11791171
return;
@@ -1289,14 +1281,7 @@ static void zend_file_cache_unserialize_op_array(zend_op_array *op_arr
12891281
UNSERIALIZE_PTR(op_array->live_range);
12901282
UNSERIALIZE_PTR(op_array->scope);
12911283
UNSERIALIZE_STR(op_array->doc_comment);
1292-
if (op_array->attributes && !IS_UNSERIALIZED(op_array->attributes)) {
1293-
HashTable *ht;
1294-
1295-
UNSERIALIZE_PTR(op_array->attributes);
1296-
ht = op_array->attributes;
1297-
zend_file_cache_unserialize_hash(ht,
1298-
script, buf, zend_file_cache_unserialize_zval, ZVAL_PTR_DTOR);
1299-
}
1284+
UNSERIALIZE_ATTRIBUTES(op_array->attributes);
13001285
UNSERIALIZE_PTR(op_array->try_catch_array);
13011286
UNSERIALIZE_PTR(op_array->prototype);
13021287

@@ -1350,14 +1335,7 @@ static void zend_file_cache_unserialize_prop_info(zval *zv,
13501335
if (prop->doc_comment) {
13511336
UNSERIALIZE_STR(prop->doc_comment);
13521337
}
1353-
if (prop->attributes && !IS_UNSERIALIZED(prop->attributes)) {
1354-
HashTable *ht;
1355-
1356-
UNSERIALIZE_PTR(prop->attributes);
1357-
ht = prop->attributes;
1358-
zend_file_cache_unserialize_hash(ht,
1359-
script, buf, zend_file_cache_unserialize_zval, ZVAL_PTR_DTOR);
1360-
}
1338+
UNSERIALIZE_ATTRIBUTES(prop->attributes);
13611339
zend_file_cache_unserialize_type(&prop->type, script, buf);
13621340
}
13631341
}
@@ -1382,14 +1360,7 @@ static void zend_file_cache_unserialize_class_constant(zval *
13821360
if (c->doc_comment) {
13831361
UNSERIALIZE_STR(c->doc_comment);
13841362
}
1385-
if (c->attributes && !IS_UNSERIALIZED(c->attributes)) {
1386-
HashTable *ht;
1387-
1388-
UNSERIALIZE_PTR(c->attributes);
1389-
ht = c->attributes;
1390-
zend_file_cache_unserialize_hash(ht,
1391-
script, buf, zend_file_cache_unserialize_zval, ZVAL_PTR_DTOR);
1392-
}
1363+
UNSERIALIZE_ATTRIBUTES(c->attributes);
13931364
}
13941365
}
13951366
}
@@ -1444,14 +1415,7 @@ static void zend_file_cache_unserialize_class(zval *zv,
14441415
script, buf, zend_file_cache_unserialize_class_constant, NULL);
14451416
UNSERIALIZE_STR(ce->info.user.filename);
14461417
UNSERIALIZE_STR(ce->info.user.doc_comment);
1447-
if (ce->info.user.attributes && !IS_UNSERIALIZED(ce->info.user.attributes)) {
1448-
HashTable *ht;
1449-
1450-
UNSERIALIZE_PTR(ce->info.user.attributes);
1451-
ht = ce->info.user.attributes;
1452-
zend_file_cache_unserialize_hash(ht,
1453-
script, buf, zend_file_cache_unserialize_zval, ZVAL_PTR_DTOR);
1454-
}
1418+
UNSERIALIZE_ATTRIBUTES(ce->info.user.attributes);
14551419
zend_file_cache_unserialize_hash(&ce->properties_info,
14561420
script, buf, zend_file_cache_unserialize_prop_info, NULL);
14571421

ext/opcache/zend_persist.c

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,25 @@
7676
} \
7777
} while (0)
7878

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+
7998
typedef void (*zend_persist_func_t)(zval*);
8099

81100
static void zend_persist_zval(zval *z);
@@ -375,6 +394,9 @@ static void zend_persist_op_array_ex(zend_op_array *op_array, zend_persistent_sc
375394
op_array->doc_comment = NULL;
376395
}
377396
}
397+
if (op_array->attributes) {
398+
zend_persist_attributes(op_array->attributes);
399+
}
378400

379401
if (op_array->try_catch_array) {
380402
op_array->try_catch_array = zend_shared_alloc_get_xlat_entry(op_array->try_catch_array);
@@ -553,18 +575,7 @@ static void zend_persist_op_array_ex(zend_op_array *op_array, zend_persistent_sc
553575
}
554576

555577
if (op_array->attributes) {
556-
if (already_stored) {
557-
op_array->attributes = zend_shared_alloc_get_xlat_entry(op_array->attributes);
558-
ZEND_ASSERT(op_array->attributes != NULL);
559-
} else {
560-
zend_hash_persist(op_array->attributes, zend_persist_zval);
561-
zend_accel_store(op_array->attributes, sizeof(HashTable));
562-
/* make immutable array */
563-
GC_REFCOUNT(op_array->attributes) = 2;
564-
GC_TYPE_INFO(op_array->attributes) = IS_ARRAY | (IS_ARRAY_IMMUTABLE << 8);
565-
op_array->attributes->u.flags |= HASH_FLAG_STATIC_KEYS;
566-
op_array->attributes->u.flags &= ~HASH_FLAG_APPLY_PROTECTION;
567-
}
578+
zend_persist_attributes(op_array->attributes);
568579
}
569580

570581
if (op_array->try_catch_array) {
@@ -710,13 +721,7 @@ static void zend_persist_property_info(zval *zv)
710721
}
711722
}
712723
if (prop->attributes) {
713-
zend_hash_persist(prop->attributes, zend_persist_zval);
714-
zend_accel_store(prop->attributes, sizeof(HashTable));
715-
/* make immutable array */
716-
GC_REFCOUNT(prop->attributes) = 2;
717-
GC_TYPE_INFO(prop->attributes) = IS_ARRAY | (IS_ARRAY_IMMUTABLE << 8);
718-
prop->attributes->u.flags |= HASH_FLAG_STATIC_KEYS;
719-
prop->attributes->u.flags &= ~HASH_FLAG_APPLY_PROTECTION;
724+
zend_persist_attributes(prop->attributes);
720725
}
721726
zend_persist_type(&prop->type);
722727
}
@@ -758,13 +763,7 @@ static void zend_persist_class_constant(zval *zv)
758763
}
759764
}
760765
if (c->attributes) {
761-
zend_hash_persist(c->attributes, zend_persist_zval);
762-
zend_accel_store(c->attributes, sizeof(HashTable));
763-
/* make immutable array */
764-
GC_REFCOUNT(c->attributes) = 2;
765-
GC_TYPE_INFO(c->attributes) = IS_ARRAY | (IS_ARRAY_IMMUTABLE << 8);
766-
c->attributes->u.flags |= HASH_FLAG_STATIC_KEYS;
767-
c->attributes->u.flags &= ~HASH_FLAG_APPLY_PROTECTION;
766+
zend_persist_attributes(c->attributes);
768767
}
769768
}
770769

@@ -853,13 +852,7 @@ static void zend_persist_class_entry(zval *zv)
853852
}
854853
}
855854
if (ce->info.user.attributes) {
856-
zend_hash_persist(ce->info.user.attributes, zend_persist_zval);
857-
zend_accel_store(ce->info.user.attributes, sizeof(HashTable));
858-
/* make immutable array */
859-
GC_REFCOUNT(ce->info.user.attributes) = 2;
860-
GC_TYPE_INFO(ce->info.user.attributes) = IS_ARRAY | (IS_ARRAY_IMMUTABLE << 8);
861-
ce->info.user.attributes->u.flags |= HASH_FLAG_STATIC_KEYS;
862-
ce->info.user.attributes->u.flags &= ~HASH_FLAG_APPLY_PROTECTION;
855+
zend_persist_attributes(ce->info.user.attributes);
863856
}
864857
zend_hash_persist(&ce->properties_info);
865858
ZEND_HASH_FOREACH_BUCKET(&ce->properties_info, p) {

ext/opcache/zend_persist_calc.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,21 @@
5353
} \
5454
} while (0)
5555

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+
5671
static void zend_persist_zval_calc(zval *z);
5772

5873
static void zend_hash_persist_calc(HashTable *ht)
@@ -250,8 +265,7 @@ static void zend_persist_op_array_calc_ex(zend_op_array *op_array)
250265
}
251266

252267
if (op_array->attributes) {
253-
ADD_DUP_SIZE(op_array->attributes, sizeof(HashTable));
254-
zend_hash_persist_calc(op_array->attributes, zend_persist_zval_calc);
268+
zend_persist_attributes_calc(op_array->attributes);
255269
}
256270

257271
if (op_array->try_catch_array) {
@@ -331,8 +345,7 @@ static void zend_persist_property_info_calc(zval *zv)
331345
ADD_STRING(prop->doc_comment);
332346
}
333347
if (prop->attributes) {
334-
ADD_DUP_SIZE(prop->attributes, sizeof(HashTable));
335-
zend_hash_persist_calc(prop->attributes, zend_persist_zval_calc);
348+
zend_persist_attributes_calc(prop->attributes);
336349
}
337350
}
338351
}
@@ -349,8 +362,7 @@ static void zend_persist_class_constant_calc(zval *zv)
349362
ADD_STRING(c->doc_comment);
350363
}
351364
if (c->attributes) {
352-
ADD_DUP_SIZE(c->attributes, sizeof(HashTable));
353-
zend_hash_persist_calc(c->attributes, zend_persist_zval_calc);
365+
zend_persist_attributes_calc(c->attributes);
354366
}
355367
}
356368
}
@@ -438,8 +450,7 @@ static void zend_persist_class_entry_calc(zval *zv)
438450
ADD_STRING(ce->info.user.doc_comment);
439451
}
440452
if (ce->info.user.attributes) {
441-
ADD_DUP_SIZE(ce->info.user.attributes, sizeof(HashTable));
442-
zend_hash_persist_calc(ce->info.user.attributes, zend_persist_zval_calc);
453+
zend_persist_attributes_calc(ce->info.user.attributes);
443454
}
444455

445456
zend_hash_persist_calc(&ce->properties_info);

0 commit comments

Comments
 (0)