Skip to content

perf(storage): gRPC zerocopy codec#10888

Merged
frankyn merged 16 commits into
googleapis:mainfrom
tritone:zerocopy-codec
Oct 3, 2024
Merged

perf(storage): gRPC zerocopy codec#10888
frankyn merged 16 commits into
googleapis:mainfrom
tritone:zerocopy-codec

Conversation

@tritone

@tritone tritone commented Sep 19, 2024

Copy link
Copy Markdown
Contributor

Replace the current custom codec for ReadObjectResponse with a CodecV2 that can handle data split across multiple buffers.

Unit tests pass but need to finish up some stuff to get the end-to-end working.

@tritone tritone requested review from a team September 19, 2024 19:39
@product-auto-label product-auto-label Bot added the api: storage Issues related to the Cloud Storage API. label Sep 19, 2024
Replace the current custom codec for ReadObjectResponse with a
CodecV2 that can handle data split across multiple buffers.

Unit tests pass but need to finish up some stuff to get the
end-to-end working.
Comment thread storage/grpc_client.go Outdated
Comment thread storage/grpc_client.go Outdated
// Consume a bytes field from the input. Returns offsets for the data in the buffer slices
// and an error.
func (d *readResponseDecoder) consumeBytes() (*bufferSliceOffsets, error) {
b := (*d.databufs)[d.currBuf].ReadOnlyData()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit, this function only seems to require (*d.databufs)[d.currBuf].Len().

@BrennaEpp BrennaEpp left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Couple initial comments, looks like there is some commented out code as well.

Comment thread storage/grpc_client.go Outdated
Comment thread storage/grpc_client.go Outdated
Comment thread storage/grpc_client.go Outdated
Comment thread storage/grpc_client.go Outdated
Comment thread storage/grpc_client.go Outdated
Comment thread storage/grpc_client.go Outdated
Comment thread storage/grpc_client_test.go Outdated
Comment thread storage/grpc_client_test.go
Comment thread storage/grpc_client_test.go Outdated
Comment thread storage/grpc_client.go Outdated
Comment thread storage/grpc_client.go Outdated
Comment thread storage/grpc_client.go Outdated
Comment thread storage/grpc_client.go Outdated
Comment thread storage/grpc_client.go Outdated
Comment thread storage/grpc_client.go Outdated
// Consume a varint that represents the length of a bytes field. Return the length of
// the data, and advance the offsets by the length of the varint.
func (d *readResponseDecoder) consumeVarint() (uint64, error) {
b := (*d.databufs)[d.currBuf].ReadOnlyData()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I wonder if it's be worth stashing the current buffer's []byte in readResponseDecoder, to avoid the need for repeated calls to ReadOnlyData.

Or perhaps even stash a [][]byte and materialize all the buffers once at the start of decoding. You could avoid an allocation in the common case by keeping a [N][]byte in the decoder:

type readResponseDecoder struct {
  bufs [][]byte
  staticBufs [4][]byte
}

func (d *readResponseDecoder) init() {
  d.bufs = d.staticBufs[:0]
  for _, b := range d.databufs {
    d.bufs = append(d.bufs, b.ReadOnlyData())
  }
}

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I don't fully understand this, why would staticBufs be of len 4?

I think I'll probably just leave this for now if that's okay, I don't see overhead from these calls.

Comment thread storage/grpc_client.go Outdated
Comment thread storage/grpc_client.go
Comment thread storage/grpc_client.go
Comment thread storage/grpc_client.go Outdated
Comment thread storage/grpc_client_test.go Outdated

@neild neild left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Looks good to me!

Comment thread storage/grpc_client.go
currOff := d.currOff
var buf []byte
for remaining > 0 {
b := d.databufs[currBuf].ReadOnlyData()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Could simplify the code a tiny bit by making b start at the current offset:

b := b.databufs[currBuf].ReadOnlyData()[currOff:]
if len(b) < remaining {
  buf = append(buf, b...)
  remaining -= len(b)
  currBuf++
  currOff = 0
} else {
  buf = append(buf, b[:remaining]...)
  remaining = 0
}

Comment thread storage/grpc_client.go
d.currOff += remaining
remaining = 0
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Might be a bit simpler as:

d.currOff += n
for d.currBuf < len(d.databufs) && d.currOff >= d.databufs[d.currBuf].Len() {
  d.currOff -= d.databufs[d.currBuf].Len()
  d.currBuf++
}

@frankyn frankyn enabled auto-merge (squash) October 3, 2024 22:49
@frankyn frankyn merged commit aeba28f into googleapis:main Oct 3, 2024
@tritone tritone deleted the zerocopy-codec branch October 7, 2024 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

api: storage Issues related to the Cloud Storage API.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants