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