proto3 encoding - struct vs marshaled struct
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
add a comment |
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
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
add a comment |
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
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
encoding protocol-buffers proto3
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
add a comment |
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
add a comment |
2 Answers
2
active
oldest
votes
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
add a comment |
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
add a comment |
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
add a comment |
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
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
answered Nov 13 '18 at 9:25
Marc Gravell♦Marc Gravell
778k19221282541
778k19221282541
add a comment |
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 13 '18 at 10:03
YaFredYaFred
5,32821531
5,32821531
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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