proto3 encoding - struct vs marshaled struct










1














I wish to know if there could be any significant difference in terms of mem efficiency between marshaling a struct and marshaling a marshaled struct.



Example:
Assume we have a struct B with some fields.



message B...


The common representation:



message A 
B b = 1;



Another way:



message A 
bytes b = 1;



Where b is a marshaled B struct.



Generally, is it a good practice? any efficiency implications?



Thanks,
Elad










share|improve this question





















  • minor point - note that there's no such thing as "proto3 encoding" - the encoding hasn't changed at all in the public protobuf time; "proto3" relates only to the DSL syntax, and doesn't impact the encoding at all.
    – Marc Gravell
    Nov 13 '18 at 9:20















1














I wish to know if there could be any significant difference in terms of mem efficiency between marshaling a struct and marshaling a marshaled struct.



Example:
Assume we have a struct B with some fields.



message B...


The common representation:



message A 
B b = 1;



Another way:



message A 
bytes b = 1;



Where b is a marshaled B struct.



Generally, is it a good practice? any efficiency implications?



Thanks,
Elad










share|improve this question





















  • minor point - note that there's no such thing as "proto3 encoding" - the encoding hasn't changed at all in the public protobuf time; "proto3" relates only to the DSL syntax, and doesn't impact the encoding at all.
    – Marc Gravell
    Nov 13 '18 at 9:20













1












1








1







I wish to know if there could be any significant difference in terms of mem efficiency between marshaling a struct and marshaling a marshaled struct.



Example:
Assume we have a struct B with some fields.



message B...


The common representation:



message A 
B b = 1;



Another way:



message A 
bytes b = 1;



Where b is a marshaled B struct.



Generally, is it a good practice? any efficiency implications?



Thanks,
Elad










share|improve this question













I wish to know if there could be any significant difference in terms of mem efficiency between marshaling a struct and marshaling a marshaled struct.



Example:
Assume we have a struct B with some fields.



message B...


The common representation:



message A 
B b = 1;



Another way:



message A 
bytes b = 1;



Where b is a marshaled B struct.



Generally, is it a good practice? any efficiency implications?



Thanks,
Elad







encoding protocol-buffers proto3






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 9:17









gavragavra

381210




381210











  • minor point - note that there's no such thing as "proto3 encoding" - the encoding hasn't changed at all in the public protobuf time; "proto3" relates only to the DSL syntax, and doesn't impact the encoding at all.
    – Marc Gravell
    Nov 13 '18 at 9:20
















  • minor point - note that there's no such thing as "proto3 encoding" - the encoding hasn't changed at all in the public protobuf time; "proto3" relates only to the DSL syntax, and doesn't impact the encoding at all.
    – Marc Gravell
    Nov 13 '18 at 9:20















minor point - note that there's no such thing as "proto3 encoding" - the encoding hasn't changed at all in the public protobuf time; "proto3" relates only to the DSL syntax, and doesn't impact the encoding at all.
– Marc Gravell
Nov 13 '18 at 9:20




minor point - note that there's no such thing as "proto3 encoding" - the encoding hasn't changed at all in the public protobuf time; "proto3" relates only to the DSL syntax, and doesn't impact the encoding at all.
– Marc Gravell
Nov 13 '18 at 9:20












2 Answers
2






active

oldest

votes


















1














At the payload level, they are identical - however, in terms of how implementations treat them, there may be differences. The most obvious difference is that you can't use a bytes until you further deserialize it; this has pros and cons:



  • if you weren't ever going to touch it anyway, this could be fine and advantageous - avoiding some CPU processing that you didn't need for read or write; this will also mean that any downstream allocations (strings, etc) don't need to happen - so you only have a single allocation chunk: easy and efficient

  • if you do need to read it, then in addition to making life less convenient, you could have allocated an extra chunk of memory for the raw form (a chunk of bytes), and you'll need to allocate for the deserialized form; if you went straight to the deserialized form, most implementations would have skipped that intermediate allocation

So: yes, it will have different characteristics. Whether they are advantageous (or the opposite) depends on whether you also need to do the extra deserialization step on the bytes payload






