@@ -21,32 +21,23 @@ fn _byte_pair_merge<T>(
21
21
// The rank of the last item in the vector is not a valid value.
22
22
let mut parts: Vec < ( usize , usize ) > = ( 0 ..piece. len ( ) + 1 ) . map ( |i| ( i, usize:: MAX ) ) . collect ( ) ;
23
23
24
- // NOTE: using a macro here because a closure fails to get inlined
25
- // according to optimization remarks.
26
- // A closure also cannot capture a reference to `piece` without
27
- // the borrow checker complaining about the mutable borrows during
28
- // the assignments later in this code.
29
- macro_rules! get_rank {
30
- ( $start_idx: expr, $skip: expr) => { {
31
- let start_idx: usize = $start_idx;
32
- let skip: usize = $skip;
24
+ let get_rank = {
25
+ #[ inline( always) ]
26
+ |parts : & Vec < ( usize , usize ) > , start_idx : usize , skip : usize | {
33
27
if ( start_idx + skip + 2 ) < parts. len ( ) {
34
28
ranks
35
29
. get ( & piece[ parts[ start_idx] . 0 ..parts[ start_idx + skip + 2 ] . 0 ] )
36
- . map ( |r| * r )
30
+ . copied ( )
37
31
} else {
38
32
None
39
33
}
40
- } } ;
41
- ( $idx: expr) => { {
42
- get_rank!( $idx, 0 )
43
- } } ;
44
- }
34
+ }
35
+ } ;
45
36
46
37
// We look up the ranks once in the beggining and iteratively update
47
38
// them during each merge, which reduces the number of rank lookups.
48
39
for i in 0 ..parts. len ( ) - 2 {
49
- match get_rank ! ( i ) {
40
+ match get_rank ( & parts , i , 0 ) {
50
41
Some ( rank) => {
51
42
// usize::MAX is a sentinel value and cannot be a valid rank
52
43
debug_assert ! ( rank != usize :: MAX ) ;
@@ -89,9 +80,9 @@ fn _byte_pair_merge<T>(
89
80
// parts[i] and parts[i-1] before removing, which could thrash
90
81
// the cache. Thus, we update the rank calculation by skipping over
91
82
// parts[i + 1], by invoking `get_rank!` with `skip = 1`.
92
- parts[ i] . 1 = get_rank ! ( i, 1 ) . unwrap_or ( usize:: MAX ) ;
83
+ parts[ i] . 1 = get_rank ( & parts , i, 1 ) . unwrap_or ( usize:: MAX ) ;
93
84
if i > 0 {
94
- parts[ i - 1 ] . 1 = get_rank ! ( i - 1 , 1 ) . unwrap_or ( usize:: MAX ) ;
85
+ parts[ i - 1 ] . 1 = get_rank ( & parts , i - 1 , 1 ) . unwrap_or ( usize:: MAX ) ;
95
86
}
96
87
97
88
parts. remove ( i + 1 ) ;
0 commit comments