Skip to content

Commit

Permalink
Persistent Descriptor Pool (#6899)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
TeBoring authored and rafi-kamal committed Nov 19, 2019
1 parent cd7b27b commit edda0c9
Show file tree
Hide file tree
Showing 9 changed files with 554 additions and 246 deletions.
244 changes: 175 additions & 69 deletions php/ext/google/protobuf/def.c

Large diffs are not rendered by default.

107 changes: 45 additions & 62 deletions php/ext/google/protobuf/encode_decode.c

Large diffs are not rendered by default.

37 changes: 15 additions & 22 deletions php/ext/google/protobuf/message.c
Expand Up @@ -255,7 +255,7 @@ void* message_data(MessageHeader* msg) {

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

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

zend_class_entry* registered_ce = desc->klass;
zend_class_entry* registered_ce = desc->intern->klass;

if (desc->layout == NULL) {
MessageLayout* layout = create_layout(desc->msgdef);
desc->layout = layout;
if (desc->intern->layout == NULL) {
MessageLayout* layout = create_layout(desc->intern->msgdef);
desc->intern->layout = layout;
}

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

if (is_wrapper) {
PHP_PROTO_HASHTABLE_VALUE subdesc_php = get_def_obj(submsgdef);
Descriptor* subdesc = UNBOX_HASHTABLE_VALUE(Descriptor, subdesc_php);
DescriptorInternal* subdesc = get_msgdef_desc(submsgdef);
subklass = subdesc->klass;
}
}
Expand Down Expand Up @@ -435,8 +434,7 @@ void Message_construct(zval* msg, zval* array_wrapper) {
is_wrapper = is_wrapper_msg(submsgdef);

if (is_wrapper) {
PHP_PROTO_HASHTABLE_VALUE subdesc_php = get_def_obj(submsgdef);
Descriptor* subdesc = UNBOX_HASHTABLE_VALUE(Descriptor, subdesc_php);
DescriptorInternal* subdesc = get_msgdef_desc(submsgdef);
subklass = subdesc->klass;
}
}
Expand All @@ -459,8 +457,7 @@ void Message_construct(zval* msg, zval* array_wrapper) {
}
} else if (upb_fielddef_issubmsg(field)) {
const upb_msgdef* submsgdef = upb_fielddef_msgsubdef(field);
PHP_PROTO_HASHTABLE_VALUE desc_php = get_def_obj(submsgdef);
Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, desc_php);
DescriptorInternal* desc = get_msgdef_desc(submsgdef);

CACHED_VALUE* cached = NULL;
if (upb_fielddef_containingoneof(field)) {
Expand Down Expand Up @@ -530,7 +527,7 @@ PHP_METHOD(Message, __construct) {

PHP_METHOD(Message, clear) {
MessageHeader* msg = UNBOX(MessageHeader, getThis());
Descriptor* desc = msg->descriptor;
DescriptorInternal* desc = msg->descriptor;
zend_class_entry* ce = desc->klass;

zend_object_std_dtor(&msg->std TSRMLS_CC);
Expand Down Expand Up @@ -1536,14 +1533,13 @@ PHP_METHOD(Any, unpack) {
}

const char* fully_qualified_name = type_url + url_prefix_len;
PHP_PROTO_HASHTABLE_VALUE desc_php = get_proto_obj(fully_qualified_name);
if (desc_php == NULL) {
DescriptorInternal* desc = get_proto_desc(fully_qualified_name);
if (desc == NULL) {
zend_throw_exception(
NULL, "Specified message in any hasn't been added to descriptor pool",
0 TSRMLS_CC);
return;
}
Descriptor* desc = UNBOX_HASHTABLE_VALUE(Descriptor, desc_php);
zend_class_entry* klass = desc->klass;
ZVAL_OBJ(return_value, klass->create_object(klass TSRMLS_CC));
MessageHeader* msg = UNBOX(MessageHeader, return_value);
Expand Down Expand Up @@ -1589,8 +1585,7 @@ PHP_METHOD(Any, pack) {
PHP_PROTO_FAKE_SCOPE_END;

// Set type url.
Descriptor* desc =
UNBOX_HASHTABLE_VALUE(Descriptor, get_ce_obj(Z_OBJCE_P(val)));
DescriptorInternal* desc = get_ce_desc(Z_OBJCE_P(val));
const char* fully_qualified_name = upb_msgdef_fullname(desc->msgdef);
size_t type_url_len =
strlen(TYPE_URL_PREFIX) + strlen(fully_qualified_name) + 1;
Expand All @@ -1617,14 +1612,12 @@ PHP_METHOD(Any, is) {
return;
}

PHP_PROTO_HASHTABLE_VALUE desc_php = get_ce_obj(klass);
if (desc_php == NULL) {
DescriptorInternal* desc = get_ce_desc(klass);
if (desc == NULL) {
RETURN_BOOL(false);
}

// Create corresponded type url.
Descriptor* desc =
UNBOX_HASHTABLE_VALUE(Descriptor, get_ce_obj(klass));
const char* fully_qualified_name = upb_msgdef_fullname(desc->msgdef);
size_t type_url_len =
strlen(TYPE_URL_PREFIX) + strlen(fully_qualified_name) + 1;
Expand Down

0 comments on commit edda0c9

Please sign in to comment.