Skip to content
Open
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
10 changes: 9 additions & 1 deletion benches/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use criterion::{Criterion, criterion_group, criterion_main};
use eccodes::FallibleIterator;
use eccodes::{FallibleIterator, KeyRead};
use eccodes::codes_file::{CodesFile, ProductKind};
use std::hint::black_box;
use std::path::Path;
Expand Down Expand Up @@ -28,6 +28,14 @@ pub fn key_reading(c: &mut Criterion) {
b.iter(|| msg.read_key_dynamic(black_box("values")).unwrap())
});

c.bench_function("static double array reading", |b| {
b.iter(|| -> Vec<f64> { msg.read_key(black_box("values")).unwrap() })
});

c.bench_function("static float array reading", |b| {
b.iter(|| -> Vec<f32> { msg.read_key(black_box("values")).unwrap() })
});

c.bench_function("string reading", |b| {
b.iter(|| msg.read_key_dynamic(black_box("name")).unwrap())
});
Expand Down
12 changes: 10 additions & 2 deletions src/codes_message/read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use crate::{
codes_message::CodesMessage,
errors::CodesError,
intermediate_bindings::{
NativeKeyType, codes_get_bytes, codes_get_double, codes_get_double_array, codes_get_long,
codes_get_long_array, codes_get_native_type, codes_get_size, codes_get_string,
NativeKeyType, codes_get_bytes, codes_get_double, codes_get_double_array,
codes_get_float_array, codes_get_long, codes_get_long_array, codes_get_native_type,
codes_get_size, codes_get_string,
},
};

Expand Down Expand Up @@ -113,6 +114,13 @@ macro_rules! key_size_check {

impl_key_read!(scalar, codes_get_long, NativeKeyType::Long, i64);
impl_key_read!(scalar, codes_get_double, NativeKeyType::Double, f64);
// Double-typed ecCodes arrays can be decoded directly into f32.
impl_key_read!(
array,
codes_get_float_array,
NativeKeyType::Double,
Vec<f32>
);
impl_key_read!(array, codes_get_string, NativeKeyType::Str, String);
impl_key_read!(array, codes_get_bytes, NativeKeyType::Bytes, Vec<u8>);
impl_key_read!(array, codes_get_long_array, NativeKeyType::Long, Vec<i64>);
Expand Down
24 changes: 24 additions & 0 deletions src/intermediate_bindings/codes_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,30 @@ pub unsafe fn codes_get_double_array(
}
}

pub unsafe fn codes_get_float_array(
handle: *const codes_handle,
key: &str,
) -> Result<Vec<f32>, CodesError> {
unsafe {
pointer_guard::non_null!(handle);

let mut key_size = codes_get_size(handle, key)?;
let key = CString::new(key).unwrap();

let mut key_values: Vec<f32> = vec![0.0; key_size];

let error_code = eccodes_sys::codes_get_float_array(
handle,
key.as_ptr(),
key_values.as_mut_ptr().cast(),
&raw mut key_size,
);
error_code_to_result(error_code)?;

Ok(key_values)
}
}

pub unsafe fn codes_get_long_array(
handle: *const codes_handle,
key: &str,
Expand Down
4 changes: 2 additions & 2 deletions src/intermediate_bindings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ pub enum NativeKeyType {
}

pub use codes_get::{
codes_get_bytes, codes_get_double, codes_get_double_array, codes_get_long,
codes_get_long_array, codes_get_message, codes_get_native_type, codes_get_size,
codes_get_bytes, codes_get_double, codes_get_double_array, codes_get_float_array,
codes_get_long, codes_get_long_array, codes_get_message, codes_get_native_type, codes_get_size,
codes_get_string,
};
pub use codes_handle::{codes_handle_clone, codes_handle_delete, codes_handle_new_from_file};
Expand Down