Skip to content

Commit

Permalink
Clippy: the performance parts (#12)
Browse files Browse the repository at this point in the history
* fix: clippy::or_fun_call
* fix: clippy::map_clone
* fix: clippy unnecessary clones
* fix: clippy::needless_pass_by_value
* fix: clippy::match-same-arms
* fix: clippy::redundant-closure-for-method-calls

Co-authored-by: Otavio Pace <[email protected]>
Co-authored-by: Julia Naomi <[email protected]>
  • Loading branch information
evaporei and naomijub authored Sep 15, 2020
1 parent 07254c9 commit 018524b
Show file tree
Hide file tree
Showing 15 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "brcode"
version = "1.1.0"
version = "1.1.1"
authors = ["Julia Naomi <[email protected]>"]
edition = "2018"
description = "Crate to parse and emit BR Codes"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A crate to parse and emit [PIX BR Code](https://www.bcb.gov.br/content/estabilid

```toml
[dependencies]
brcode = "1.1.0"
brcode = "1.1.1"
```

Shellscript to get files from release:
Expand Down
2 changes: 1 addition & 1 deletion benches/both_ways.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn brcode_benchmark(c: &mut Criterion) {

fn vec_benchmark(c: &mut Criterion) {
let code = code();
c.bench_function("using vec", |b| b.iter(|| to_string(from_str(&code))));
c.bench_function("using vec", |b| b.iter(|| to_string(&from_str(&code))));
}

criterion_group!(benches, brcode_benchmark, vec_benchmark);
Expand Down
Binary file modified clj-brcode/libbrcode.dylib
Binary file not shown.
Binary file modified clj-brcode/libbrcode.so
Binary file not shown.
Binary file modified libbrcode.dylib
Binary file not shown.
Binary file modified libbrcode.so
Binary file not shown.
Binary file modified node-brcode/libbrcode.dylib
Binary file not shown.
Binary file modified node-brcode/libbrcode.so
Binary file not shown.
2 changes: 1 addition & 1 deletion src/aux.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl Data {

pub fn to_hash(&self) -> HashMap<usize, Data> {
match self {
Data::Hash(map) => (map.0).clone().to_owned(),
Data::Hash(map) => (map.0).to_owned(),
_ => HashMap::new(),
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/emit.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::parse::Data;

pub(crate) fn emit(code: Vec<(usize, Data)>) -> String {
pub(crate) fn emit(code: &[(usize, Data)]) -> String {
code.iter()
.map(|e| match &e.1 {
Data::Single(s) => format!("{:02}{:02}{}", e.0, s.len(), s),
Expand Down Expand Up @@ -29,7 +29,7 @@ mod test {
fn emits_same_data_as_receives() {
let code = "00020104141234567890123426580014BR.GOV.BCB.PIX0136123e4567-e12b-12d1-a456-42665544000027300012BR.COM.OUTRO011001234567895204000053039865406123.455802BR5917NOME DO RECEBEDOR6008BRASILIA61087007490062190515RP12345678-201980390012BR.COM.OUTRO01190123.ABCD.3456.WXYZ6304AD38";
let parsed_data = from_str(code);
let emited = emit(parsed_data);
let emited = emit(&parsed_data);

assert_eq!(code, emited);
}
Expand All @@ -38,7 +38,7 @@ mod test {
fn helloworld_in_tag_00() {
let code = "26062602oi";
let parsed_data = from_str(code);
let emited = emit(parsed_data);
let emited = emit(&parsed_data);

assert_eq!(code, emited);
}
Expand Down
14 changes: 7 additions & 7 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ pub fn from_str(code: &str) -> Vec<(usize, parse::Data)> {
parse::parse(code, 99)
}

pub fn to_string(code: Vec<(usize, parse::Data)>) -> String {
emit::emit(code)
pub fn to_string(code: &[(usize, parse::Data)]) -> String {
emit::emit(&code)
}

pub fn brcode_to_string(code: BrCode) -> String {
Expand All @@ -35,7 +35,7 @@ fn chars_to_string(pointer: *const c_char) -> String {
str::from_utf8(slice).unwrap().to_string()
}

fn to_c_char(s: String) -> *const c_char {
fn to_c_char(s: &str) -> *const c_char {
let cs = CString::new(s.as_bytes()).unwrap();
let ptr = cs.as_ptr();
mem::forget(cs);
Expand All @@ -47,29 +47,29 @@ fn to_c_char(s: String) -> *const c_char {
pub extern "C" fn edn_from_brcode(edn: *const c_char) -> *const c_char {
let edn_str = chars_to_string(edn);
let brcode = str_to_brcode(&edn_str);
to_c_char(edn_rs::to_string(brcode))
to_c_char(&edn_rs::to_string(brcode))
}

#[no_mangle]
pub extern "C" fn edn_to_brcode(edn: *const c_char) -> *const c_char {
let edn_str = chars_to_string(edn);
let brcode: BrCode = edn_rs::from_str(&edn_str).unwrap();

to_c_char(brcode_to_string(brcode))
to_c_char(&brcode_to_string(brcode))
}

// Json
#[no_mangle]
pub extern "C" fn json_from_brcode(json: *const c_char) -> *const c_char {
let json_str = chars_to_string(json);
let brcode = str_to_brcode(&json_str);
to_c_char(serde_json::to_string(&brcode).unwrap_or("error".to_string()))
to_c_char(&serde_json::to_string(&brcode).unwrap_or_else(|_| "error".to_string()))
}

#[no_mangle]
pub extern "C" fn json_to_brcode(edn: *const c_char) -> *const c_char {
let edn_str = chars_to_string(edn);
let brcode: BrCode = serde_json::from_str(&edn_str).unwrap();

to_c_char(brcode_to_string(brcode))
to_c_char(&brcode_to_string(brcode))
}
12 changes: 6 additions & 6 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ impl From<Vec<(usize, Data)>> for BrCode {
fn from(code: Vec<(usize, Data)>) -> Self {
let hash = HashBrCode::new(code).0;
let merchant_information = (26usize..=51usize)
.map(|i| (i, hash.get(&i).clone()))
.map(|i| (i, hash.get(&i)))
.filter_map(|e| {
e.1.map(|d| {
Some(MerchantInfo {
id: e.0,
info: {
let hm = d.to_hash();
let mut keys = hm.keys().map(|k| *k).collect::<Vec<usize>>();
let mut keys = hm.keys().copied().collect::<Vec<usize>>();
keys.sort();
keys.into_iter()
.map(|idx| Info {
Expand All @@ -75,14 +75,14 @@ impl From<Vec<(usize, Data)>> for BrCode {
})
.collect::<Vec<MerchantInfo>>();
let templates = (80usize..=99usize)
.map(|i| (i, hash.get(&i).clone()))
.map(|i| (i, hash.get(&i)))
.filter_map(|e| {
e.1.map(|d| {
Some(Template {
id: e.0,
info: {
let hm = d.to_hash();
let mut keys = hm.keys().map(|k| *k).collect::<Vec<usize>>();
let mut keys = hm.keys().copied().collect::<Vec<usize>>();
keys.sort();
keys.into_iter()
.map(|idx| Info {
Expand All @@ -99,12 +99,12 @@ impl From<Vec<(usize, Data)>> for BrCode {
BrCode {
payload_version: hash[&0usize].to_str().parse().unwrap(),
initiation_methos: hash.get(&1usize).map(|e| e.to_str().parse().unwrap()),
merchant_account_information: hash.get(&4usize).map(|e| e.to_str()),
merchant_account_information: hash.get(&4usize).map(crate::aux::Data::to_str),
merchant_information: merchant_information,
merchant_category_code: hash[&52usize].to_str().parse().unwrap(),
merchant_name: hash[&59usize].to_str(),
merchant_city: hash[&60usize].to_str(),
postal_code: hash.get(&61usize).map(|e| e.to_str()),
postal_code: hash.get(&61usize).map(crate::aux::Data::to_str),
currency: hash[&53usize].to_str(),
amount: hash.get(&54usize).map(|e| e.to_str().parse().unwrap()),
country_code: hash[&58usize].to_str(),
Expand Down
3 changes: 1 addition & 2 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ pub(crate) fn parse(code: &str, max: usize) -> Vec<(usize, Data)> {
(0usize..=max)
.filter_map(|_| parse_code(&mut chars))
.map(|code| match code.0 {
26..=51 => (code.0, Data::Vector(inner_parse(&code.1, 99))),
80..=99 => (code.0, Data::Vector(inner_parse(&code.1, 99))),
26..=51 | 80..=98 => (code.0, Data::Vector(inner_parse(&code.1, 99))),
62 => (code.0, Data::Vector(inner_parse(&code.1, 25))),
_ => (code.0, Data::Single(code.1)),
})
Expand Down
4 changes: 2 additions & 2 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn test_from_str() {

#[test]
fn test_to_string() {
let actual = brcode::to_string(data_expected());
let actual = brcode::to_string(&data_expected());
let expected = code();

assert_eq!(actual, expected);
Expand All @@ -19,7 +19,7 @@ fn test_to_string() {
#[test]
fn assert_both_ways() {
let from = brcode::from_str(&dynamic_code());
let to = brcode::to_string(from);
let to = brcode::to_string(&from);

assert_eq!(to, dynamic_code())
}
Expand Down

0 comments on commit 018524b

Please sign in to comment.