-
Notifications
You must be signed in to change notification settings - Fork 15
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
feat: speed up ProtoMethods #108
Open
elias-orijtech
wants to merge
2
commits into
cosmos:main
Choose a base branch
from
elias-orijtech:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
elias-orijtech
changed the title
features/fastreflection: initialize ProtoMethods once
feat: speed up ProtoMethods
Mar 21, 2023
Added another optimization. Let me know if they belong in separate PRs. |
I believe the remaining allocations are intrinsic to the serialization process, and thus cannot be reduced further. |
The ProtoMethods is called for every unmarshal, which in turn allocates an protoiface.Methods. The Methods object does not refer to a partucular message object, so the allocation can be saved by one-time initialization at startup. Found by the the alecthomas/go_serialization_benchmarks suite: go test -bench='Pulsar' -memprofile mem.out -cpu 6 ./ Before: goos: darwin goarch: arm64 pkg: github.com/alecthomas/go_serialization_benchmarks Benchmark_Pulsar_Marshal-6 7480744 154.1 ns/op 51.57 B/serial 96 B/op 2 allocs/op Benchmark_Pulsar_Unmarshal-6 5057922 236.9 ns/op 51.60 B/serial 256 B/op 7 allocs/op After: Benchmark_Pulsar_Marshal-6 7251019 153.9 ns/op 51.57 B/serial 96 B/op 2 allocs/op Benchmark_Pulsar_Unmarshal-6 6409070 187.2 ns/op 51.63 B/serial 160 B/op 5 allocs/op
CheckInitialized checks for missing required fields, which is not relevant for a proto3 implementation such as Pulsar. However, without it protobuf will fall back to a slow fallback that allocates memory per serialization. Before: Benchmark_Pulsar_Marshal-6 7251019 153.9 ns/op 51.57 B/serial 96 B/op 2 allocs/op Benchmark_Pulsar_Unmarshal-6 6409070 187.2 ns/op 51.63 B/serial 160 B/op 5 allocs/op After: Benchmark_Pulsar_Marshal-6 12480471 91.47 ns/op 51.61 B/serial 128 B/op 2 allocs/op Benchmark_Pulsar_Unmarshal-6 10919157 108.6 ns/op 51.58 B/serial 128 B/op 3 allocs/op
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
The ProtoMethods is called for every unmarshal, which in turn allocates an protoiface.Methods. The Methods object does not refer to a partucular message object, so the allocation can be saved by one-time initialization at startup.
Found by the the alecthomas/go_serialization_benchmarks suite:
Before:
goos: darwin
goarch: arm64
pkg: github.com/alecthomas/go_serialization_benchmarks
Benchmark_Pulsar_Marshal-6 7480744 154.1 ns/op 51.57 B/serial 96 B/op 2 allocs/op
Benchmark_Pulsar_Unmarshal-6 5057922 236.9 ns/op 51.60 B/serial 256 B/op 7 allocs/op
After:
Benchmark_Pulsar_Marshal-6 7251019 153.9 ns/op 51.57 B/serial 96 B/op 2 allocs/op
Benchmark_Pulsar_Unmarshal-6 6409070 187.2 ns/op 51.63 B/serial 160 B/op 5 allocs/op
CC @tac0turtle @odeke-em