@@ -52,8 +52,11 @@ cfg_if::cfg_if! {
5252 }
5353}
5454
55+ mod lru;
5556mod stash;
5657
58+ use lru:: Lru ;
59+
5760const MAPPINGS_CACHE_SIZE : usize = 4 ;
5861
5962struct Mapping {
@@ -263,7 +266,7 @@ struct Cache {
263266 ///
264267 /// Note that this is basically an LRU cache and we'll be shifting things
265268 /// around in here as we symbolize addresses.
266- mappings : Vec < ( usize , Mapping ) > ,
269+ mappings : Lru < ( usize , Mapping ) , MAPPINGS_CACHE_SIZE > ,
267270}
268271
269272struct Library {
@@ -344,7 +347,7 @@ pub unsafe fn clear_symbol_cache() {
344347impl Cache {
345348 fn new ( ) -> Cache {
346349 Cache {
347- mappings : Vec :: with_capacity ( MAPPINGS_CACHE_SIZE ) ,
350+ mappings : Lru :: default ( ) ,
348351 libraries : native_libraries ( ) ,
349352 }
350353 }
@@ -403,31 +406,18 @@ impl Cache {
403406 }
404407
405408 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) ;
410410
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)
417413 } 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+ } ;
429419
430- let mapping = & mut self . mappings [ 0 ] . 1 ;
420+ let ( _ , mapping) = cache_entry? ;
431421 let cx: & ' a mut Context < ' static > = & mut mapping. cx ;
432422 let stash: & ' a Stash = & mapping. stash ;
433423 // don't leak the `'static` lifetime, make sure it's scoped to just
0 commit comments