Skip to content

feat(s3-crt): zero-copy GetObject via caller-owned recv_buffer - #4013

Closed
benamizilberNvidia wants to merge 3 commits into
aws:mainfrom
benamizilberNvidia:s3crt-recv-buffer
Closed

benamizilberNvidia wants to merge 3 commits into
aws:mainfrom
benamizilberNvidia:s3crt-recv-buffer

Conversation

@benamizilberNvidia

Copy link
Copy Markdown
Contributor

Summary

Continuing the discussion in #3993: adds zero-copy download support to S3CrtClient::GetObject, exposing aws-c-s3's recv_buffer option (in aws-c-s3 since v1.5.2) through a new GetObjectRequest::SetRecvBuffer() call. Response bytes are written directly into the caller's buffer instead of an internal pooled buffer the caller then copies out of — eliminating one full-object-sized memcpy per GET.

S3CrtClient already talks to aws-c-s3's C API directly for its three CRT-accelerated operations (PutObject, GetObject, CopyObject all hand-build aws_s3_meta_request_options and call aws_s3_client_make_meta_request) — this adds one more field to that same existing path, not a new integration pattern.

Why this matters for model loading

NVIDIA Run:ai Model Streamer streams model weights from object storage into host/GPU memory during inference startup, using S3CrtClient for S3-backed models. It ships pre-installed in the official AWS Deep Learning Containers for vLLM, and is AWS's documented fast-loading path for inference on EKS:

Speeding up S3CrtClient's download path translates directly into faster, AWS-recommended model loading — a shared interest, not a niche optimization.

We also looked at S3TransferManager, where recv_buffer-style support is planned, and want to lay out why we're proposing this on S3CrtClient as well:

  • Model Streamer needs ListObjectsV2 alongside GET; S3TransferManager only exposes Upload/Download.
  • We need per-request completion to be event-driven (a callback the moment one range-GET finishes), so each tensor becomes usable as soon as its own bytes land, independent of the hundreds of others in flight. S3CrtClient gives us that for free via its per-call handler. S3TransferManager can likely reach the same outcome, but via a completion future (needing a blocking/polling thread per transfer) or a registered listener object per transfer — overhead we'd rather avoid at this concurrency. Happy to be corrected.

API shape

void SetRecvBuffer(void* data, size_t capacity);

Unlike aws-crt-cpp's own recv_buffer binding (aws-crt-cpp#926), which takes a ByteBuf& the caller must keep alive, GetObjectRequest is a plain copyable value object — storing a live reference on it would be a lifetime hazard out of step with the rest of its API. We take a raw pointer + capacity instead, copied into an internally-owned aws_byte_buf when the meta-request is built, the same way this file already handles aws_s3_checksum_config.

No SDK-side validation is added: SetRecvBuffer() only exists on GetObjectRequest, so GET-only is structural. Every other constraint (buffer capacity, partNumber incompatibility, pool support) is already enforced by aws-c-s3 itself with clean typed errors, which already propagate through this client's normal failed-Outcome path — duplicating that validation here would be dead code.

Testing

  • Two unit tests (S3CrtUnitTests.cpp) covering SetRecvBuffer/GetRecvBuffer/GetRecvBufferCapacity/RecvBufferHasBeenSet round-trip and defaults.
  • One integration test (BucketAndObjectOperationTest.cpp, NoAuthPublicBucketRecvBuffer), modeled on the existing NoAuthPublicBucket test: downloads a real, public, no-auth object via recv_buffer, byte-compares it against a normal GetObject of the same object, confirms the response body stream stays empty, and confirms a partNumber GET and a too-small buffer both fail the outcome cleanly.
  • A changelog fragment is included.

All of the above were built and run against the real SDK (full CRT dependency chain compiled from source) and passed.

Continuing the discussion in aws#3993: adds GetObjectRequest::SetRecvBuffer(),
exposing aws-c-s3's recv_buffer option (present since aws-c-s3 v1.5.2) on
S3CrtClient::GetObject. Response bytes are written directly into the
caller's buffer instead of the CRT's pooled buffer, eliminating one
full-object-sized copy per GET.

Changes the code generator templates, not generated output by hand, per
CONTRIBUTING.md; the included generated-file diffs were produced by
building and running the generator against the real s3 C2J model.

Tests: unit tests covering the new request accessors, and an integration
test (modeled on the existing NoAuthPublicBucket test) that downloads a
real public object via recv_buffer, byte-compares it against a normal
GetObject, and confirms a part-number GET and a too-small buffer both
fail the outcome cleanly. All built and run against the real SDK.

Signed-off-by: Ben Ami Zilber <bzilber@nvidia.com>

