sway_core/query_engine/
mod.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
use parking_lot::{RwLock, RwLockReadGuard, RwLockWriteGuard};
use std::{
    collections::HashMap,
    ops::{Deref, DerefMut},
    path::PathBuf,
    sync::Arc,
    time::SystemTime,
};
use sway_error::{error::CompileError, warning::CompileWarning};
use sway_types::{IdentUnique, ProgramId, SourceId, Spanned};

use crate::{
    decl_engine::{DeclId, DeclRef},
    language::ty::{TyFunctionDecl, TyFunctionSig, TyModule},
    {Engines, Programs},
};

#[derive(Debug, Clone, Hash, PartialEq, Eq)]
pub struct ModuleCacheKey {
    pub path: Arc<PathBuf>,
    pub include_tests: bool,
}

impl ModuleCacheKey {
    pub fn new(path: Arc<PathBuf>, include_tests: bool) -> Self {
        Self {
            path,
            include_tests,
        }
    }
}

#[derive(Clone, Debug)]
pub struct ModuleCommonInfo {
    pub path: Arc<PathBuf>,
    pub hash: u64,
    pub include_tests: bool,
    pub dependencies: Vec<Arc<PathBuf>>,
}

#[derive(Clone, Debug)]
pub struct ParsedModuleInfo {
    pub modified_time: Option<SystemTime>,
    pub version: Option<u64>,
}

#[derive(Clone, Debug)]
pub struct TypedModuleInfo {
    pub module: Arc<TyModule>,
    pub version: Option<u64>,
}

#[derive(Clone, Debug)]
pub struct ModuleCacheEntry {
    pub common: ModuleCommonInfo,
    pub parsed: ParsedModuleInfo,
    pub typed: Option<TypedModuleInfo>,
}

impl ModuleCacheEntry {
    pub fn new(common: ModuleCommonInfo, parsed: ParsedModuleInfo) -> Self {
        Self {
            common,
            parsed,
            typed: None,
        }
    }

    pub fn is_typed(&self) -> bool {
        self.typed.is_some()
    }

    pub fn set_typed(&mut self, typed: TypedModuleInfo) {
        self.typed = Some(typed);
    }

    pub fn update_common(&mut self, new_common: ModuleCommonInfo) {
        self.common = new_common;
    }

    pub fn update_parsed(&mut self, new_parsed: ParsedModuleInfo) {
        self.parsed = new_parsed;
    }

    pub fn update_parsed_and_common(
        &mut self,
        new_common: ModuleCommonInfo,
        new_parsed: ParsedModuleInfo,
    ) {
        self.common = new_common;
        self.parsed = new_parsed;
    }
}

#[derive(Debug, Default, Clone)]
pub struct ModuleCacheMap(HashMap<ModuleCacheKey, ModuleCacheEntry>);

