Skip to content

Commit 588d5aa

Browse files
habermancopybara-github
authored andcommitted
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
1 parent 6964e2c commit 588d5aa

1 file changed

Lines changed: 2 additions & 13 deletions

File tree

php/ext/google/protobuf/message.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,6 @@ PHP_METHOD(Message, mergeFrom) {
682682
PHP_METHOD(Message, mergeFromString) {
683683
Message* intern = (Message*)Z_OBJ_P(getThis());
684684
char* data = NULL;
685-
char* data_copy = NULL;
686685
zend_long data_len;
687686
const upb_MiniTable* l = upb_MessageDef_MiniTable(intern->desc->msgdef);
688687
upb_Arena* arena = Arena_Get(&intern->arena);
@@ -692,11 +691,7 @@ PHP_METHOD(Message, mergeFromString) {
692691
return;
693692
}
694693

695-
// TODO: avoid this copy when we can make the decoder copy.
696-
data_copy = upb_Arena_Malloc(arena, data_len);
697-
memcpy(data_copy, data, data_len);
698-
699-
if (upb_Decode(data_copy, data_len, intern->msg, l, NULL, 0, arena) !=
694+
if (upb_Decode(data, data_len, intern->msg, l, NULL, 0, arena) !=
700695
kUpb_DecodeStatus_Ok) {
701696
zend_throw_exception_ex(NULL, 0, "Error occurred during parsing");
702697
return;
@@ -739,7 +734,6 @@ PHP_METHOD(Message, serializeToString) {
739734
PHP_METHOD(Message, mergeFromJsonString) {
740735
Message* intern = (Message*)Z_OBJ_P(getThis());
741736
char* data = NULL;
742-
char* data_copy = NULL;
743737
zend_long data_len;
744738
upb_Arena* arena = Arena_Get(&intern->arena);
745739
upb_Status status;
@@ -751,17 +745,12 @@ PHP_METHOD(Message, mergeFromJsonString) {
751745
return;
752746
}
753747

754-
// TODO: avoid this copy when we can make the decoder copy.
755-
data_copy = upb_Arena_Malloc(arena, data_len + 1);
756-
memcpy(data_copy, data, data_len);
757-
data_copy[data_len] = '\0';
758-
759748
if (ignore_json_unknown) {
760749
options |= upb_JsonDecode_IgnoreUnknown;
761750
}
762751

763752
upb_Status_Clear(&status);
764-
if (!upb_JsonDecode(data_copy, data_len, intern->msg, intern->desc->msgdef,
753+
if (!upb_JsonDecode(data, data_len, intern->msg, intern->desc->msgdef,
765754
DescriptorPool_GetSymbolTable(), options, arena,
766755
&status)) {
767756
zend_throw_exception_ex(NULL, 0, "Error occurred during parsing: %s",

0 commit comments

Comments
 (0)