Skip to content

Commit 819daa3

Browse files
authoredNov 26, 2022
Optimize algorithm, generic (#31)
1 parent b070537 commit 819daa3

File tree

4 files changed

+281
-209
lines changed

4 files changed

+281
-209
lines changed
 

‎README.md

+84-96
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
## Abilities
1616
#### `TryFromHex` trait
17-
- Convert `Hex` to `Num`
17+
- Convert hex to num
1818
- type `AsRef<str> -> isize`
1919
- type `AsRef<str> -> i8`
2020
- type `AsRef<str> -> i16`
@@ -29,21 +29,21 @@
2929
- type `AsRef<str> -> u128`
3030

3131
#### `bytes` prefixed functions
32-
- Convert `Bytes` to `Hex`
32+
- Convert bytes to hex
3333
- type `AsRef<[u8]> -> String`
3434

3535
#### `hex` prefixed functions
36-
- Convert `HexBytes` to `Hex`
36+
- Convert `HexBytes` to hex
3737
- type `&[u8] -> &str`
3838
- e.g. `b"0x..." -> "0x..."`
39-
- Transform `Hex` from `Array`
39+
- Transform hex from `Array`
4040
- type `&str -> [u8; N]`
41-
- Convert `Hex` to `Bytes`
42-
- type `AsRef<str> -> Vec<u8>`
43-
- Convert `Hex` to `Slice`
44-
- type `AsRef<str> -> &[u8]`
45-
- Transform `Hex` to `T`
46-
- type `AsRef<str> -> T`
41+
- Convert hex to bytes
42+
- type `AsRef<[u8]> -> Vec<u8>`
43+
- Convert hex to `Slice`
44+
- type `AsRef<[u8]> -> &[u8]`
45+
- Transform hex to `T`
46+
- type `AsRef<[u8]> -> T`
4747
- e.g. `"0x..." -> [u8; 20] -> H160`
4848

4949
#### `slice` prefixed functions
@@ -72,123 +72,111 @@
7272
- e.g. `"0x00" -> vec![0_u8]`
7373

7474
## Benchmark results
75-
<div align="right"><sub>Friday, November 25th, 2022</sub></div>
75+
<div align="right"><sub>Friday, November 26th, 2022</sub></div>
7676

7777
```rs
78-
array_bytes::bytes2hex time: [38.078 µs 38.126 µs 38.177 µs]
79-
change: [-0.5147% -0.2140% +0.1154%] (p = 0.18 > 0.05)
80-
No change in performance detected.
81-
Found 14 outliers among 100 measurements (14.00%)
82-
1 (1.00%) low severe
83-
2 (2.00%) high mild
84-
11 (11.00%) high severe
78+
array_bytes::bytes2hex time: [37.241 µs 37.321 µs 37.407 µs]
79+
change: [-2.2373% -1.9757% -1.7126%] (p = 0.00 < 0.05)
80+
Performance has improved.
81+
Found 4 outliers among 100 measurements (4.00%)
82+
4 (4.00%) high mild
8583

86-
hex::encode time: [136.19 µs 136.65 µs 137.14 µs]
87-
change: [-0.3002% -0.0359% +0.2459%] (p = 0.81 > 0.05)
88-
No change in performance detected.
89-
Found 9 outliers among 100 measurements (9.00%)
90-
2 (2.00%) low mild
91-
6 (6.00%) high mild
84+
hex::encode time: [132.17 µs 132.42 µs 132.66 µs]
85+
change: [-2.4772% -2.2353% -1.9952%] (p = 0.00 < 0.05)
86+
Performance has improved.
87+
Found 5 outliers among 100 measurements (5.00%)
88+
4 (4.00%) high mild
9289
1 (1.00%) high severe
9390

94-
rustc_hex::to_hex time: [79.155 µs 79.268 µs 79.398 µs]
95-
change: [-2.9058% -1.8791% -0.9713%] (p = 0.00 < 0.05)
96-
Change within noise threshold.
91+
rustc_hex::to_hex time: [77.565 µs 77.885 µs 78.236 µs]
92+
change: [-1.7109% -1.4392% -1.1561%] (p = 0.00 < 0.05)
93+
Performance has improved.
9794
Found 5 outliers among 100 measurements (5.00%)
9895
4 (4.00%) high mild
9996
1 (1.00%) high severe
10097

101-
faster_hex::hex_string time: [18.483 µs 18.524 µs 18.565 µs]
102-
change: [-0.6456% -0.3159% -0.0313%] (p = 0.04 < 0.05)
103-
Change within noise threshold.
104-
Found 11 outliers among 100 measurements (11.00%)
105-
1 (1.00%) low mild
106-
8 (8.00%) high mild
98+
faster_hex::hex_string time: [18.049 µs 18.091 µs 18.140 µs]
99+
change: [-2.1506% -1.7957% -1.3953%] (p = 0.00 < 0.05)
100+
Performance has improved.
101+
Found 7 outliers among 100 measurements (7.00%)
102+
5 (5.00%) high mild
107103
2 (2.00%) high severe
108104

109105
faster_hex::hex_encode_fallback
110-
time: [18.497 µs 18.528 µs 18.561 µs]
111-
change: [-11.257% -4.9349% -0.9424%] (p = 0.08 > 0.05)
112-
No change in performance detected.
113-
Found 8 outliers among 100 measurements (8.00%)
114-
6 (6.00%) high mild
115-
2 (2.00%) high severe
116-
117-
array_bytes::hex2bytes time: [224.34 µs 224.59 µs 224.86 µs]
118-
change: [-1.7703% -1.2368% -0.7744%] (p = 0.00 < 0.05)
119-
Change within noise threshold.
120-
Found 4 outliers among 100 measurements (4.00%)
121-
3 (3.00%) high mild
106+
time: [17.978 µs 18.018 µs 18.064 µs]
107+
change: [-2.6657% -2.3283% -1.9846%] (p = 0.00 < 0.05)
108+
Performance has improved.
109+
Found 2 outliers among 100 measurements (2.00%)
110+
1 (1.00%) high mild
122111
1 (1.00%) high severe
123112

113+
array_bytes::hex2bytes time: [119.27 µs 119.54 µs 119.81 µs]
114+
change: [-2.5026% -2.2957% -2.0423%] (p = 0.00 < 0.05)
115+
Performance has improved.
116+
Found 14 outliers among 100 measurements (14.00%)
117+
11 (11.00%) high mild
118+
3 (3.00%) high severe
119+
124120
array_bytes::hex2bytes_unchecked
125-
time: [222.78 µs 223.07 µs 223.39 µs]
126-
change: [-0.5184% -0.1710% +0.1429%] (p = 0.32 > 0.05)
127-
No change in performance detected.
128-
Found 9 outliers among 100 measurements (9.00%)
129-
7 (7.00%) high mild
121+
time: [82.136 µs 82.324 µs 82.531 µs]
122+
change: [-55.176% -53.193% -52.029%] (p = 0.00 < 0.05)
123+
Performance has improved.
124+
Found 15 outliers among 100 measurements (15.00%)
125+
13 (13.00%) high mild
130126
2 (2.00%) high severe
131127

132-
array_bytes::hex2slice time: [211.37 µs 211.49 µs 211.62 µs]
133-
change: [-3.1739% -2.1127% -1.2688%] (p = 0.00 < 0.05)
134-
Performance has improved.
135-
Found 6 outliers among 100 measurements (6.00%)
136-
2 (2.00%) low severe
137-
4 (4.00%) high severe
128+
array_bytes::hex2slice time: [112.94 µs 113.32 µs 113.78 µs]
129+
change: [-1.6410% -1.1545% -0.6772%] (p = 0.00 < 0.05)
130+
Change within noise threshold.
131+
Found 3 outliers among 100 measurements (3.00%)
132+
2 (2.00%) high mild
133+
1 (1.00%) high severe
138134

139135
array_bytes::hex2slice_unchecked
140-
time: [212.00 µs 212.34 µs 212.71 µs]
141-
change: [-0.8427% -0.5482% -0.2810%] (p = 0.00 < 0.05)
142-
Change within noise threshold.
143-
Found 9 outliers among 100 measurements (9.00%)
144-
1 (1.00%) low mild
145-
6 (6.00%) high mild
146-
2 (2.00%) high severe
136+
time: [89.416 µs 89.650 µs 89.956 µs]
137+
change: [-22.750% -22.423% -22.099%] (p = 0.00 < 0.05)
138+
Performance has improved.
139+
Found 14 outliers among 100 measurements (14.00%)
140+
8 (8.00%) high mild
141+
6 (6.00%) high severe
147142

148-
hex::decode time: [244.37 µs 244.78 µs 245.25 µs]
149-
change: [-1.4130% -1.0496% -0.7133%] (p = 0.00 < 0.05)
143+
hex::decode time: [239.97 µs 240.64 µs 241.33 µs]
144+
change: [+0.3733% +0.6910% +1.0245%] (p = 0.00 < 0.05)
150145
Change within noise threshold.
151-
Found 8 outliers among 100 measurements (8.00%)
152-
5 (5.00%) high mild
153-
3 (3.00%) high severe
146+
Found 1 outliers among 100 measurements (1.00%)
147+
1 (1.00%) high mild
154148

155-
hex::decode_to_slice time: [166.67 µs 166.90 µs 167.16 µs]
156-
change: [+0.1484% +0.3293% +0.5160%] (p = 0.00 < 0.05)
157-
Change within noise threshold.
158-
Found 3 outliers among 100 measurements (3.00%)
149+
hex::decode_to_slice time: [162.75 µs 163.12 µs 163.61 µs]
150+
change: [-0.4036% -0.0331% +0.3614%] (p = 0.86 > 0.05)
151+
No change in performance detected.
152+
Found 7 outliers among 100 measurements (7.00%)
159153
3 (3.00%) high mild
154+
4 (4.00%) high severe
160155

161-
rustc_hex::from_hex time: [176.56 µs 177.79 µs 179.13 µs]
162-
change: [+1.4009% +2.5404% +3.5866%] (p = 0.00 < 0.05)
163-
Performance has regressed.
164-
Found 5 outliers among 100 measurements (5.00%)
165-
2 (2.00%) low mild
166-
1 (1.00%) high mild
167-
2 (2.00%) high severe
156+
rustc_hex::from_hex time: [166.34 µs 167.65 µs 169.03 µs]
157+
change: [-1.5255% -0.5122% +0.5735%] (p = 0.33 > 0.05)
158+
No change in performance detected.
168159

169-
faster_hex::hex_decode time: [39.127 µs 39.342 µs 39.582 µs]
170-
change: [-0.0442% +0.3160% +0.6546%] (p = 0.08 > 0.05)
160+
faster_hex::hex_decode time: [38.419 µs 38.613 µs 38.812 µs]
161+
change: [-0.9090% -0.3666% +0.1714%] (p = 0.19 > 0.05)
171162
No change in performance detected.
172-
Found 18 outliers among 100 measurements (18.00%)
173-
3 (3.00%) low severe
174-
8 (8.00%) high mild
175-
7 (7.00%) high severe
163+
Found 4 outliers among 100 measurements (4.00%)
164+
4 (4.00%) high mild
176165

177166
faster_hex::hex_decode_unchecked
178-
time: [16.429 µs 16.479 µs 16.538 µs]
179-
change: [+0.5738% +0.9176% +1.3113%] (p = 0.00 < 0.05)
180-
Change within noise threshold.
181-
Found 14 outliers among 100 measurements (14.00%)
182-
6 (6.00%) high mild
183-
8 (8.00%) high severe
167+
time: [16.122 µs 16.166 µs 16.212 µs]
168+
change: [-0.2496% +0.1886% +0.6435%] (p = 0.41 > 0.05)
169+
No change in performance detected.
170+
Found 1 outliers among 100 measurements (1.00%)
171+
1 (1.00%) high mild
184172

185173
faster_hex::hex_decode_fallback
186-
time: [16.422 µs 16.440 µs 16.460 µs]
187-
change: [+0.3595% +0.6397% +0.9141%] (p = 0.00 < 0.05)
174+
time: [16.001 µs 16.039 µs 16.081 µs]
175+
change: [-1.1315% -0.7797% -0.4279%] (p = 0.00 < 0.05)
188176
Change within noise threshold.
189-
Found 5 outliers among 100 measurements (5.00%)
190-
1 (1.00%) high mild
191-
4 (4.00%) high severe
177+
Found 8 outliers among 100 measurements (8.00%)
178+
6 (6.00%) high mild
179+
2 (2.00%) high severe
192180
```
193181

194182
<div align="right">

‎fuzz/fuzz_targets/bytes_hex_conversion.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
libfuzzer_sys::fuzz_target!(|data: &[u8]| {
44
let _ = array_bytes::bytes2hex("", data);
55
let _ = array_bytes::hex_bytes2hex_str(data);
6-
let _ = array_bytes::hex2bytes(&String::from_utf8_lossy(data));
6+
let _ = array_bytes::hex2bytes(data);
77
{
88
let mut v = vec![0; data.len() / 2];
9-
let _ = array_bytes::hex2slice(&String::from_utf8_lossy(data), &mut v);
9+
let _ = array_bytes::hex2slice(data, &mut v);
1010
}
1111
});

0 commit comments

Comments
 (0)