From 588d5aa0dbee01c6caf51add7c7ed74edbe0c794 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Thu, 21 Dec 2023 07:49:15 -0800 Subject: [PATCH] Optimized binary/JSON parsing to no longer copy input data into a temp buffer. These decoders have copied by default for a long time. There is no longer any need to copy anything. This should reduce CPU and memory usage. PiperOrigin-RevId: 592859621 --- php/ext/google/protobuf/message.c | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/php/ext/google/protobuf/message.c b/php/ext/google/protobuf/message.c index 979f226fc9..15461bf988 100644 --- a/php/ext/google/protobuf/message.c +++ b/php/ext/google/protobuf/message.c @@ -682,7 +682,6 @@ PHP_METHOD(Message, mergeFrom) { PHP_METHOD(Message, mergeFromString) { Message* intern = (Message*)Z_OBJ_P(getThis()); char* data = NULL; - char* data_copy = NULL; zend_long data_len; const upb_MiniTable* l = upb_MessageDef_MiniTable(intern->desc->msgdef); upb_Arena* arena = Arena_Get(&intern->arena); @@ -692,11 +691,7 @@ PHP_METHOD(Message, mergeFromString) { return; } - // TODO: avoid this copy when we can make the decoder copy. - data_copy = upb_Arena_Malloc(arena, data_len); - memcpy(data_copy, data, data_len); - - if (upb_Decode(data_copy, data_len, intern->msg, l, NULL, 0, arena) != + if (upb_Decode(data, data_len, intern->msg, l, NULL, 0, arena) != kUpb_DecodeStatus_Ok) { zend_throw_exception_ex(NULL, 0, "Error occurred during parsing"); return; @@ -739,7 +734,6 @@ PHP_METHOD(Message, serializeToString) { PHP_METHOD(Message, mergeFromJsonString) { Message* intern = (Message*)Z_OBJ_P(getThis()); char* data = NULL; - char* data_copy = NULL; zend_long data_len; upb_Arena* arena = Arena_Get(&intern->arena); upb_Status status; @@ -751,17 +745,12 @@ PHP_METHOD(Message, mergeFromJsonString) { return; } - // TODO: avoid this copy when we can make the decoder copy. - data_copy = upb_Arena_Malloc(arena, data_len + 1); - memcpy(data_copy, data, data_len); - data_copy[data_len] = '\0'; - if (ignore_json_unknown) { options |= upb_JsonDecode_IgnoreUnknown; } upb_Status_Clear(&status); - if (!upb_JsonDecode(data_copy, data_len, intern->msg, intern->desc->msgdef, + if (!upb_JsonDecode(data, data_len, intern->msg, intern->desc->msgdef, DescriptorPool_GetSymbolTable(), options, arena, &status)) { zend_throw_exception_ex(NULL, 0, "Error occurred during parsing: %s",