Skip to content

Add safe zero-copy conversion from bytes::Bytes (#4254) #4260

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions arrow-buffer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ path = "src/lib.rs"
bench = false

[dependencies]
bytes = { version = "1.4" }
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is such a common dependency that I don't see this being overly burdensome

Copy link
Contributor

Choose a reason for hiding this comment

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

https://crates.io/crates/bytes/reverse_dependencies -- crates.io agrees with you ✅

If it causes problems for anyone, we could also put it behind a feature flag, but perhaps we can wait to see if anyone needs that before doing it

Copy link
Contributor

Choose a reason for hiding this comment

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

if it's so common, why do we have our own Bytes types in the first place?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the bytes crate doesn't allow foreign allocations

Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we could file a request upstream? I don't see anything related to this functionality in the currently open issues

https://github.com/tokio-rs/bytes/issues?q=is%3Aissue+is%3Aopen+allocation+

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tokio-rs/bytes#437 is the upstream ticket

num = { version = "0.4", default-features = false, features = ["std"] }
half = { version = "2.1", default-features = false }

Expand Down
28 changes: 28 additions & 0 deletions arrow-buffer/src/bytes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,3 +148,31 @@ impl Debug for Bytes {
write!(f, " }}")
}
}

impl From<bytes::Bytes> for Bytes {
fn from(value: bytes::Bytes) -> Self {
Self {
len: value.len(),
ptr: NonNull::new(value.as_ptr() as _).unwrap(),
deallocation: Deallocation::Custom(std::sync::Arc::new(value)),
}
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_from_bytes() {
let bytes = bytes::Bytes::from(vec![1, 2, 3, 4]);
let arrow_bytes: Bytes = bytes.clone().into();

assert_eq!(bytes.as_ptr(), arrow_bytes.as_ptr());

drop(bytes);
drop(arrow_bytes);

let _ = Bytes::from(bytes::Bytes::new());
}
}
3 changes: 2 additions & 1 deletion arrow-flight/src/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

use crate::{utils::flight_data_to_arrow_batch, FlightData};
use arrow_array::{ArrayRef, RecordBatch};
use arrow_buffer::Buffer;
use arrow_schema::{Schema, SchemaRef};
use bytes::Bytes;
use futures::{ready, stream::BoxStream, Stream, StreamExt};
Expand Down Expand Up @@ -258,7 +259,7 @@ impl FlightDataDecoder {
));
};

let buffer: arrow_buffer::Buffer = data.data_body.into();
let buffer = Buffer::from_bytes(data.data_body.into());
Copy link
Contributor Author

@tustvold tustvold Aug 16, 2023

Choose a reason for hiding this comment

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

This API is kind of unfortunate, but I've not been able to find a good way to workaround the blanket From<AsRef<[u8]>> impl for Buffer

Copy link
Contributor

Choose a reason for hiding this comment

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

👍 I think we can use this API in IOx as well, FWIW.

let dictionary_batch =
message.header_as_dictionary_batch().ok_or_else(|| {
FlightError::protocol(
Expand Down
2 changes: 1 addition & 1 deletion arrow-flight/src/sql/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -538,7 +538,7 @@ pub fn arrow_data_from_flight_data(

let dictionaries_by_field = HashMap::new();
let record_batch = read_record_batch(
&Buffer::from(&flight_data.data_body),
&Buffer::from_bytes(flight_data.data_body.into()),
ipc_record_batch,
arrow_schema_ref.clone(),
&dictionaries_by_field,
Expand Down