-
Notifications
You must be signed in to change notification settings - Fork 4.6k
encoding/proto: enable use cached size option #8569
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,10 +23,11 @@ package proto | |
| import ( | ||
| "fmt" | ||
|
|
||
| "google.golang.org/grpc/encoding" | ||
| "google.golang.org/grpc/mem" | ||
| "google.golang.org/protobuf/proto" | ||
| "google.golang.org/protobuf/protoadapt" | ||
|
|
||
| "google.golang.org/grpc/encoding" | ||
| "google.golang.org/grpc/mem" | ||
| ) | ||
|
|
||
| // Name is the name registered for the proto compressor. | ||
|
|
@@ -48,15 +49,15 @@ func (c *codecV2) Marshal(v any) (data mem.BufferSlice, err error) { | |
|
|
||
| size := proto.Size(vv) | ||
arjan-bal marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since this seems to rely on a runtime behaviour of calling There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if we don't call There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this be cleaner? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The docstring of And that if that condition is not met, bad things might happen. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just want to flag an edge case that normally shouldn’t happen: if another part of the stack keeps a reference to the proto message and it’s shared across multiple Go routines, one routine might modify the message while another is marshaling it through this codec. In that situation, enabling That said, the issue I’m describing would still cause (other) problems even if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've updated the comment: rs-unity@ec0c47e |
||
| if mem.IsBelowBufferPoolingThreshold(size) { | ||
| buf, err := proto.Marshal(vv) | ||
| buf, err := proto.MarshalOptions{UseCachedSize: true}.Marshal(vv) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| data = append(data, mem.SliceBuffer(buf)) | ||
| } else { | ||
| pool := mem.DefaultBufferPool() | ||
| buf := pool.Get(size) | ||
| if _, err := (proto.MarshalOptions{}).MarshalAppend((*buf)[:0], vv); err != nil { | ||
| if _, err := (proto.MarshalOptions{UseCachedSize: true}).MarshalAppend((*buf)[:0], vv); err != nil { | ||
| pool.Put(buf) | ||
| return nil, err | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.