@sbiscigl sbiscigl left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

first and foremost thank you for contributing.

We also looked at S3TransferManager, where recv_buffer-style support is planned, and want to lay out why we're proposing this on S3CrtClient as well

we'd like functionality that combines S3 calls, like splitting a download into ranged GETs or an upload into a multipart upload, to live in a separate higher level library that composes those calls, rather than in the generated client itself. S3CrtClient already mixes the two, and moving away from that is the whole reason we built S3TransferManager. keeping them separate also keeps the client predictable, for example a PutObject call should only need the permissions for PutObject, not s3:AbortMultipartUpload because it turned into a multipart upload behind the scenes.

Model Streamer needs ListObjectsV2 alongside GET; S3TransferManager only exposes Upload/Download

ListObjectsV2 is a modeled operation, so it belongs on a client, and the downloads belong in S3TransferManager. they're meant to be used side by side.

We need per-request completion to be event-driven

S3TransferManager should cover this: DownloadProgressListener::OnTransferComplete fires from the CRT finish callback as soon as that download is done, and you can pass one listener instance to every DownloadRequest. the S3CrtClient handler isn't meaningfully cheaper, since it's a std::function per call and also fires once the whole GetObject is done, not per ranged part.

that said, your use case is solid and you've already done the work, so we're happy to take this on S3CrtClient. i've left some inline comments on the PR with things that need to be addressed before we can merge it.

