Skip to content

Commit

Permalink
Merge pull request #103 from nockty/feature/unsafe
Browse files Browse the repository at this point in the history
Add `unmarshal_unsafe` feature
  • Loading branch information
vmg authored Oct 4, 2023
2 parents 91b0b3f + 6a7e8af commit 5a1a54c
Show file tree
Hide file tree
Showing 23 changed files with 25,863 additions and 31 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ gen-testproto: gen-wkt-testproto install
testproto/pool/pool_with_oneof.proto \
testproto/proto3opt/opt.proto \
testproto/proto2/scalars.proto \
testproto/unsafe/unsafe.proto \
|| exit 1;

gen-wkt-testproto: install
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ The following features can be generated:

- `unmarshal`: generates a `func (p *YourProto) UnmarshalVT(data []byte)` that behaves similarly to calling `proto.Unmarshal(data, p)` on the message, except the unmarshalling is performed by unrolled codegen without using reflection and allocating as little memory as possible. If the receiver `p` is **not** fully zeroed-out, the unmarshal call will actually behave like `proto.Merge(data, p)`. This is because the `proto.Unmarshal` in the ProtoBuf API is implemented by resetting the destination message and then calling `proto.Merge` on it. To ensure proper `Unmarshal` semantics, ensure you've called `proto.Reset` on your message before calling `UnmarshalVT`, or that your message has been newly allocated.

- `unmarshal_unsafe` generates a `func (p *YourProto) UnmarshalVTUnsafe(data []byte)` that behaves like `UnmarshalVT`, except it unsafely casts slices of data to `bytes` and `string` fields instead of copying them to newly allocated arrays, so that it performs less allocations. **Data received from the wire has to be left untouched for the lifetime of the message.** Otherwise, the message's `bytes` and `string` fields can be corrupted.

- `pool`: generates the following helper methods

- `func (p *YourProto) ResetVT()`: this function behaves similarly to `proto.Reset(p)`, except it keeps as much memory as possible available on the message, so that further calls to `UnmarshalVT` on the same message will need to allocate less memory. This an API meant to be used with memory pools and does not need to be used directly.
Expand Down Expand Up @@ -221,4 +223,4 @@ plugins:
opt: paths=source_relative
```
Running `buf generate` will now also include the `vtprotobuf` optimized helpers.
Running `buf generate` will now also include the `vtprotobuf` optimized helpers.
Loading

0 comments on commit 5a1a54c

Please sign in to comment.