-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement mismatched model id + improvement
- Loading branch information
1 parent
5f2f339
commit 9a3a673
Showing
13 changed files
with
188 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
/// Found a way to prepend bytes at the beginning of a Vec<u8> with a constant overhead. | ||
use bincode::{Decode, Encode}; | ||
use criterion::{criterion_group, criterion_main, BenchmarkId, Criterion}; | ||
|
||
fn criterion_benchmark(c: &mut Criterion) { | ||
let mut group = c.benchmark_group("encode"); | ||
|
||
// 1 byte, 1KB, 1MB, 10MB, 100MB | ||
for nb_bytes in [1, 1024, 1024 * 1024, 10 * 1024 * 1024, 100 * 1024 * 1024].into_iter() { | ||
group.throughput(criterion::Throughput::Bytes(nb_bytes as u64)); | ||
|
||
let header: Vec<u8> = vec![0; 4]; | ||
let mut data: Vec<u8> = vec![1; nb_bytes]; | ||
group.bench_function(BenchmarkId::new("prepend_bytes", nb_bytes), |b| { | ||
b.iter(|| { | ||
// Fastest way to prepend bytes to data | ||
let mut header = header.clone(); | ||
header.append(&mut data); | ||
// prepend bytes to data | ||
// let mut header = header.clone(); | ||
// header.extend_from_slice(&data); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
criterion_group!(benches, criterion_benchmark); | ||
criterion_main!(benches); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,22 @@ | ||
use crate::ModelAttributes; | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
|
||
pub(crate) fn generate_native_model_decode_body() -> TokenStream { | ||
pub(crate) fn generate_native_model_decode_body(attrs: &ModelAttributes) -> TokenStream { | ||
let id = attrs.id.clone().expect("id is required"); | ||
let gen = quote! { | ||
fn native_model_decode_body(data: Vec<u8>) -> Result<Self, native_model::DecodeBodyError> { | ||
native_model_decode_body(data).map_err(|e| native_model::DecodeBodyError { | ||
fn native_model_decode_body(data: Vec<u8>, id: u32) -> Result<Self, native_model::DecodeBodyError> { | ||
println!("id: {}, {}", id, #id); | ||
if id != #id { | ||
return Err(native_model::DecodeBodyError::MismatchedModelId); | ||
} | ||
|
||
native_model_decode_body(data).map_err(|e| native_model::DecodeBodyError::DecodeError { | ||
msg: format!("{}", e), | ||
source: e.into(), | ||
}) | ||
} | ||
}; | ||
|
||
gen.into() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.