123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- #![feature(test)]
- extern crate test;
- use test::{black_box, Bencher};
- extern crate compiler_builtins;
- use compiler_builtins::mem::{memcmp, memcpy, memmove, memset};
- fn memcpy_builtin(b: &mut Bencher, n: usize, offset: usize) {
- let v1 = vec![1u8; n + offset];
- let mut v2 = vec![0u8; n + offset];
- b.bytes = n as u64;
- b.iter(|| {
- let src: &[u8] = black_box(&v1[offset..]);
- let dst: &mut [u8] = black_box(&mut v2[offset..]);
- dst.copy_from_slice(src);
- })
- }
- fn memcpy_rust(b: &mut Bencher, n: usize, offset: usize) {
- let v1 = vec![1u8; n + offset];
- let mut v2 = vec![0u8; n + offset];
- b.bytes = n as u64;
- b.iter(|| {
- let src: &[u8] = black_box(&v1[offset..]);
- let dst: &mut [u8] = black_box(&mut v2[offset..]);
- unsafe { memcpy(dst.as_mut_ptr(), src.as_ptr(), n) }
- })
- }
- fn memset_builtin(b: &mut Bencher, n: usize, offset: usize) {
- let mut v1 = vec![0u8; n + offset];
- b.bytes = n as u64;
- b.iter(|| {
- let dst: &mut [u8] = black_box(&mut v1[offset..]);
- let val: u8 = black_box(27);
- for b in dst {
- *b = val;
- }
- })
- }
- fn memset_rust(b: &mut Bencher, n: usize, offset: usize) {
- let mut v1 = vec![0u8; n + offset];
- b.bytes = n as u64;
- b.iter(|| {
- let dst: &mut [u8] = black_box(&mut v1[offset..]);
- let val = black_box(27);
- unsafe { memset(dst.as_mut_ptr(), val, n) }
- })
- }
- fn memcmp_builtin(b: &mut Bencher, n: usize) {
- let v1 = vec![0u8; n];
- let mut v2 = vec![0u8; n];
- v2[n - 1] = 1;
- b.bytes = n as u64;
- b.iter(|| {
- let s1: &[u8] = black_box(&v1);
- let s2: &[u8] = black_box(&v2);
- s1.cmp(s2)
- })
- }
- fn memcmp_rust(b: &mut Bencher, n: usize) {
- let v1 = vec![0u8; n];
- let mut v2 = vec![0u8; n];
- v2[n - 1] = 1;
- b.bytes = n as u64;
- b.iter(|| {
- let s1: &[u8] = black_box(&v1);
- let s2: &[u8] = black_box(&v2);
- unsafe { memcmp(s1.as_ptr(), s2.as_ptr(), n) }
- })
- }
- fn memmove_builtin(b: &mut Bencher, n: usize) {
- let mut v = vec![0u8; n + n / 2];
- b.bytes = n as u64;
- b.iter(|| {
- let s: &mut [u8] = black_box(&mut v);
- s.copy_within(0..n, n / 2);
- })
- }
- fn memmove_rust(b: &mut Bencher, n: usize) {
- let mut v = vec![0u8; n + n / 2];
- b.bytes = n as u64;
- b.iter(|| {
- let dst: *mut u8 = black_box(&mut v[n / 2..]).as_mut_ptr();
- let src: *const u8 = black_box(&v).as_ptr();
- unsafe { memmove(dst, src, n) };
- })
- }
- #[bench]
- fn memcpy_builtin_4096(b: &mut Bencher) {
- memcpy_builtin(b, 4096, 0)
- }
- #[bench]
- fn memcpy_rust_4096(b: &mut Bencher) {
- memcpy_rust(b, 4096, 0)
- }
- #[bench]
- fn memcpy_builtin_1048576(b: &mut Bencher) {
- memcpy_builtin(b, 1048576, 0)
- }
- #[bench]
- fn memcpy_rust_1048576(b: &mut Bencher) {
- memcpy_rust(b, 1048576, 0)
- }
- #[bench]
- fn memcpy_builtin_4096_offset(b: &mut Bencher) {
- memcpy_builtin(b, 4096, 65)
- }
- #[bench]
- fn memcpy_rust_4096_offset(b: &mut Bencher) {
- memcpy_rust(b, 4096, 65)
- }
- #[bench]
- fn memcpy_builtin_1048576_offset(b: &mut Bencher) {
- memcpy_builtin(b, 1048576, 65)
- }
- #[bench]
- fn memcpy_rust_1048576_offset(b: &mut Bencher) {
- memcpy_rust(b, 1048576, 65)
- }
- #[bench]
- fn memset_builtin_4096(b: &mut Bencher) {
- memset_builtin(b, 4096, 0)
- }
- #[bench]
- fn memset_rust_4096(b: &mut Bencher) {
- memset_rust(b, 4096, 0)
- }
- #[bench]
- fn memset_builtin_1048576(b: &mut Bencher) {
- memset_builtin(b, 1048576, 0)
- }
- #[bench]
- fn memset_rust_1048576(b: &mut Bencher) {
- memset_rust(b, 1048576, 0)
- }
- #[bench]
- fn memset_builtin_4096_offset(b: &mut Bencher) {
- memset_builtin(b, 4096, 65)
- }
- #[bench]
- fn memset_rust_4096_offset(b: &mut Bencher) {
- memset_rust(b, 4096, 65)
- }
- #[bench]
- fn memset_builtin_1048576_offset(b: &mut Bencher) {
- memset_builtin(b, 1048576, 65)
- }
- #[bench]
- fn memset_rust_1048576_offset(b: &mut Bencher) {
- memset_rust(b, 1048576, 65)
- }
- #[bench]
- fn memcmp_builtin_4096(b: &mut Bencher) {
- memcmp_builtin(b, 4096)
- }
- #[bench]
- fn memcmp_rust_4096(b: &mut Bencher) {
- memcmp_rust(b, 4096)
- }
- #[bench]
- fn memcmp_builtin_1048576(b: &mut Bencher) {
- memcmp_builtin(b, 1048576)
- }
- #[bench]
- fn memcmp_rust_1048576(b: &mut Bencher) {
- memcmp_rust(b, 1048576)
- }
- #[bench]
- fn memmove_builtin_4096(b: &mut Bencher) {
- memmove_builtin(b, 4096)
- }
- #[bench]
- fn memmove_rust_4096(b: &mut Bencher) {
- memmove_rust(b, 4096)
- }
- #[bench]
- fn memmove_builtin_1048576(b: &mut Bencher) {
- memmove_builtin(b, 1048576)
- }
- #[bench]
- fn memmove_rust_1048576(b: &mut Bencher) {
- memmove_rust(b, 1048576)
- }
|