share|improve this answer




























    1














    I think it's a bad practice to declare a bytes field instead of a struct you would have otherwise specified in a proto file.



    It's called a specification hole: you will have to write an additional documentation to describe how the receiver has to understand the bytes






    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













      draft saved

      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277563%2fproto3-encoding-struct-vs-marshaled-struct%23new-answer', 'question_page');

      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      1














      At the payload level, they are identical - however, in terms of how implementations treat them, there may be differences. The most obvious difference is that you can't use a bytes until you further deserialize it; this has pros and cons:



      • if you weren't ever going to touch it anyway, this could be fine and advantageous - avoiding some CPU processing that you didn't need for read or write; this will also mean that any downstream allocations (strings, etc) don't need to happen - so you only have a single allocation chunk: easy and efficient

      • if you do need to read it, then in addition to making life less convenient, you could have allocated an extra chunk of memory for the raw form (a chunk of bytes), and you'll need to allocate for the deserialized form; if you went straight to the deserialized form, most implementations would have skipped that intermediate allocation

      So: yes, it will have different characteristics. Whether they are advantageous (or the opposite) depends on whether you also need to do the extra deserialization step on the bytes payload






      share|improve this answer

























        1














        At the payload level, they are identical - however, in terms of how implementations treat them, there may be differences. The most obvious difference is that you can't use a bytes until you further deserialize it; this has pros and cons:



        • if you weren't ever going to touch it anyway, this could be fine and advantageous - avoiding some CPU processing that you didn't need for read or write; this will also mean that any downstream allocations (strings, etc) don't need to happen - so you only have a single allocation chunk: easy and efficient

        • if you do need to read it, then in addition to making life less convenient, you could have allocated an extra chunk of memory for the raw form (a chunk of bytes), and you'll need to allocate for the deserialized form; if you went straight to the deserialized form, most implementations would have skipped that intermediate allocation

        So: yes, it will have different characteristics. Whether they are advantageous (or the opposite) depends on whether you also need to do the extra deserialization step on the bytes payload






        share|improve this answer























          1












          1








          1






          At the payload level, they are identical - however, in terms of how implementations treat them, there may be differences. The most obvious difference is that you can't use a bytes until you further deserialize it; this has pros and cons:



          • if you weren't ever going to touch it anyway, this could be fine and advantageous - avoiding some CPU processing that you didn't need for read or write; this will also mean that any downstream allocations (strings, etc) don't need to happen - so you only have a single allocation chunk: easy and efficient

          • if you do need to read it, then in addition to making life less convenient, you could have allocated an extra chunk of memory for the raw form (a chunk of bytes), and you'll need to allocate for the deserialized form; if you went straight to the deserialized form, most implementations would have skipped that intermediate allocation

          So: yes, it will have different characteristics. Whether they are advantageous (or the opposite) depends on whether you also need to do the extra deserialization step on the bytes payload






          share|improve this answer












          At the payload level, they are identical - however, in terms of how implementations treat them, there may be differences. The most obvious difference is that you can't use a bytes until you further deserialize it; this has pros and cons:



          • if you weren't ever going to touch it anyway, this could be fine and advantageous - avoiding some CPU processing that you didn't need for read or write; this will also mean that any downstream allocations (strings, etc) don't need to happen - so you only have a single allocation chunk: easy and efficient

          • if you do need to read it, then in addition to making life less convenient, you could have allocated an extra chunk of memory for the raw form (a chunk of bytes), and you'll need to allocate for the deserialized form; if you went straight to the deserialized form, most implementations would have skipped that intermediate allocation

          So: yes, it will have different characteristics. Whether they are advantageous (or the opposite) depends on whether you also need to do the extra deserialization step on the bytes payload







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 '18 at 9:25









          Marc GravellMarc Gravell

          778k19221282541




          778k19221282541























              1














              I think it's a bad practice to declare a bytes field instead of a struct you would have otherwise specified in a proto file.



              It's called a specification hole: you will have to write an additional documentation to describe how the receiver has to understand the bytes






              share|improve this answer

























                1














                I think it's a bad practice to declare a bytes field instead of a struct you would have otherwise specified in a proto file.



                It's called a specification hole: you will have to write an additional documentation to describe how the receiver has to understand the bytes






                share|improve this answer























                  1












                  1








                  1






                  I think it's a bad practice to declare a bytes field instead of a struct you would have otherwise specified in a proto file.



                  It's called a specification hole: you will have to write an additional documentation to describe how the receiver has to understand the bytes






                  share|improve this answer












                  I think it's a bad practice to declare a bytes field instead of a struct you would have otherwise specified in a proto file.



                  It's called a specification hole: you will have to write an additional documentation to describe how the receiver has to understand the bytes







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 13 '18 at 10:03









                  YaFredYaFred

                  5,32821531




                  5,32821531



























                      draft saved

                      draft discarded
















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid


                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.

                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53277563%2fproto3-encoding-struct-vs-marshaled-struct%23new-answer', 'question_page');

                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      27

                      Top Tejano songwriter Luis Silva dead of heart attack at 64

                      Category:Rhetoric