Skip to content

Commit

Permalink
Cheaper maybe_from (#1677)
Browse files Browse the repository at this point in the history
  • Loading branch information
AdamGS authored Dec 13, 2024
1 parent e8323ac commit dc56eac
Show file tree
Hide file tree
Showing 11 changed files with 15 additions and 14 deletions.
2 changes: 1 addition & 1 deletion bench-vortex/benches/compress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn parquet_decompress_read(buf: bytes::Bytes) -> usize {
}

fn parquet_compressed_written_size(array: &ArrayData, compression: Compression) -> usize {
let chunked = ChunkedArray::maybe_from(array.clone()).unwrap();
let chunked = ChunkedArray::maybe_from(array).unwrap();
let (batches, schema) = chunked_to_vec_record_batch(chunked);
parquet_compress_write(batches, schema, compression, &mut Vec::new())
}
Expand Down
2 changes: 1 addition & 1 deletion vortex-array/src/array/extension/compute/compare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl CompareFn<ExtensionArray> for ExtensionEncoding {
}

// If the RHS is an extension array matching ours, we can extract the storage.
if let Some(rhs_ext) = ExtensionArray::maybe_from(rhs.clone()) {
if let Some(rhs_ext) = ExtensionArray::maybe_from(rhs) {
return compare(lhs.storage(), rhs_ext.storage(), operator).map(Some);
}

Expand Down
5 changes: 3 additions & 2 deletions vortex-array/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,8 +64,9 @@ macro_rules! impl_encoding {
///
/// Preferred in cases where a backtrace isn't needed, like when trying multiple encoding to go
/// down different code paths.
pub fn maybe_from(data: $crate::ArrayData) -> Option<Self> {
(data.encoding().id() == <[<$Name Encoding>] as $crate::encoding::Encoding>::ID).then_some(Self(data))
pub fn maybe_from(data: impl AsRef<$crate::ArrayData>) -> Option<Self> {
let data = data.as_ref();
(data.encoding().id() == <[<$Name Encoding>] as $crate::encoding::Encoding>::ID).then_some(Self(data.clone()))
}
}

Expand Down
2 changes: 1 addition & 1 deletion vortex-ipc/src/stream_writer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl<W: VortexWrite> StreamArrayWriter<W> {
}

pub async fn write_array(self, array: ArrayData) -> VortexResult<Self> {
if let Some(chunked_array) = ChunkedArray::maybe_from(array.clone()) {
if let Some(chunked_array) = ChunkedArray::maybe_from(&array) {
self.write_array_stream(chunked_array.array_stream()).await
} else {
self.write_array_stream(array.into_array_stream()).await
Expand Down
2 changes: 1 addition & 1 deletion vortex-sampling-compressor/src/compressors/alp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ impl EncodingCompressor for ALPCompressor {

fn can_compress(&self, array: &ArrayData) -> Option<&dyn EncodingCompressor> {
// Only support primitive arrays
let parray = PrimitiveArray::maybe_from(array.clone())?;
let parray = PrimitiveArray::maybe_from(array)?;

// Only supports f32 and f64
if !matches!(parray.ptype(), PType::F32 | PType::F64) {
Expand Down
2 changes: 1 addition & 1 deletion vortex-sampling-compressor/src/compressors/alp_rd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl EncodingCompressor for ALPRDCompressor {

fn can_compress(&self, array: &ArrayData) -> Option<&dyn EncodingCompressor> {
// Only support primitive arrays
let parray = PrimitiveArray::maybe_from(array.clone())?;
let parray = PrimitiveArray::maybe_from(array)?;

// Only supports f32 and f64
if !matches!(parray.ptype(), PType::F32 | PType::F64) {
Expand Down
2 changes: 1 addition & 1 deletion vortex-sampling-compressor/src/compressors/bitpacked.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl EncodingCompressor for BitPackedCompressor {

fn can_compress(&self, array: &ArrayData) -> Option<&dyn EncodingCompressor> {
// Only support primitive arrays
let parray = PrimitiveArray::maybe_from(array.clone())?;
let parray = PrimitiveArray::maybe_from(array)?;

// Only supports unsigned ints
if !parray.ptype().is_unsigned_int() {
Expand Down
2 changes: 1 addition & 1 deletion vortex-sampling-compressor/src/compressors/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl EncodingCompressor for DeltaCompressor {

fn can_compress(&self, array: &ArrayData) -> Option<&dyn EncodingCompressor> {
// Only support primitive arrays
let parray = PrimitiveArray::maybe_from(array.clone())?;
let parray = PrimitiveArray::maybe_from(array)?;

// Only supports ints
if !parray.ptype().is_unsigned_int() {
Expand Down
6 changes: 3 additions & 3 deletions vortex-sampling-compressor/src/compressors/dict.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ impl EncodingCompressor for DictCompressor {
like: Option<CompressionTree<'a>>,
ctx: SamplingCompressor<'a>,
) -> VortexResult<CompressedArray<'a>> {
let (codes, values) = if let Some(p) = PrimitiveArray::maybe_from(array.clone()) {
let (codes, values) = if let Some(p) = PrimitiveArray::maybe_from(array) {
let (codes, values) = dict_encode_primitive(&p);
(codes.into_array(), values.into_array())
} else if let Some(vb) = VarBinArray::maybe_from(array.clone()) {
} else if let Some(vb) = VarBinArray::maybe_from(array) {
let (codes, values) = dict_encode_varbin(&vb);
(codes.into_array(), values.into_array())
} else if let Some(vb) = VarBinViewArray::maybe_from(array.clone()) {
} else if let Some(vb) = VarBinViewArray::maybe_from(array) {
let (codes, values) = dict_encode_varbinview(&vb);
(codes.into_array(), values.into_array())
} else {
Expand Down
2 changes: 1 addition & 1 deletion vortex-sampling-compressor/src/compressors/for.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl EncodingCompressor for FoRCompressor {

fn can_compress(&self, array: &ArrayData) -> Option<&dyn EncodingCompressor> {
// Only support primitive arrays
let parray = PrimitiveArray::maybe_from(array.clone())?;
let parray = PrimitiveArray::maybe_from(array)?;

// Only supports integers
if !parray.ptype().is_int() {
Expand Down
2 changes: 1 addition & 1 deletion vortex-sampling-compressor/src/compressors/zigzag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl EncodingCompressor for ZigZagCompressor {

fn can_compress(&self, array: &ArrayData) -> Option<&dyn EncodingCompressor> {
// Only support primitive arrays
let parray = PrimitiveArray::maybe_from(array.clone())?;
let parray = PrimitiveArray::maybe_from(array)?;

// Only supports signed integers
if !parray.ptype().is_signed_int() {
Expand Down

0 comments on commit dc56eac

Please sign in to comment.