Skip to content

Commit

Permalink
Remove many of the codec "default" methods and _opt variants
Browse files Browse the repository at this point in the history
The main intention of this change is that these methods are not intended to be called by a typical zarrs user, but internally by `Array`, which will always explicitly set options.
Partial decoder methods are an exception, since an array can return a partial decoder for a chunk.
  • Loading branch information
LDeakin committed Feb 19, 2024
1 parent e7df20e commit 72085fd
Show file tree
Hide file tree
Showing 43 changed files with 622 additions and 589 deletions.
24 changes: 19 additions & 5 deletions benches/codecs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use zarrs::array::{
codec::{
array_to_bytes::bytes::Endianness,
bytes_to_bytes::blosc::{BloscCompressor, BloscShuffleMode},
ArrayCodecTraits, BloscCodec, BytesCodec, BytesToBytesCodecTraits,
ArrayCodecTraits, BloscCodec, BytesCodec, BytesToBytesCodecTraits, CodecOptions,
},
BytesRepresentation, ChunkRepresentation, DataType,
};
Expand Down Expand Up @@ -36,7 +36,11 @@ fn codec_bytes(c: &mut Criterion) {
group.throughput(Throughput::Bytes(size3));
// encode and decode have the same implementation
group.bench_function(BenchmarkId::new("encode_decode", size3), |b| {
b.iter(|| codec.encode(data.clone(), &rep).unwrap());
b.iter(|| {
codec
.encode(data.clone(), &rep, &CodecOptions::default())
.unwrap()
});
});
}
}
Expand All @@ -60,13 +64,23 @@ fn codec_blosc(c: &mut Criterion) {
let rep = BytesRepresentation::FixedSize(size3);

let data_decoded: Vec<u8> = (0..size3).map(|i| i as u8).collect();
let data_encoded = codec.encode(data_decoded.clone()).unwrap();
let data_encoded = codec
.encode(data_decoded.clone(), &CodecOptions::default())
.unwrap();
group.throughput(Throughput::Bytes(size3));
group.bench_function(BenchmarkId::new("encode", size3), |b| {
b.iter(|| codec.encode(data_decoded.clone()).unwrap());
b.iter(|| {
codec
.encode(data_decoded.clone(), &CodecOptions::default())
.unwrap()
});
});
group.bench_function(BenchmarkId::new("decode", size3), |b| {
b.iter(|| codec.decode(data_encoded.clone(), &rep).unwrap());
b.iter(|| {
codec
.decode(data_encoded.clone(), &rep, &CodecOptions::default())
.unwrap()
});
});
}
}
Expand Down
12 changes: 5 additions & 7 deletions src/array/array_async_readable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl<TStorage: ?Sized + AsyncReadableStorageTraits + 'static> Array<TStorage> {
let chunk_representation = self.chunk_array_representation(chunk_indices)?;
let chunk_decoded = self
.codecs()
.async_decode_opt(chunk_encoded, &chunk_representation, options)
.async_decode(chunk_encoded, &chunk_representation, options)
.await
.map_err(ArrayError::CodecError)?;
let chunk_decoded_size =
Expand Down Expand Up @@ -602,7 +602,7 @@ impl<TStorage: ?Sized + AsyncReadableStorageTraits + 'static> Array<TStorage> {
self.chunk_array_representation(&chunk_indices)?;
let partial_decoder = self
.codecs()
.async_partial_decoder_opt(
.async_partial_decoder(
input_handle,
&chunk_representation,
&options,
Expand Down Expand Up @@ -769,7 +769,7 @@ impl<TStorage: ?Sized + AsyncReadableStorageTraits + 'static> Array<TStorage> {

let decoded_bytes = self
.codecs()
.async_partial_decoder_opt(input_handle, &chunk_representation, options)
.async_partial_decoder(input_handle, &chunk_representation, options)
.await?
.partial_decode_opt(&[chunk_subset.clone()], options)
.await?

Check warning on line 775 in src/array/array_async_readable.rs

View check run for this annotation

Codecov / codecov/patch

src/array/array_async_readable.rs#L775

Added line #L775 was not covered by tests
Expand Down Expand Up @@ -895,14 +895,12 @@ impl<TStorage: ?Sized + AsyncReadableStorageTraits + 'static> Array<TStorage> {
let chunk_representation = self.chunk_array_representation(chunk_indices)?;
Ok(self
.codecs()
.async_partial_decoder_opt(input_handle, &chunk_representation, options)
.async_partial_decoder(input_handle, &chunk_representation, options)
.await?)
}

/// Initialises a partial decoder for the chunk at `chunk_indices` (default options).
///
/// # Errors
/// Returns an [`ArrayError`] if initialisation of the partial decoder fails.
#[allow(clippy::missing_panics_doc, clippy::missing_errors_doc)]
pub async fn async_partial_decoder<'a>(
&'a self,
chunk_indices: &[u64],
Expand Down
2 changes: 1 addition & 1 deletion src/array/array_async_writable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<TStorage: ?Sized + AsyncWritableStorageTraits + 'static> Array<TStorage> {
.create_async_writable_transformer(storage_handle);
let chunk_encoded: Vec<u8> = self
.codecs()
.async_encode_opt(chunk_bytes, &chunk_array_representation, options)
.async_encode(chunk_bytes, &chunk_array_representation, options)
.await
.map_err(ArrayError::CodecError)?;
crate::storage::async_store_chunk(
Expand Down
19 changes: 6 additions & 13 deletions src/array/array_sync_readable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl<TStorage: ?Sized + ReadableStorageTraits + 'static> Array<TStorage> {
let chunk_representation = self.chunk_array_representation(chunk_indices)?;
let chunk_decoded = self
.codecs()
.decode_opt(chunk_encoded, &chunk_representation, options)
.decode(chunk_encoded, &chunk_representation, options)
.map_err(ArrayError::CodecError)?;
let chunk_decoded_size =
chunk_representation.num_elements_usize() * chunk_representation.data_type().size();
Expand Down Expand Up @@ -304,12 +304,7 @@ impl<TStorage: ?Sized + ReadableStorageTraits + 'static> Array<TStorage> {
.map_err(ArrayError::StorageError)?;
if let Some(chunk_encoded) = chunk_encoded {
self.codecs()
.decode_into_array_view_opt(
&chunk_encoded,
&chunk_representation,
array_view,
options,
)
.decode_into_array_view(&chunk_encoded, &chunk_representation, array_view, options)
.map_err(ArrayError::CodecError)
} else {
// fill array_view with fill value
Expand Down Expand Up @@ -383,7 +378,7 @@ impl<TStorage: ?Sized + ReadableStorageTraits + 'static> Array<TStorage> {
));

self.codecs()
.partial_decoder_opt(input_handle, &chunk_representation, options)?
.partial_decoder(input_handle, &chunk_representation, options)?
.partial_decode_into_array_view_opt(chunk_subset, array_view, options)
.map_err(ArrayError::CodecError)
}
Expand Down Expand Up @@ -921,7 +916,7 @@ impl<TStorage: ?Sized + ReadableStorageTraits + 'static> Array<TStorage> {
));

