abseil-cpp icon indicating copy to clipboard operation
abseil-cpp copied to clipboard

Copying data out of Cord - CopyToArray/pread/memcpy like interface?

Open wjywbs opened this issue 4 years ago • 3 comments

Cord has CopyCordToString(). Could there be a helper function to copy to a char* buffer? For example,

// Copies the data in the cord from |offset| up to buffer |size|. Returns the actual number of bytes copied.
// Returns 0 if offset is out of bound.
size_t CopyToArray(char* buffer, size_t size, size_t offset = 0);

Alternatives considered: use cord.Subcord(offset, size) and iterate chunks, but Subcord() seems to have the overhead of creating new cord reps.

wjywbs avatar Mar 19 '22 23:03 wjywbs

Can you tell us more about your use case?

derekmauro avatar Mar 20 '22 01:03 derekmauro

I'm using grpc to fetch some data. Since open source protobuf copies data, I didn't parse the response into protobuf. I convert grpc response data slices into a cord and plan to operate on the cord.

wjywbs avatar Mar 21 '22 17:03 wjywbs

To be honest this sounds like premature optimization. Are you sure the overhead of Subcord matters for this use case?

However, I'd bet protobuf and/or grpc are currently working on getting a better zero-copy implementation in place now that Cord has been open source for some time. You might consider asking them for what you need.

derekmauro avatar Mar 21 '22 18:03 derekmauro