Skip to content

Commit

Permalink
Optimize Key and KeyPtr primitives (#433)
Browse files Browse the repository at this point in the history
* [chores] add primitives/target to gitignore

* [primitives] add Key2 replacement for Key

# Conflicts:
#	primitives/src/lib.rs

* [primitives] add KeyPtr2 replacement for KeyPtr

* [primitives] add benchmark tests for Key, Key2, KeyPtr and KeyPtr2

* [primitives] rename some benches

* [primitives] inline some KeyPtr methods

This is mainly to make comparison between old and new KeyPtr fairer.

# Conflicts:
#	primitives/src/key_ptr.rs

* [primitives] carve out fallback for big-endian to_bytes

This makes it better testable.

* [primitives] include big-endian fallback procedures into a test

* [primitives] add doc comment to Key2

* [primitives] apply rustfmt

* [primitives] fix clippy warning

* [primitives] add repr(transparent) to Key2

* [primitives] replace AsRef<[u8; 32]> impl with Key2::try_as_bytes method

* [primitives] add SAFETY comment to unsafe block

* [primitives] apply rustfmt

* [primitives] add Display impl to Key2
  • Loading branch information
Robbepop authored Jun 13, 2020
1 parent 870095c commit f074786
Show file tree
Hide file tree
Showing 7 changed files with 525 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/core/target/
/model/target/
/lang/target/
/primitives/target/
/examples/**/target/
/design/

Expand Down
7 changes: 7 additions & 0 deletions primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,17 @@ tiny-keccak = { version = "2.0", features = ["keccak"] }
scale = { package = "parity-scale-codec", version = "1.3", default-features = false, features = ["derive", "full"] }
scale-info = { version = "0.1", default-features = false, features = ["derive"], optional = true }

[dev-dependencies]
criterion = "0.3.1"

[features]
default = ["std"]
std = [
"ink_prelude/std",
"scale/std",
"scale-info/std",
]

[[bench]]
name = "bench"
harness = false
156 changes: 156 additions & 0 deletions primitives/benches/bench.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
// Copyright 2018-2020 Parity Technologies (UK) Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

use criterion::{
black_box,
criterion_group,
criterion_main,
Criterion,
};
use ink_primitives::{
Key,
Key2,
KeyPtr,
KeyPtr2,
};

criterion_group!(
bench_key,
bench_key_add_assign_u64,
bench_key2_add_assign_u64,
bench_key2_add_assign_u64_one_ofvl,
bench_key2_add_assign_u64_two_ofvls,
bench_key2_add_assign_u64_three_ofvls,
bench_key2_add_assign_u64_wrap,
);
criterion_group!(
bench_key_ptr,
bench_key_ptr_advance_by,
bench_key_ptr2_advance_by,
bench_key_ptr_advance_by_repeat,
bench_key_ptr2_advance_by_repeat,
);
criterion_main!(bench_key, bench_key_ptr,);

fn bench_key_add_assign_u64(c: &mut Criterion) {
let key = Key([0x00; 32]);
c.bench_function("Key::add_assign(u64)", |b| {
b.iter(|| {
let mut copy = black_box(key);
let _ = black_box(copy += 1u64);
})
});
}

fn bench_key2_add_assign_u64(c: &mut Criterion) {
let key = Key2::from([0x00; 32]);
c.bench_function("Key2::add_assign(u64)", |b| {
b.iter(|| {
let mut copy = black_box(key);
let _ = black_box(copy += 1u64);
})
});
}

fn bench_key2_add_assign_u64_one_ofvl(c: &mut Criterion) {
let key = Key2::from([
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]);
c.bench_function("Key2::add_assign(u64) - 1 ofvl", |b| {
b.iter(|| {
let mut copy = black_box(key);
let _ = black_box(copy += 1u64);
})
});
}

fn bench_key2_add_assign_u64_two_ofvls(c: &mut Criterion) {
let key = Key2::from([
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]);
c.bench_function("Key2::add_assign(u64) - 2 ofvls", |b| {
b.iter(|| {
let mut copy = black_box(key);
let _ = black_box(copy += 1u64);
})
});
}

fn bench_key2_add_assign_u64_three_ofvls(c: &mut Criterion) {
let key = Key2::from([
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
]);
c.bench_function("Key2::add_assign(u64) - 3 ofvls", |b| {
b.iter(|| {
let mut copy = black_box(key);
let _ = black_box(copy += 1u64);
})
});
}

fn bench_key2_add_assign_u64_wrap(c: &mut Criterion) {
let key = Key2::from([0xFF; 32]);
c.bench_function("Key2::add_assign(u64) - wrap", |b| {
b.iter(|| {
let mut copy = black_box(key);
let _ = black_box(copy += 1u64);
})
});
}

fn bench_key_ptr_advance_by(c: &mut Criterion) {
let key = Key([0x00; 32]);
c.bench_function("KeyPtr::advance_by copy", |b| {
b.iter(|| {
let mut key_ptr = KeyPtr::from(key.clone());
let _ = black_box(key_ptr.advance_by(1));
})
});
}

fn bench_key_ptr2_advance_by(c: &mut Criterion) {
let key = Key2::from([0x00; 32]);
c.bench_function("KeyPtr2::advance_by copy", |b| {
b.iter(|| {
let mut key_ptr = KeyPtr2::from(key.clone());
let _ = black_box(key_ptr.advance_by(1));
})
});
}

fn bench_key_ptr_advance_by_repeat(c: &mut Criterion) {
let key = Key([0x00; 32]);
let mut key_ptr = KeyPtr::from(key.clone());
c.bench_function("KeyPtr::advance_by reuse", |b| {
b.iter(|| {
let _ = black_box(key_ptr.advance_by(1));
})
});
}

fn bench_key_ptr2_advance_by_repeat(c: &mut Criterion) {
let key = Key2::from([0x00; 32]);
let mut key_ptr = KeyPtr2::from(key.clone());
c.bench_function("KeyPtr2::advance_by reuse", |b| {
b.iter(|| {
let _ = black_box(key_ptr.advance_by(1));
})
});
}
Loading

0 comments on commit f074786

Please sign in to comment.