inline void* GetRecvBuffer() const { return m_recvBuffer; }
inline size_t GetRecvBufferCapacity() const { return m_recvBufferCapacity; }
inline bool RecvBufferHasBeenSet() const { return m_recvBufferHasBeenSet; }
inline void SetRecvBuffer(void* data, size_t capacity) {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

We take a raw pointer + capacity instead, copied into an internally-owned aws_byte_buf

i'm not sure i follow why this is safer than a ByteBuf, since a ByteBuf is a pointer, length and capacity and either way the caller has to keep the memory alive until the request finishes. could we take a std::shared_ptr<Aws::Crt::ByteBuf> instead, the same way PutObjectRequest::SetBody takes a std::shared_ptr<Aws::IOStream>? copies of the request then share the same buffer, and the client doesn't need to build its own.

inline const std::shared_ptr<Aws::Crt::ByteBuf>& GetRecvBuffer() const { return m_recvBuffer; }
inline void SetRecvBuffer(std::shared_ptr<Aws::Crt::ByteBuf> buffer) { m_recvBuffer = std::move(buffer); }
inline GetObjectRequest& WithRecvBuffer(std::shared_ptr<Aws::Crt::ByteBuf> buffer) {
  SetRecvBuffer(std::move(buffer));
  return *this;
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

correction on my part, a shared_ptr<ByteBuf> only shares the pointer and length, not the memory itself, so the request still doesn't own anything. what we want is for the request to share ownership of the buffer itself, the same way SetBody shares the stream, something like a std::shared_ptr<Aws::Utils::ByteBuffer>. the request copy on userData then keeps the memory alive until the CRT is done with it, even if the caller drops their reference early.

inline const std::shared_ptr<Aws::Utils::ByteBuffer>& GetRecvBuffer() const { return m_recvBuffer; }
inline void SetRecvBuffer(std::shared_ptr<Aws::Utils::ByteBuffer> buffer) { m_recvBuffer = std::move(buffer); }

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.

Thanks for the correction and the leak fix.

We'd like to go with the original shared_ptrAws::Crt::ByteBuf suggestion rather than shared_ptrAws::Utils::ByteBuffer. Performance-sensitive callers often need to manage recv buffers themselves in ways a generic owning type can't accommodate — pinned memory, kernel-registered I/O buffers, and similar — so requiring Aws::Utils::ByteBuffer's own allocator ownership, or forcing a copy into it, would break that for us and likely for others with similar needs. Crt::ByteBuf being non-owning avoids this: it keeps the descriptor alive via refcount without claiming the bytes, and we're fine keeping buffer-lifetime as our responsibility as the caller — same contract any pinned-buffer API already requires.

I'll push a fix once we've settled on the way forward for recvBuffer.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

that makes sense, ByteBuffer can only own memory it allocated itself, so it can't take pinned or registered memory without a copy. since the caller owns the memory either way though, a shared_ptr would only be sharing the descriptor, so i'd take Aws::Crt::ByteBuf by value instead. the request just holds the pointer and capacity, not the memory, which matches how aws-c-s3 treats recv_buffer. the client then copies it into userData->recvBuffer and passes its address, and RecvBufferHasBeenSet() can check buffer != nullptr. could you also add a doc comment on the setter saying the memory has to stay valid until the handler runs and can't be shared between requests in flight?

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.

yes I will make it clear in the docs 👍

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.

Docs added in the latest push, along with the switch to Aws::Crt::ByteBuf by value as discussed.

private:
void* m_recvBuffer = nullptr;
size_t m_recvBufferCapacity = 0;
bool m_recvBufferHasBeenSet = false;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

the generated models keep all the HasBeenSet bools together at the end of the class so they pack tightly, and sitting here between a size_t and an Aws::String this one adds 7 bytes of padding. do we need it at all though? a null check on the buffer already tells you it was set.

inline bool RecvBufferHasBeenSet() const { return m_recvBuffer != nullptr; }

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.

Done — dropped the bool, RecvBufferHasBeenSet() is now a null check on the buffer.

options.shutdown_callback = GetObjectRequestShutdownCallback;
options.type = AWS_S3_META_REQUEST_TYPE_GET_OBJECT;
if (request.RecvBufferHasBeenSet()) {
userData->recvBuffer = Aws::MakeUnique<struct aws_byte_buf>(ALLOCATION_TAG);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

with the request holding a shared_ptr to the caller's ByteBuf, i think this can pass it straight through, and the heap allocated copy and the recvBuffer member on CrtRequestCallbackUserData can go away. the request copy on userData already keeps the ByteBuf alive until the meta request finishes.

if (request.RecvBufferHasBeenSet()) {
  options.body_callback = nullptr;
  options.recv_buffer = request.GetRecvBuffer().get();
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

small correction here, the recvBuffer member does need to stay since aws-c-s3 holds on to the pointer until the request finishes, but it can be a plain Aws::Crt::ByteBuf view over the shared buffer instead of a heap allocation.

if (request.RecvBufferHasBeenSet()) {
  const auto& buffer = request.GetRecvBuffer();
  userData->recvBuffer = Aws::Crt::ByteBufFromEmptyArray(buffer->GetUnderlyingData(), buffer->GetLength());
  options.body_callback = nullptr;
  options.recv_buffer = &userData->recvBuffer;
}

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.

With the request now holding Aws::Crt::ByteBuf by value, this copies straight into userData->recvBuffer (no heap allocation) and passes its address — pushed.

partNumberRequest.SetKey(key);
partNumberRequest.SetPartNumber(1);
partNumberRequest.SetRecvBuffer(recvBuffer.data(), recvBuffer.size());
EXPECT_FALSE(Client->GetObject(partNumberRequest).IsSuccess());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

this partNumber case fails when the meta request is created, and the meta_request == nullptr branch in GetObjectAsync returns without freeing userData. that leak was already there, but this change makes it much easier to hit, and with the request owning the buffer it would leak the whole buffer each time. could you free userData before returning? it's the same block in the S3CrtSpecificOperations templates you're already editing, so the fix covers PutObject and CopyObject too.

if (meta_request == nullptr) {
  Aws::Delete(userData);
  return handler(this, request,
                 GetObjectOutcome(Aws::Client::AWSError<S3CrtErrors>(S3CrtErrors::INTERNAL_FAILURE, "INTERNAL_FAILURE",
                                                                     "Unable to create s3 meta request", false)),
                 handlerContext);
}

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.

Fixed as suggested, applied to all three (GetObject, PutObject, CopyObject) — pushed.

…Data leak

Addresses review on aws#4013: SetRecvBuffer now takes Aws::Crt::ByteBuf by
value instead of void*/size_t, matching aws-c-s3's own recv_buffer shape
and avoiding any owning wrapper that would conflict with caller-managed
memory (pinned, kernel-registered, etc). The client copies the struct
into CrtRequestCallbackUserData::recvBuffer (no heap allocation) and
passes its address to aws_s3_meta_request_options.

Also frees userData before the early return when
aws_s3_client_make_meta_request fails, in GetObject, PutObject, and
CopyObject - that path is easy to trigger now via recv_buffer +
partNumber, and would otherwise leak the shared buffer reference each
time.

Updated the unit tests and the NoAuthPublicBucketRecvBuffer integration
test for the new signature; all pass locally.

Signed-off-by: Ben Ami Zilber <bzilber@nvidia.com>
@sbiscigl

sbiscigl commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

LGTM, for process reasons i need to pull your commit onto a branch authored by team member, the commit is still credited to you.

#4018

also worth noting i had to patch codegen with your latest changes so i just rolled those into your commit, let me know if you have any issues with that.

Thanks for contributing!

@sbiscigl

sbiscigl commented Dec 4, 2026

Copy link
Copy Markdown
Collaborator

merged your commit, closing thanks for contributing

@sbiscigl sbiscigl closed this Dec 4, 2026
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.

2 participants