Skip to content

Commit 3cae867

Browse files
authored
Persistent Descriptor Pool (#6899)
* Make reserve names map persistent * Add DescriptorInternal to map * Use get_msgdef_desc in encode_decode.c * Add persistent map for ce=>def and enum=>def * Replace get_ce_obj * Remove get_proto_obj * Remove obsolete fields from Descriptor and EnumDescriptor * Add cache for descriptor php values * Add cache for descriptors * Fix bug * Avoid add generated file again if it has been added * Fix the bug upb depends on null-ended str for look up. * Initialize generated pool impl * Turn down old generated pool * Add init entry flag protobuf.keep_descriptor_pool_after_request By default, it's off. Add protobuf.keep_descriptor_pool_after_request=1 to php.ini to enable it * Fix zts build
1 parent 3cc55d6 commit 3cae867

File tree

9 files changed

+554
-246
lines changed

9 files changed

+554
-246
lines changed

php/ext/google/protobuf/def.c

+175-69
Large diffs are not rendered by default.

php/ext/google/protobuf/encode_decode.c

+45-62
Large diffs are not rendered by default.

php/ext/google/protobuf/message.c

+15-22
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ void* message_data(MessageHeader* msg) {
255255

256256
void custom_data_init(const zend_class_entry* ce,
257257
MessageHeader* intern PHP_PROTO_TSRMLS_DC) {
258-
Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, get_ce_obj(ce));
258+
DescriptorInternal* desc = get_ce_desc(ce);
259259
intern->data = ALLOC_N(uint8_t, desc->layout->size);
260260
// We wrap first so that everything in the message object is GC-rooted in
261261
// case a collection happens during object creation in layout_init().
@@ -279,15 +279,15 @@ void build_class_from_descriptor(
279279
Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, php_descriptor);
280280

281281
// Map entries don't have existing php class.
282-
if (upb_msgdef_mapentry(desc->msgdef)) {
282+
if (upb_msgdef_mapentry(desc->intern->msgdef)) {
283283
return;
284284
}
285285

286-
zend_class_entry* registered_ce = desc->klass;
286+
zend_class_entry* registered_ce = desc->intern->klass;
287287

288-
if (desc->layout == NULL) {
289-
MessageLayout* layout = create_layout(desc->msgdef);
290-
desc->layout = layout;
288+
if (desc->intern->layout == NULL) {
289+
MessageLayout* layout = create_layout(desc->intern->msgdef);
290+
desc->intern->layout = layout;
291291
}
292292

293293
registered_ce->create_object = message_create;
@@ -395,8 +395,7 @@ void Message_construct(zval* msg, zval* array_wrapper) {
395395
is_wrapper = is_wrapper_msg(submsgdef);
396396

397397
if (is_wrapper) {
398-
PHP_PROTO_HASHTABLE_VALUE subdesc_php = get_def_obj(submsgdef);
399-
Descriptor* subdesc = UNBOX_HASHTABLE_VALUE(Descriptor, subdesc_php);
398+
DescriptorInternal* subdesc = get_msgdef_desc(submsgdef);
400399
subklass = subdesc->klass;
401400
}
402401
}
@@ -435,8 +434,7 @@ void Message_construct(zval* msg, zval* array_wrapper) {
435434
is_wrapper = is_wrapper_msg(submsgdef);
436435

437436
if (is_wrapper) {
438-
PHP_PROTO_HASHTABLE_VALUE subdesc_php = get_def_obj(submsgdef);
439-
Descriptor* subdesc = UNBOX_HASHTABLE_VALUE(Descriptor, subdesc_php);
437+
DescriptorInternal* subdesc = get_msgdef_desc(submsgdef);
440438
subklass = subdesc->klass;
441439
}
442440
}
@@ -459,8 +457,7 @@ void Message_construct(zval* msg, zval* array_wrapper) {
459457
}
460458
} else if (upb_fielddef_issubmsg(field)) {
461459
const upb_msgdef* submsgdef = upb_fielddef_msgsubdef(field);
462-
PHP_PROTO_HASHTABLE_VALUE desc_php = get_def_obj(submsgdef);
463-
Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, desc_php);
460+
DescriptorInternal* desc = get_msgdef_desc(submsgdef);
464461

465462
CACHED_VALUE* cached = NULL;
466463
if (upb_fielddef_containingoneof(field)) {
@@ -530,7 +527,7 @@ PHP_METHOD(Message, __construct) {
530527

531528
PHP_METHOD(Message, clear) {
532529
MessageHeader* msg = UNBOX(MessageHeader, getThis());
533-
Descriptor* desc = msg->descriptor;
530+
DescriptorInternal* desc = msg->descriptor;
534531
zend_class_entry* ce = desc->klass;
535532

536533
zend_object_std_dtor(&msg->std TSRMLS_CC);
@@ -1536,14 +1533,13 @@ PHP_METHOD(Any, unpack) {
15361533
}
15371534

15381535
const char* fully_qualified_name = type_url + url_prefix_len;
1539-
PHP_PROTO_HASHTABLE_VALUE desc_php = get_proto_obj(fully_qualified_name);
1540-
if (desc_php == NULL) {
1536+
DescriptorInternal* desc = get_proto_desc(fully_qualified_name);
1537+
if (desc == NULL) {
15411538
zend_throw_exception(
15421539
NULL, "Specified message in any hasn't been added to descriptor pool",
15431540
0 TSRMLS_CC);
15441541
return;
15451542
}
1546-
Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, desc_php);
15471543
zend_class_entry* klass = desc->klass;
15481544
ZVAL_OBJ(return_value, klass->create_object(klass TSRMLS_CC));
15491545
MessageHeader* msg = UNBOX(MessageHeader, return_value);
@@ -1589,8 +1585,7 @@ PHP_METHOD(Any, pack) {
15891585
PHP_PROTO_FAKE_SCOPE_END;
15901586

15911587
// Set type url.
1592-
Descriptor* desc =
1593-
UNBOX_HASHTABLE_VALUE(Descriptor, get_ce_obj(Z_OBJCE_P(val)));
1588+
DescriptorInternal* desc = get_ce_desc(Z_OBJCE_P(val));
15941589
const char* fully_qualified_name = upb_msgdef_fullname(desc->msgdef);
15951590
size_t type_url_len =
15961591
strlen(TYPE_URL_PREFIX) + strlen(fully_qualified_name) + 1;
@@ -1617,14 +1612,12 @@ PHP_METHOD(Any, is) {
16171612
return;
16181613
}
16191614

1620-
PHP_PROTO_HASHTABLE_VALUE desc_php = get_ce_obj(klass);
1621-
if (desc_php == NULL) {
1615+
DescriptorInternal* desc = get_ce_desc(klass);
1616+
if (desc == NULL) {
16221617
RETURN_BOOL(false);
16231618
}
16241619

16251620
// Create corresponded type url.
1626-
Descriptor* desc =
1627-
UNBOX_HASHTABLE_VALUE(Descriptor, get_ce_obj(klass));
16281621
const char* fully_qualified_name = upb_msgdef_fullname(desc->msgdef);
16291622
size_t type_url_len =
16301623
strlen(TYPE_URL_PREFIX) + strlen(fully_qualified_name) + 1;

0 commit comments

Comments
 (0)