Skip to content
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

Support one rust type serializing as many dtypes #15

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
target
Cargo.lock
tests/*.npy
benches/*.npy
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@ matrix:
allow_failures:
- rust: nightly

script:
- cargo build --verbose
- cargo build --verbose --features derive --examples
- cargo test --verbose --features derive
47 changes: 46 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,51 @@ members = [ "npy-derive" ]
byteorder = "1"
nom = "3"

[dependencies.npy-derive]
path = "npy-derive"
version = "0.4"
optional = true
default-features = false

[dev-dependencies]
memmap = "0.6"
npy-derive = { path = "npy-derive", version = "0.4" }

[features]
default = []

# Reexports the derive macros so that you can use them qualified under `npy::`:
#
# #[derive(npy::Serialize, npy::Deserialize, npy::AutoSerialize)]
# struct Struct { ... }
#
# This is a nicer alternative to `#[macro_use] extern crate npy_derive`, which
# directly imports things like `#[derive(Serialize)]` that may conflict with
# other crates (e.g. `serde`).
derive = ["npy-derive"]

[[example]]
name = "plain"

[[example]]
name = "large"
required-features = ["derive"]

[[example]]
name = "simple"
required-features = ["derive"]

[[example]]
name = "roundtrip"
required-features = ["derive"]

[[test]]
name = "derive_hygiene"
required-features = ["derive"]

[[test]]
name = "roundtrip"
required-features = ["derive"]

[[test]]
name = "serialize_array"
required-features = ["derive"]
117 changes: 90 additions & 27 deletions benches/bench.rs
Original file line number Diff line number Diff line change
@@ -1,44 +1,107 @@
#![feature(test)]

#[macro_use]
extern crate npy_derive;
extern crate npy;
extern crate test;

use npy::Serializable;
use npy::{Serialize, Deserialize, AutoSerialize, TypeWrite, TypeRead};
use test::Bencher;
use test::black_box as bb;

#[derive(Serializable, Debug, PartialEq)]
struct Array {
a: i32,
b: f32,
const NITER: usize = 100_000;

macro_rules! gen_benches {
($T:ty, $new:expr) => {
#[inline(never)]
fn test_data() -> Vec<u8> {
let mut raw = Vec::new();
let writer = <$T>::writer(&<$T>::default_dtype()).unwrap();
for i in 0usize..NITER {
writer.write_one(&mut raw, &$new(i)).unwrap();
}
raw
}

#[bench]
fn read(b: &mut Bencher) {
let raw = test_data();
b.iter(|| {
let dtype = <$T>::default_dtype();
let reader = <$T>::reader(&dtype).unwrap();

let mut remainder = &raw[..];
for _ in 0usize..NITER {
let (value, new_remainder) = reader.read_one(remainder);
bb(value);
remainder = new_remainder;
}
assert_eq!(remainder.len(), 0);
});
}

#[bench]
fn read_to_vec(b: &mut Bencher) {
// FIXME: Write to a Cursor<Vec<u8>> once #16 is merged
let path = concat!("benches/bench_", stringify!($T), ".npy");

npy::to_file(path, (0usize..NITER).map($new)).unwrap();
let bytes = std::fs::read(path).unwrap();

b.iter(|| {
bb(npy::NpyData::<$T>::from_bytes(&bytes).unwrap().to_vec())
});
}

#[bench]
fn write(b: &mut Bencher) {
b.iter(|| {
bb(test_data())
});
}
};
}

const NITER: usize = 100_000;
#[cfg(feature = "derive")]
mod simple {
use super::*;

fn test_data() -> Vec<u8> {
let mut raw = Vec::new();
for i in 0..NITER {
let arr = Array { a: i as i32, b: i as f32 };
arr.write(&mut raw).unwrap();
#[derive(npy::Serialize, npy::Deserialize, npy::AutoSerialize)]
#[derive(Debug, PartialEq)]
struct Simple {
a: i32,
b: f32,
}
raw

gen_benches!(Simple, |i| Simple { a: i as i32, b: i as f32 });
}

#[bench]
fn read(b: &mut Bencher) {
let raw = test_data();
b.iter(|| {
for i in 0..NITER {
bb(Array::read(&raw[i*8..]));
}
});
#[cfg(feature = "derive")]
mod one_field {
use super::*;

#[derive(npy::Serialize, npy::Deserialize, npy::AutoSerialize)]
#[derive(Debug, PartialEq)]
struct OneField {
a: i32,
}

gen_benches!(OneField, |i| OneField { a: i as i32 });
}

#[bench]
fn write(b: &mut Bencher) {
b.iter(|| {
bb(test_data())
});
#[cfg(feature = "derive")]
mod array {
use super::*;

#[derive(npy::Serialize, npy::Deserialize, npy::AutoSerialize)]
#[derive(Debug, PartialEq)]
struct Array {
a: [f32; 8],
}

gen_benches!(Array, |i| Array { a: [i as f32; 8] });
}

mod plain_f32 {
use super::*;

gen_benches!(f32, |i| i as f32);
}
5 changes: 1 addition & 4 deletions examples/large.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@

extern crate memmap;
#[macro_use]
extern crate npy_derive;
extern crate npy;

use std::fs::File;
use memmap::MmapOptions;


#[derive(Serializable, Debug, Default)]
#[derive(npy::Serialize, npy::Deserialize, Debug, Default)]
struct Array {
a: i32,
b: f32,
Expand Down
3 changes: 1 addition & 2 deletions examples/roundtrip.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@

#[macro_use]
extern crate npy_derive;
extern crate npy;

use std::io::Read;

#[derive(Serializable, Debug, PartialEq, Clone)]
#[derive(Serialize, Deserialize, AutoSerialize, Debug, PartialEq, Clone)]
struct Array {
a: i32,
b: f32,
Expand Down
3 changes: 1 addition & 2 deletions examples/simple.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

#[macro_use]
extern crate npy_derive;
extern crate npy;

Expand All @@ -12,7 +11,7 @@ use npy::NpyData;
// a = np.array([(1,2.5,4), (2,3.1,5)], dtype=[('a', 'i4'),('b', 'f4'),('c', 'i8')])
// np.save('examples/simple.npy', a)

#[derive(Serializable, Debug)]
#[derive(npy::Deserialize, Debug)]
struct Array {
a: i32,
b: f32,
Expand Down
1 change: 1 addition & 0 deletions npy-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ repository = "https://github.com/potocpav/npy-rs"
proc-macro = true

[dependencies]
proc-macro2 = "0.2"
quote = "0.4"
syn = "0.12"
Loading