mem.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #![feature(test)]
  2. extern crate test;
  3. use test::{black_box, Bencher};
  4. extern crate compiler_builtins;
  5. use compiler_builtins::mem::{memcmp, memcpy, memmove, memset};
  6. fn memcpy_builtin(b: &mut Bencher, n: usize, offset: usize) {
  7. let v1 = vec![1u8; n + offset];
  8. let mut v2 = vec![0u8; n + offset];
  9. b.bytes = n as u64;
  10. b.iter(|| {
  11. let src: &[u8] = black_box(&v1[offset..]);
  12. let dst: &mut [u8] = black_box(&mut v2[offset..]);
  13. dst.copy_from_slice(src);
  14. })
  15. }
  16. fn memcpy_rust(b: &mut Bencher, n: usize, offset: usize) {
  17. let v1 = vec![1u8; n + offset];
  18. let mut v2 = vec![0u8; n + offset];
  19. b.bytes = n as u64;
  20. b.iter(|| {
  21. let src: &[u8] = black_box(&v1[offset..]);
  22. let dst: &mut [u8] = black_box(&mut v2[offset..]);
  23. unsafe { memcpy(dst.as_mut_ptr(), src.as_ptr(), n) }
  24. })
  25. }
  26. fn memset_builtin(b: &mut Bencher, n: usize, offset: usize) {
  27. let mut v1 = vec![0u8; n + offset];
  28. b.bytes = n as u64;
  29. b.iter(|| {
  30. let dst: &mut [u8] = black_box(&mut v1[offset..]);
  31. let val: u8 = black_box(27);
  32. for b in dst {
  33. *b = val;
  34. }
  35. })
  36. }
  37. fn memset_rust(b: &mut Bencher, n: usize, offset: usize) {
  38. let mut v1 = vec![0u8; n + offset];
  39. b.bytes = n as u64;
  40. b.iter(|| {
  41. let dst: &mut [u8] = black_box(&mut v1[offset..]);
  42. let val = black_box(27);
  43. unsafe { memset(dst.as_mut_ptr(), val, n) }
  44. })
  45. }
  46. fn memcmp_builtin(b: &mut Bencher, n: usize) {
  47. let v1 = vec![0u8; n];
  48. let mut v2 = vec![0u8; n];
  49. v2[n - 1] = 1;
  50. b.bytes = n as u64;
  51. b.iter(|| {
  52. let s1: &[u8] = black_box(&v1);
  53. let s2: &[u8] = black_box(&v2);
  54. s1.cmp(s2)
  55. })
  56. }
  57. fn memcmp_rust(b: &mut Bencher, n: usize) {
  58. let v1 = vec![0u8; n];
  59. let mut v2 = vec![0u8; n];
  60. v2[n - 1] = 1;
  61. b.bytes = n as u64;
  62. b.iter(|| {
  63. let s1: &[u8] = black_box(&v1);
  64. let s2: &[u8] = black_box(&v2);
  65. unsafe { memcmp(s1.as_ptr(), s2.as_ptr(), n) }
  66. })
  67. }
  68. fn memmove_builtin(b: &mut Bencher, n: usize) {
  69. let mut v = vec![0u8; n + n / 2];
  70. b.bytes = n as u64;
  71. b.iter(|| {
  72. let s: &mut [u8] = black_box(&mut v);
  73. s.copy_within(0..n, n / 2);
  74. })
  75. }
  76. fn memmove_rust(b: &mut Bencher, n: usize) {
  77. let mut v = vec![0u8; n + n / 2];
  78. b.bytes = n as u64;
  79. b.iter(|| {
  80. let dst: *mut u8 = black_box(&mut v[n / 2..]).as_mut_ptr();
  81. let src: *const u8 = black_box(&v).as_ptr();
  82. unsafe { memmove(dst, src, n) };
  83. })
  84. }
  85. #[bench]
  86. fn memcpy_builtin_4096(b: &mut Bencher) {
  87. memcpy_builtin(b, 4096, 0)
  88. }
  89. #[bench]
  90. fn memcpy_rust_4096(b: &mut Bencher) {
  91. memcpy_rust(b, 4096, 0)
  92. }
  93. #[bench]
  94. fn memcpy_builtin_1048576(b: &mut Bencher) {
  95. memcpy_builtin(b, 1048576, 0)
  96. }
  97. #[bench]
  98. fn memcpy_rust_1048576(b: &mut Bencher) {
  99. memcpy_rust(b, 1048576, 0)
  100. }
  101. #[bench]
  102. fn memcpy_builtin_4096_offset(b: &mut Bencher) {
  103. memcpy_builtin(b, 4096, 65)
  104. }
  105. #[bench]
  106. fn memcpy_rust_4096_offset(b: &mut Bencher) {
  107. memcpy_rust(b, 4096, 65)
  108. }
  109. #[bench]
  110. fn memcpy_builtin_1048576_offset(b: &mut Bencher) {
  111. memcpy_builtin(b, 1048576, 65)
  112. }
  113. #[bench]
  114. fn memcpy_rust_1048576_offset(b: &mut Bencher) {
  115. memcpy_rust(b, 1048576, 65)
  116. }
  117. #[bench]
  118. fn memset_builtin_4096(b: &mut Bencher) {
  119. memset_builtin(b, 4096, 0)
  120. }
  121. #[bench]
  122. fn memset_rust_4096(b: &mut Bencher) {
  123. memset_rust(b, 4096, 0)
  124. }
  125. #[bench]
  126. fn memset_builtin_1048576(b: &mut Bencher) {
  127. memset_builtin(b, 1048576, 0)
  128. }
  129. #[bench]
  130. fn memset_rust_1048576(b: &mut Bencher) {
  131. memset_rust(b, 1048576, 0)
  132. }
  133. #[bench]
  134. fn memset_builtin_4096_offset(b: &mut Bencher) {
  135. memset_builtin(b, 4096, 65)
  136. }
  137. #[bench]
  138. fn memset_rust_4096_offset(b: &mut Bencher) {
  139. memset_rust(b, 4096, 65)
  140. }
  141. #[bench]
  142. fn memset_builtin_1048576_offset(b: &mut Bencher) {
  143. memset_builtin(b, 1048576, 65)
  144. }
  145. #[bench]
  146. fn memset_rust_1048576_offset(b: &mut Bencher) {
  147. memset_rust(b, 1048576, 65)
  148. }
  149. #[bench]
  150. fn memcmp_builtin_4096(b: &mut Bencher) {
  151. memcmp_builtin(b, 4096)
  152. }
  153. #[bench]
  154. fn memcmp_rust_4096(b: &mut Bencher) {
  155. memcmp_rust(b, 4096)
  156. }
  157. #[bench]
  158. fn memcmp_builtin_1048576(b: &mut Bencher) {
  159. memcmp_builtin(b, 1048576)
  160. }
  161. #[bench]
  162. fn memcmp_rust_1048576(b: &mut Bencher) {
  163. memcmp_rust(b, 1048576)
  164. }
  165. #[bench]
  166. fn memmove_builtin_4096(b: &mut Bencher) {
  167. memmove_builtin(b, 4096)
  168. }
  169. #[bench]
  170. fn memmove_rust_4096(b: &mut Bencher) {
  171. memmove_rust(b, 4096)
  172. }
  173. #[bench]
  174. fn memmove_builtin_1048576(b: &mut Bencher) {
  175. memmove_builtin(b, 1048576)
  176. }
  177. #[bench]
  178. fn memmove_rust_1048576(b: &mut Bencher) {
  179. memmove_rust(b, 1048576)
  180. }