impl Deref for ModuleCacheMap {
    type Target = HashMap<ModuleCacheKey, ModuleCacheEntry>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for ModuleCacheMap {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl ModuleCacheMap {
    pub fn update_entry(
        &mut self,
        key: &ModuleCacheKey,
        new_common: ModuleCommonInfo,
        new_parsed: ParsedModuleInfo,
    ) {
        if let Some(entry) = self.get_mut(key) {
            entry.update_parsed_and_common(new_common, new_parsed);
        } else {
            self.insert(key.clone(), ModuleCacheEntry::new(new_common, new_parsed));
        }
    }
}

pub type ProgramsCacheMap = HashMap<Arc<PathBuf>, ProgramsCacheEntry>;
pub type FunctionsCacheMap = HashMap<(IdentUnique, String), FunctionCacheEntry>;

#[derive(Clone, Debug)]
pub struct ProgramsCacheEntry {
    pub path: Arc<PathBuf>,
    pub programs: Programs,
    pub handler_data: (Vec<CompileError>, Vec<CompileWarning>),
}

#[derive(Clone, Debug)]
pub struct FunctionCacheEntry {
    pub fn_decl: DeclRef<DeclId<TyFunctionDecl>>,
}

#[derive(Debug, Default)]
pub struct QueryEngine {
    // We want the below types wrapped in Arcs to optimize cloning from LSP.
    programs_cache: CowCache<ProgramsCacheMap>,
    pub module_cache: CowCache<ModuleCacheMap>,
    // NOTE: Any further AstNodes that are cached need to have garbage collection applied, see clear_module()
    function_cache: CowCache<FunctionsCacheMap>,
}

impl Clone for QueryEngine {
    fn clone(&self) -> Self {
        Self {
            programs_cache: CowCache::new(self.programs_cache.read().clone()),
            module_cache: CowCache::new(self.module_cache.read().clone()),
            function_cache: CowCache::new(self.function_cache.read().clone()),
        }
    }
}

impl QueryEngine {
    pub fn update_or_insert_parsed_module_cache_entry(&self, entry: ModuleCacheEntry) {
        let path = entry.common.path.clone();
        let include_tests = entry.common.include_tests;
        let key = ModuleCacheKey::new(path, include_tests);
        let mut cache = self.module_cache.write();
        cache.update_entry(&key, entry.common, entry.parsed);
    }

    pub fn update_typed_module_cache_entry(&self, key: &ModuleCacheKey, entry: TypedModuleInfo) {
        let mut cache = self.module_cache.write();
        cache.get_mut(key).unwrap().set_typed(entry);
    }

    pub fn get_programs_cache_entry(&self, path: &Arc<PathBuf>) -> Option<ProgramsCacheEntry> {
        let cache = self.programs_cache.read();
        cache.get(path).cloned()
    }

    pub fn insert_programs_cache_entry(&self, entry: ProgramsCacheEntry) {
        let mut cache = self.programs_cache.write();
        cache.insert(entry.path.clone(), entry);
    }

    pub fn get_function(
        &self,
        engines: &Engines,
        ident: IdentUnique,
        sig: TyFunctionSig,
    ) -> Option<DeclRef<DeclId<TyFunctionDecl>>> {
        let cache = self.function_cache.read();
        cache
            .get(&(ident, sig.get_type_str(engines)))
            .map(|s| s.fn_decl.clone())
    }

    pub fn insert_function(
        &self,
        engines: &Engines,
        ident: IdentUnique,
        sig: TyFunctionSig,
        fn_decl: DeclRef<DeclId<TyFunctionDecl>>,
    ) {
        let mut cache = self.function_cache.write();
        cache.insert(
            (ident, sig.get_type_str(engines)),
            FunctionCacheEntry { fn_decl },
        );
    }

    /// Removes all data associated with the `source_id` from the function cache.
    pub fn clear_module(&mut self, source_id: &SourceId) {
        self.function_cache
            .write()
            .retain(|(ident, _), _| ident.span().source_id().map_or(true, |id| id != source_id));
    }

    /// Removes all data associated with the `program_id` from the function cache.
    pub fn clear_program(&mut self, program_id: &ProgramId) {
        self.function_cache.write().retain(|(ident, _), _| {
            ident
                .span()
                .source_id()
                .map_or(true, |id| id.program_id() != *program_id)
        });
    }

    ///  Commits all changes to their respective caches.
    pub fn commit(&self) {
        self.programs_cache.commit();
        self.module_cache.commit();
        self.function_cache.commit();
    }
}

/// Thread-safe, copy-on-write cache optimized for LSP operations.
///
/// Addresses key LSP challenges:
/// 1. Concurrent read access to shared data
/// 2. Local modifications for cancellable operations (e.g., compilation)
/// 3. Prevents incomplete results from affecting shared state
/// 4. Maintains consistency via explicit commit step
///
/// Uses `Arc<RwLock<T>>` for shared state and `RwLock<Option<T>>` for local changes.
/// Suitable for interactive sessions with frequent file changes.
#[derive(Debug, Default)]
pub struct CowCache<T: Clone> {
    inner: Arc<RwLock<T>>,
    local: RwLock<Option<T>>,
}

impl<T: Clone> CowCache<T> {
    /// Creates a new `CowCache` with the given initial value.
    ///
    /// The value is wrapped in an `Arc<RwLock<T>>` to allow shared access across threads.
    pub fn new(value: T) -> Self {
        Self {
            inner: Arc::new(RwLock::new(value)),
            local: RwLock::new(None),
        }
    }

    /// Provides read access to the cached value.
    ///
    /// If a local modification exists, it returns a reference to the local copy.
    /// Otherwise, it returns a reference to the shared state.
    ///
    /// This method is optimized for concurrent read access in LSP operations.
    pub fn read(&self) -> impl Deref<Target = T> + '_ {
        if self.local.read().is_some() {
            ReadGuard::Local(self.local.read())
        } else {
            ReadGuard::Shared(self.inner.read())
        }
    }

    /// Provides write access to a local copy of the cached value.
    ///
    /// In LSP, this is used for operations like compilation tasks that may be cancelled.
    /// It allows modifications without affecting the shared state until explicitly committed.
    pub fn write(&self) -> impl DerefMut<Target = T> + '_ {
        let mut local = self.local.write();
        if local.is_none() {
            *local = Some(self.inner.read().clone());
        }
        WriteGuard(local)
    }

    /// Commits local modifications to the shared state.
    ///
    /// Called after successful completion of a compilation task.
    /// If a task is cancelled, not calling this method effectively discards local changes.
    pub fn commit(&self) {
        if let Some(local) = self.local.write().take() {
            *self.inner.write() = local;
        }
    }
}

/// A guard type that provides read access to either the local or shared state.
enum ReadGuard<'a, T: Clone> {
    Local(RwLockReadGuard<'a, Option<T>>),
    Shared(RwLockReadGuard<'a, T>),
}

impl<'a, T: Clone> Deref for ReadGuard<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        match self {
            ReadGuard::Local(r) => r.as_ref().unwrap(),
            ReadGuard::Shared(guard) => guard.deref(),
        }
    }
}

/// A guard type that provides write access to the local state.
struct WriteGuard<'a, T: Clone>(RwLockWriteGuard<'a, Option<T>>);

impl<'a, T: Clone> Deref for WriteGuard<'a, T> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        self.0.as_ref().unwrap()
    }
}

impl<'a, T: Clone> DerefMut for WriteGuard<'a, T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        self.0.as_mut().unwrap()
    }
}