Skip to content

encoding: avoid heap allocation when encoding/decoding SpatialObject#53304

Merged
craig[bot] merged 2 commits intocockroachdb:masterfrom
nvb:nvanbenschoten/geoEncAlloc
Aug 24, 2020
Merged

encoding: avoid heap allocation when encoding/decoding SpatialObject#53304
craig[bot] merged 2 commits intocockroachdb:masterfrom
nvb:nvanbenschoten/geoEncAlloc

Conversation

@nvb
Copy link
Copy Markdown
Contributor

@nvb nvb commented Aug 24, 2020

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 #53252.

I don't have any benchmarks set up, so I wasn't able to measure the
exact effect of this change.

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.
@nvb nvb requested review from otan and sumeerbhola August 24, 2020 03:27
@cockroach-teamcity
Copy link
Copy Markdown
Member

This change is Reviewable

protoutil.Marshal causes both arguments to escape to the heap.
This is easily avoided.
Copy link
Copy Markdown
Collaborator

@sumeerbhola sumeerbhola left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:lgtm:

(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: :shipit: complete! 1 of 0 LGTMs obtained (waiting on @otan)

@nvb
Copy link
Copy Markdown
Contributor Author

nvb commented Aug 24, 2020

(I'm starting to yearn for explicit heap allocations instead of this Go "magic")

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+

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Aug 24, 2020

Build failed (retrying...):

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Aug 24, 2020

Build failed (retrying...):

@craig
Copy link
Copy Markdown
Contributor

craig bot commented Aug 24, 2020

Build succeeded:

@craig craig bot merged commit 83c9e34 into cockroachdb:master Aug 24, 2020
@nvb nvb deleted the nvanbenschoten/geoEncAlloc branch August 25, 2020 01:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants