@@ -21,7 +21,6 @@ use std::env;
21
21
use std:: fs:: File ;
22
22
use std:: path:: { Path , PathBuf } ;
23
23
use std:: prelude:: v1:: * ;
24
- use std:: sync:: Mutex ;
25
24
26
25
const MAPPINGS_CACHE_SIZE : usize = 4 ;
27
26
@@ -329,35 +328,33 @@ impl Mapping {
329
328
}
330
329
}
331
330
332
- // A very small, very simple LRU cache for debug info mappings.
333
- //
334
- // The hit rate should be very high, since the typical stack doesn't cross
335
- // between many shared libraries .
336
- //
337
- // The `addr2line::Context` structures are pretty expensive to create. Its
338
- // cost is expected to be amortized by subsequent `locate` queries, which
339
- // leverage the structures built when constructing `addr2line::Context`s to
340
- // get nice speedups. If we didn't have this cache, that amortization would
341
- // never happen, and symbolicating backtraces would be ssssllllooooowwww.
342
- static mut MAPPINGS_CACHE : Option < Mutex < Vec < ( PathBuf , Mapping ) > > > = None ;
343
-
344
- fn lazy_cache ( ) -> & ' static Mutex < Vec < ( PathBuf , Mapping ) > > {
345
- unsafe {
346
- MAPPINGS_CACHE . get_or_insert_with ( || Mutex :: new ( Vec :: with_capacity ( MAPPINGS_CACHE_SIZE ) ) )
347
- }
331
+ type Cache = Vec < ( PathBuf , Mapping ) > ;
332
+
333
+ fn with_cache ( f : impl FnOnce ( & mut Cache ) ) {
334
+ // A very small, very simple LRU cache for debug info mappings .
335
+ //
336
+ // The hit rate should be very high, since the typical stack doesn't cross
337
+ // between many shared libraries.
338
+ //
339
+ // The `addr2line::Context` structures are pretty expensive to create. Its
340
+ // cost is expected to be amortized by subsequent `locate` queries, which
341
+ // leverage the structures built when constructing `addr2line::Context`s to
342
+ // get nice speedups. If we didn't have this cache, that amortization would
343
+ // never happen, and symbolicating backtraces would be ssssllllooooowwww.
344
+ static mut MAPPINGS_CACHE : Option < Cache > = None ;
345
+
346
+ unsafe { f ( MAPPINGS_CACHE . get_or_insert_with ( || Vec :: with_capacity ( MAPPINGS_CACHE_SIZE ) ) ) }
348
347
}
349
348
350
349
pub fn clear_symbol_cache ( ) {
351
- if let Ok ( mut cache) = lazy_cache ( ) . lock ( ) {
352
- cache. clear ( ) ;
353
- }
350
+ with_cache ( |cache| cache. clear ( ) ) ;
354
351
}
355
352
356
353
fn with_mapping_for_path < F > ( path : PathBuf , f : F )
357
354
where
358
355
F : FnMut ( & Context < ' _ > ) ,
359
356
{
360
- if let Ok ( mut cache) = lazy_cache ( ) . lock ( ) {
357
+ with_cache ( | cache| {
361
358
let idx = cache. iter ( ) . position ( |& ( ref p, _) | p == & path) ;
362
359
363
360
// Invariant: after this conditional completes without early returning
@@ -386,7 +383,7 @@ where
386
383
}
387
384
388
385
cache[ 0 ] . 1 . rent ( f) ;
389
- }
386
+ } ) ;
390
387
}
391
388
392
389
pub unsafe fn resolve ( what : ResolveWhat , cb : & mut FnMut ( & super :: Symbol ) ) {
0 commit comments