@@ -52,8 +52,11 @@ cfg_if::cfg_if! {
52
52
}
53
53
}
54
54
55
+ mod lru;
55
56
mod stash;
56
57
58
+ use lru:: Lru ;
59
+
57
60
const MAPPINGS_CACHE_SIZE : usize = 4 ;
58
61
59
62
struct Mapping {
@@ -263,7 +266,7 @@ struct Cache {
263
266
///
264
267
/// Note that this is basically an LRU cache and we'll be shifting things
265
268
/// around in here as we symbolize addresses.
266
- mappings : Vec < ( usize , Mapping ) > ,
269
+ mappings : Lru < ( usize , Mapping ) , MAPPINGS_CACHE_SIZE > ,
267
270
}
268
271
269
272
struct Library {
@@ -344,7 +347,7 @@ pub unsafe fn clear_symbol_cache() {
344
347
impl Cache {
345
348
fn new ( ) -> Cache {
346
349
Cache {
347
- mappings : Vec :: with_capacity ( MAPPINGS_CACHE_SIZE ) ,
350
+ mappings : Lru :: default ( ) ,
348
351
libraries : native_libraries ( ) ,
349
352
}
350
353
}
@@ -403,31 +406,18 @@ impl Cache {
403
406
}
404
407
405
408
fn mapping_for_lib < ' a > ( & ' a mut self , lib : usize ) -> Option < ( & ' a mut Context < ' a > , & ' a Stash ) > {
406
- let idx = self . mappings . iter ( ) . position ( |( idx, _) | * idx == lib) ;
407
-
408
- // Invariant: after this conditional completes without early returning
409
- // from an error, the cache entry for this path is at index 0.
409
+ let cache_idx = self . mappings . iter ( ) . position ( |( lib_id, _) | * lib_id == lib) ;
410
410
411
- if let Some ( idx) = idx {
412
- // When the mapping is already in the cache, move it to the front.
413
- if idx != 0 {
414
- let entry = self . mappings . remove ( idx) ;
415
- self . mappings . insert ( 0 , entry) ;
416
- }
411
+ let cache_entry = if let Some ( idx) = cache_idx {
412
+ self . mappings . move_to_front ( idx)
417
413
} else {
418
- // When the mapping is not in the cache, create a new mapping,
419
- // insert it into the front of the cache, and evict the oldest cache
420
- // entry if necessary.
421
- let mapping = create_mapping ( & self . libraries [ lib] ) ?;
422
-
423
- if self . mappings . len ( ) == MAPPINGS_CACHE_SIZE {
424
- self . mappings . pop ( ) ;
425
- }
426
-
427
- self . mappings . insert ( 0 , ( lib, mapping) ) ;
428
- }
414
+ // When the mapping is not in the cache, create a new mapping and insert it,
415
+ // which will also evict the oldest entry.
416
+ create_mapping ( & self . libraries [ lib] )
417
+ . and_then ( |mapping| self . mappings . push_front ( ( lib, mapping) ) )
418
+ } ;
429
419
430
- let mapping = & mut self . mappings [ 0 ] . 1 ;
420
+ let ( _ , mapping) = cache_entry? ;
431
421
let cx: & ' a mut Context < ' static > = & mut mapping. cx ;
432
422
let stash: & ' a Stash = & mapping. stash ;
433
423
// don't leak the `'static` lifetime, make sure it's scoped to just
0 commit comments