Skip to content

Commit f754b22

Browse files
committed
Removed useless Mutex
1 parent 8a8f5d9 commit f754b22

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

src/symbolize/gimli.rs

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use std::env;
2121
use std::fs::File;
2222
use std::path::{Path, PathBuf};
2323
use std::prelude::v1::*;
24-
use std::sync::Mutex;
2524

2625
const MAPPINGS_CACHE_SIZE: usize = 4;
2726

@@ -329,35 +328,33 @@ impl Mapping {
329328
}
330329
}
331330

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))) }
348347
}
349348

350349
pub fn clear_symbol_cache() {
351-
if let Ok(mut cache) = lazy_cache().lock() {
352-
cache.clear();
353-
}
350+
with_cache(|cache| cache.clear());
354351
}
355352

356353
fn with_mapping_for_path<F>(path: PathBuf, f: F)
357354
where
358355
F: FnMut(&Context<'_>),
359356
{
360-
if let Ok(mut cache) = lazy_cache().lock() {
357+
with_cache(|cache| {
361358
let idx = cache.iter().position(|&(ref p, _)| p == &path);
362359

363360
// Invariant: after this conditional completes without early returning
@@ -386,7 +383,7 @@ where
386383
}
387384

388385
cache[0].1.rent(f);
389-
}
386+
});
390387
}
391388

392389
pub unsafe fn resolve(what: ResolveWhat, cb: &mut FnMut(&super::Symbol)) {

0 commit comments

Comments
 (0)