Skip to main content

olean_export/env/
reload.rs

1use super::{
2    decode::{decode_all, Module},
3    discover_all,
4    table::{SharedExprs, Table, Tables},
5    term::NameId,
6    Env,
7};
8use crate::{
9    error::{Error, Result},
10    olean::Header,
11};
12use hashbrown::HashTable;
13use rustc_hash::{FxHashMap, FxHashSet};
14use std::{
15    fs, mem,
16    path::{Path, PathBuf},
17    time::SystemTime,
18};
19
20/// The olean parts of a module that exist, with their sizes and modification times.
21pub(super) type Stamp = Vec<(u64, Option<SystemTime>)>;
22
23const PARTS: [&str; 3] = ["olean", "olean.server", "olean.private"];
24
25pub(super) fn stamp(path: &Path) -> Stamp {
26    PARTS
27        .iter()
28        .filter_map(|e| fs::metadata(path.with_extension(e)).ok())
29        .map(|m| (m.len(), m.modified().ok()))
30        .collect()
31}
32
33/// A decoded module, as [`Env::reload`] tracks it.
34#[derive(Debug)]
35pub(super) struct Loaded {
36    pub(super) module: String,
37    pub(super) path: PathBuf,
38    pub(super) stamp: Stamp,
39    pub(super) consts: Vec<NameId>,
40    pub(super) recursors: FxHashMap<NameId, Vec<NameId>>,
41    pub(super) skipped: usize,
42    pub(super) header: Header,
43    pub(super) pending: Vec<(NameId, u64)>,
44}
45
46/// What an env from [`Env::open`] keeps to decode more modules into its tables.
47#[derive(Debug)]
48pub(super) struct Live {
49    pub(super) search: Vec<PathBuf>,
50    pub(super) roots: Vec<String>,
51    pub(super) jobs: usize,
52    pub(super) index: Vec<HashTable<u32>>,
53    pub(super) ranks: Vec<Vec<u32>>,
54    pub(super) loaded: Vec<Loaded>,
55}
56
57/// What [`Env::reload`] changed.
58#[derive(Debug, Default)]
59pub struct Reload {
60    /// Modules decoded again, in import order.
61    pub modules: Vec<String>,
62    /// Their constants, in load order.
63    pub consts: Vec<NameId>,
64    /// Constants no longer in the environment.
65    pub removed: Vec<NameId>,
66}
67
68impl Reload {
69    #[must_use]
70    pub const fn is_empty(&self) -> bool {
71        self.modules.is_empty() && self.removed.is_empty()
72    }
73}
74
75impl Env {
76    /// Like [`Env::load_with`], but keeps the expression index so that [`Env::reload`] can
77    /// decode changed modules into the same tables. That costs about 10 bytes per expression.
78    pub fn open(search: &[PathBuf], roots: &[&str], jobs: usize) -> Result<Self> {
79        Self::build(search, roots, jobs, &|_, _| {}, true, false)
80    }
81
82    /// Decode again the modules whose oleans changed since the last load, add newly imported
83    /// ones and drop those no longer imported. Every id stays valid and the tables only grow,
84    /// so an [`crate::Exporter`] can resume from a [`crate::Mark`] and write just the
85    /// changes. Binder names, which no kernel reads, may differ from a fresh load.
86    pub fn reload(&mut self) -> Result<Reload> {
87        let mut live = self
88            .live
89            .take()
90            .ok_or(Error::Misuse("reload needs an environment from Env::open"))?;
91        let r = self.reload_with(&mut live);
92        self.live = Some(live);
93        r
94    }
95
96    fn reload_with(&mut self, live: &mut Live) -> Result<Reload> {
97        let roots: Vec<&str> = live.roots.iter().map(String::as_str).collect();
98        let found = discover_all(&live.search, &roots)?;
99        let stamps: Vec<Stamp> = found.iter().map(|(_, p)| stamp(p)).collect();
100        let fresh: Vec<(usize, &Path)> = {
101            let prev: FxHashMap<&str, &Stamp> = live
102                .loaded
103                .iter()
104                .map(|l| (l.module.as_str(), &l.stamp))
105                .collect();
106            if found.len() == prev.len()
107                && found
108                    .iter()
109                    .zip(&stamps)
110                    .all(|((m, _), s)| prev.get(m.as_str()) == Some(&s))
111            {
112                return Ok(Reload::default());
113            }
114            found
115                .iter()
116                .zip(&stamps)
117                .enumerate()
118                .filter(|(_, ((m, _), s))| prev.get(m.as_str()) != Some(s))
119                .map(|(i, ((_, p), _))| (i, p.as_path()))
120                .collect()
121        };
122        let tables = self.thaw(live);
123        let decoded = decode_all(&tables, &fresh, live.jobs, &|_, _| {}, false);
124        self.freeze(tables, live);
125        let mut decoded: FxHashMap<usize, Module> =
126            fresh.iter().map(|&(i, _)| i).zip(decoded?).collect();
127        let mut prev: FxHashMap<String, Loaded> = mem::take(&mut live.loaded)
128            .into_iter()
129            .map(|l| (l.module.clone(), l))
130            .collect();
131        let imported: FxHashSet<&str> = found.iter().map(|(m, _)| m.as_str()).collect();
132        let mut stale = Vec::new();
133        prev.retain(|m, l| {
134            let keep =
135                imported.contains(m.as_str()) && !fresh.iter().any(|&(i, _)| found[i].0 == *m);
136            if !keep {
137                stale.append(&mut l.consts);
138            }
139            keep
140        });
141        for c in &stale {
142            self.consts.remove(c);
143        }
144        let mut reload = Reload::default();
145        live.loaded = found
146            .into_iter()
147            .zip(stamps)
148            .enumerate()
149            .map(|(i, ((module, path), stamp))| match decoded.remove(&i) {
150                Some(m) => {
151                    reload.modules.push(module.clone());
152                    reload.consts.extend(m.consts.iter().map(|c| c.name));
153                    self.add(module, path, stamp, m)
154                }
155                None => prev.remove(&module).expect("unchanged module was loaded"),
156            })
157            .collect();
158        self.index(&live.loaded);
159        reload.removed = stale
160            .into_iter()
161            .filter(|c| !self.consts.contains_key(c))
162            .collect();
163        Ok(reload)
164    }
165
166    /// Hand the tables back to shared, growable form to decode more into them.
167    pub(super) fn thaw(&mut self, live: &mut Live) -> Tables {
168        Tables {
169            names: mem::replace(&mut self.names, Table(Vec::new())).thaw(),
170            levels: mem::replace(&mut self.levels, Table(Vec::new())).thaw(),
171            exprs: SharedExprs::thaw(
172                mem::take(&mut self.exprs),
173                mem::take(&mut live.index),
174                mem::take(&mut live.ranks),
175            ),
176        }
177    }
178
179    pub(super) fn freeze(&mut self, tables: Tables, live: &mut Live) {
180        self.names = tables.names.freeze();
181        self.levels = tables.levels.freeze();
182        (self.exprs, live.index, live.ranks) = tables.exprs.freeze();
183    }
184}