encoding: avoid heap allocation when encoding/decoding SpatialObject#53304
Merged
craig[bot] merged 2 commits intocockroachdb:masterfrom Aug 24, 2020
Merged
encoding: avoid heap allocation when encoding/decoding SpatialObject#53304craig[bot] merged 2 commits intocockroachdb:masterfrom
craig[bot] merged 2 commits intocockroachdb:masterfrom
Conversation
This commit shaves off a heap allocation in each of the following functions: - `EncodeTableKey` - `EncodeGeoAscending` - `EncodeGeoDescending` - `DecodeGeoAscending` - `DecodeGeoDescending` - `EncodeGeoValue` - `EncodeUntaggedGeoValue` - `DecodeUntaggedGeoValue` In each of these, we were letting a local SpatialObject variable escape to the heap when passed through protoutil.Marshal or protoutil.Unmarshal. This was unnecessary, as we already have a Datum on the heap nearby any of the callers of these functions, so with a bit of restructuring, we can avoid the allocations entirely. The callers of the decode functions are a little more awkward than strictly necessary right now. They are written the way that they are so that we don't accidentally regress on this improvement when we remove the double-boxing being discussed in cockroachdb#53252. I don't have any benchmarks set up, so I wasn't able to measure the exact effect of this change.
Member
protoutil.Marshal causes both arguments to escape to the heap. This is easily avoided.
sumeerbhola
approved these changes
Aug 24, 2020
Collaborator
sumeerbhola
left a comment
There was a problem hiding this comment.
(I'm starting to yearn for explicit heap allocations instead of this Go "magic")
Reviewed 4 of 4 files at r1, 1 of 1 files at r2.
Reviewable status:complete! 1 of 0 LGTMs obtained (waiting on @otan)
Contributor
Author
Agreed. The general rule of thumb that helps me is that interfaces == heap memory, so either a) avoid them, b) make sure they're inlined away, c) make sure they point to existing heap memory. bors r+ |
otan
approved these changes
Aug 24, 2020
Contributor
|
Build failed (retrying...): |
Contributor
|
Build failed (retrying...): |
Contributor
|
Build succeeded: |
This file contains hidden or 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
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.
This commit shaves off a heap allocation in each of the following functions:
EncodeTableKeyEncodeGeoAscendingEncodeGeoDescendingDecodeGeoAscendingDecodeGeoDescendingEncodeGeoValueEncodeUntaggedGeoValueDecodeUntaggedGeoValueIn each of these, we were letting a local SpatialObject variable escape
to the heap when passed through
protoutil.Marshalorprotoutil.Unmarshal.This was unnecessary, as we already have a Datum on the heap nearby any
of the callers of these functions, so with a bit of restructuring, we
can avoid the allocations entirely.
The callers of the decode functions are a little more awkward than
strictly necessary right now. They are written the way that they are so
that we don't accidentally regress on this improvement when we remove
the double-boxing being discussed in #53252.
I don't have any benchmarks set up, so I wasn't able to measure the
exact effect of this change.