self.codecs()
.partial_decoder_opt(input_handle, &chunk_representation, options)?
.partial_decoder(input_handle, &chunk_representation, options)?
.partial_decode_opt(&[chunk_subset.clone()], options)?
.pop()
.unwrap()
Expand Down Expand Up @@ -1041,13 +1036,11 @@ impl<TStorage: ?Sized + ReadableStorageTraits + 'static> Array<TStorage> {
let chunk_representation = self.chunk_array_representation(chunk_indices)?;
Ok(self
.codecs()
.partial_decoder_opt(input_handle, &chunk_representation, options)?)
.partial_decoder(input_handle, &chunk_representation, options)?)
}

/// Initialises a partial decoder for the chunk at `chunk_indices` (default options).
///
/// # Errors
/// Returns an [`ArrayError`] if initialisation of the partial decoder fails.
#[allow(clippy::missing_panics_doc, clippy::missing_errors_doc)]
pub fn partial_decoder<'a>(
&'a self,
chunk_indices: &[u64],
Expand Down
2 changes: 1 addition & 1 deletion src/array/array_sync_writable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ impl<TStorage: ?Sized + WritableStorageTraits + 'static> Array<TStorage> {
.create_writable_transformer(storage_handle);
let chunk_encoded: Vec<u8> = self
.codecs()
.encode_opt(chunk_bytes, &chunk_array_representation, options)
.encode(chunk_bytes, &chunk_array_representation, options)
.map_err(ArrayError::CodecError)?;
crate::storage::store_chunk(
&*storage_transformer,
Expand Down
Loading

0 comments on commit 72085fd

Please sign in to comment.