swc_ecma_loader/resolvers/
lru.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::num::NonZeroUsize;

use anyhow::Error;
use lru::LruCache;
use parking_lot::Mutex;
use swc_common::FileName;

use crate::resolve::{Resolution, Resolve};

#[derive(Debug)]
pub struct CachingResolver<R>
where
    R: Resolve,
{
    cache: Mutex<LruCache<(FileName, String), Resolution>>,
    inner: R,
}

impl<R> Default for CachingResolver<R>
where
    R: Resolve + Default,
{
    fn default() -> Self {
        Self::new(40, Default::default())
    }
}

impl<R> CachingResolver<R>
where
    R: Resolve,
{
    pub fn new(cap: usize, inner: R) -> Self {
        Self {
            cache: Mutex::new(LruCache::new(
                NonZeroUsize::try_from(cap).expect("cap == 0"),
            )),
            inner,
        }
    }
}

impl<R> Resolve for CachingResolver<R>
where
    R: Resolve,
{
    fn resolve(&self, base: &FileName, src: &str) -> Result<Resolution, Error> {
        {
            let mut lock = self.cache.lock();
            //
            if let Some(v) = lock.get(&(base.clone(), src.to_string())) {
                return Ok(v.clone());
            }
        }

        let resolved = self.inner.resolve(base, src)?;
        {
            let mut lock = self.cache.lock();
            lock.put((base.clone(), src.to_string()), resolved.clone());
        }

        Ok(resolved)
    }
}