Skip to content

Commit 3139999

Browse files
Create CrateInput and CrateConfigurationInput for CrateLongId and CrateConfiguration in preperation for salsa upgrade
commit-id:4d7fca4f
1 parent e7befd0 commit 3139999

File tree

7 files changed

+230
-47
lines changed

7 files changed

+230
-47
lines changed

crates/cairo-lang-defs/src/cache/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,11 +739,11 @@ enum CrateCached {
739739
Virtual { name: SmolStr, file_id: FileIdCached, settings: String },
740740
}
741741
impl CrateCached {
742-
fn new(crate_id: CrateLongId, _ctx: &mut DefCacheSavingContext<'_>) -> Self {
742+
fn new(crate_id: CrateLongId, ctx: &mut DefCacheSavingContext<'_>) -> Self {
743743
match crate_id {
744744
CrateLongId::Real { name, discriminator } => CrateCached::Real { name, discriminator },
745745
CrateLongId::Virtual { name, file_id, settings, cache_file: _ } => {
746-
CrateCached::Virtual { name, file_id: FileIdCached::new(file_id, _ctx), settings }
746+
CrateCached::Virtual { name, file_id: FileIdCached::new(file_id, ctx), settings }
747747
}
748748
}
749749
}

crates/cairo-lang-defs/src/db.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::sync::Arc;
33

44
use cairo_lang_diagnostics::{DiagnosticNote, Maybe, PluginFileDiagnosticNotes, ToMaybe};
55
use cairo_lang_filesystem::ids::{
6-
CrateId, CrateLongId, Directory, FileId, FileKind, FileLongId, VirtualFile,
6+
CrateId, CrateInput, Directory, FileId, FileKind, FileLongId, VirtualFile,
77
};
88
use cairo_lang_parser::db::ParserGroup;
99
use cairo_lang_syntax::attribute::consts::{
@@ -107,7 +107,7 @@ pub trait DefsGroup: ParserGroup {
107107
#[salsa::input]
108108
fn macro_plugin_overrides_input(
109109
&self,
110-
) -> Arc<OrderedHashMap<CrateLongId, Arc<[MacroPluginLongId]>>>;
110+
) -> Arc<OrderedHashMap<CrateInput, Arc<[MacroPluginLongId]>>>;
111111

112112
/// Interned version of `macro_plugin_overrides_input`.
113113
fn macro_plugin_overrides(&self) -> Arc<OrderedHashMap<CrateId, Arc<[MacroPluginId]>>>;
@@ -132,7 +132,7 @@ pub trait DefsGroup: ParserGroup {
132132
#[salsa::input]
133133
fn inline_macro_plugin_overrides_input(
134134
&self,
135-
) -> Arc<OrderedHashMap<CrateLongId, Arc<OrderedHashMap<String, InlineMacroExprPluginLongId>>>>;
135+
) -> Arc<OrderedHashMap<CrateInput, Arc<OrderedHashMap<String, InlineMacroExprPluginLongId>>>>;
136136

137137
/// Interned version of `inline_macro_plugin_overrides_input`.
138138
fn inline_macro_plugin_overrides(
@@ -366,7 +366,7 @@ pub fn macro_plugin_overrides(
366366
inp.iter()
367367
.map(|(crate_id, plugins)| {
368368
(
369-
crate_id.clone().intern(db),
369+
crate_id.clone().into_crate_long_id(db).intern(db),
370370
Arc::from(plugins.iter().map(|plugin| plugin.clone().intern(db)).collect_vec()),
371371
)
372372
})
@@ -382,7 +382,7 @@ pub fn inline_macro_plugin_overrides(
382382
inp.iter()
383383
.map(|(crate_id, plugins)| {
384384
(
385-
crate_id.clone().intern(db),
385+
crate_id.clone().into_crate_long_id(db).intern(db),
386386
Arc::new(
387387
plugins
388388
.iter()
@@ -1470,11 +1470,11 @@ pub trait DefsGroupEx: DefsGroup {
14701470
crate_id: CrateId,
14711471
plugins: Arc<[MacroPluginId]>,
14721472
) {
1473-
let crate_long_id = crate_id.lookup_intern(self);
1473+
let crate_input = self.crate_input(crate_id);
14741474
let mut overrides = self.macro_plugin_overrides_input().as_ref().clone();
14751475
let plugins =
14761476
plugins.iter().map(|plugin| self.lookup_intern_macro_plugin(*plugin)).collect();
1477-
overrides.insert(crate_long_id, plugins);
1477+
overrides.insert(crate_input, plugins);
14781478
self.set_macro_plugin_overrides_input(Arc::new(overrides));
14791479
}
14801480

@@ -1486,7 +1486,7 @@ pub trait DefsGroupEx: DefsGroup {
14861486
crate_id: CrateId,
14871487
plugins: Arc<OrderedHashMap<String, InlineMacroExprPluginId>>,
14881488
) {
1489-
let crate_id = crate_id.lookup_intern(self);
1489+
let crate_input = self.crate_input(crate_id);
14901490
let mut overrides = self.inline_macro_plugin_overrides_input().as_ref().clone();
14911491
let plugins = Arc::new(
14921492
plugins
@@ -1496,7 +1496,7 @@ pub trait DefsGroupEx: DefsGroup {
14961496
})
14971497
.collect(),
14981498
);
1499-
overrides.insert(crate_id, plugins);
1499+
overrides.insert(crate_input, plugins);
15001500
self.set_inline_macro_plugin_overrides_input(Arc::new(overrides));
15011501
}
15021502
}

crates/cairo-lang-filesystem/src/db.rs

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use smol_str::{SmolStr, ToSmolStr};
1313
use crate::cfg::CfgSet;
1414
use crate::flag::Flag;
1515
use crate::ids::{
16-
BlobId, BlobLongId, CodeMapping, CodeOrigin, CrateId, CrateLongId, Directory, FileId,
17-
FileInput, FileLongId, FlagId, FlagLongId, VirtualFile,
16+
BlobId, BlobLongId, CodeMapping, CodeOrigin, CrateId, CrateInput, CrateLongId, Directory,
17+
DirectoryInput, FileId, FileInput, FileLongId, FlagId, FlagLongId, VirtualFile,
1818
};
1919
use crate::span::{FileSummary, TextOffset, TextSpan, TextWidth};
2020

@@ -44,8 +44,28 @@ impl From<CrateIdentifier> for SmolStr {
4444
}
4545
}
4646

47-
/// A configuration per crate.
47+
/// Same as `CrateConfiguration` but without interning the root directory.
48+
/// This is used to avoid the need to intern the file id inside salsa database inputs.
4849
#[derive(Clone, Debug, PartialEq, Eq)]
50+
pub struct CrateConfigurationInput {
51+
pub root: DirectoryInput,
52+
pub settings: CrateSettings,
53+
pub cache_file: Option<BlobLongId>,
54+
}
55+
56+
impl CrateConfigurationInput {
57+
/// Converts the input into an [`CrateConfiguration`].
58+
pub fn into_crate_configuration(self, db: &dyn FilesGroup) -> CrateConfiguration {
59+
CrateConfiguration {
60+
root: self.root.into_directory(db),
61+
settings: self.settings,
62+
cache_file: self.cache_file.map(|blob_long_id| blob_long_id.intern(db)),
63+
}
64+
}
65+
}
66+
67+
/// A configuration per crate.
68+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
4969
pub struct CrateConfiguration {
5070
/// The root directory of the crate.
5171
pub root: Directory,
@@ -57,10 +77,19 @@ impl CrateConfiguration {
5777
pub fn default_for_root(root: Directory) -> Self {
5878
Self { root, settings: CrateSettings::default(), cache_file: None }
5979
}
80+
81+
/// Converts the configuration into an [`CrateConfigurationInput`].
82+
pub fn into_crate_configuration_input(self, db: &dyn FilesGroup) -> CrateConfigurationInput {
83+
CrateConfigurationInput {
84+
root: self.root.into_directory_input(db),
85+
settings: self.settings,
86+
cache_file: self.cache_file.map(|blob_id| blob_id.lookup_intern(db)),
87+
}
88+
}
6089
}
6190

6291
/// Same as `CrateConfiguration` but without the root directory.
63-
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, Serialize, Deserialize)]
92+
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Hash)]
6493
pub struct CrateSettings {
6594
/// The name reflecting how the crate is referred to in the Cairo code e.g. `use crate_name::`.
6695
/// If set to [`None`] then [`CrateIdentifier`] key will be used as a name.
@@ -137,7 +166,7 @@ impl Edition {
137166
}
138167

139168
/// The settings for a dependency.
140-
#[derive(Clone, Debug, Default, Hash, PartialEq, Eq, Serialize, Deserialize)]
169+
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, Hash)]
141170
pub struct DependencySettings {
142171
/// A unique string allowing identifying different copies of the same dependency
143172
/// in the compilation unit.
@@ -192,7 +221,7 @@ pub trait FilesGroup: ExternalFiles {
192221

193222
/// Main input of the project. Lists all the crates configurations.
194223
#[salsa::input]
195-
fn crate_configs_input(&self) -> Arc<OrderedHashMap<CrateLongId, CrateConfiguration>>;
224+
fn crate_configs_input(&self) -> Arc<OrderedHashMap<CrateInput, CrateConfigurationInput>>;
196225

197226
/// Interned version of `crate_configs_input`.
198227
fn crate_configs(&self) -> Arc<OrderedHashMap<CrateId, CrateConfiguration>>;
@@ -238,6 +267,12 @@ pub trait FilesGroup: ExternalFiles {
238267

239268
/// Create an input file from an interned file id.
240269
fn file_input(&self, file_id: FileId) -> FileInput;
270+
271+
/// Create an input crate from an interned crate id.
272+
fn crate_input(&self, crt: CrateId) -> CrateInput;
273+
274+
/// Create an input crate configuration from a [`CrateConfiguration`].
275+
fn crate_configuration_input(&self, config: CrateConfiguration) -> CrateConfigurationInput;
241276
}
242277

243278
pub fn init_files_group(db: &mut (dyn FilesGroup + 'static)) {
@@ -263,7 +298,12 @@ pub fn crate_configs(db: &dyn FilesGroup) -> Arc<OrderedHashMap<CrateId, CrateCo
263298
let inp = db.crate_configs_input();
264299
Arc::new(
265300
inp.iter()
266-
.map(|(crate_id, config)| (crate_id.clone().intern(db), config.clone()))
301+
.map(|(crate_input, config)| {
302+
(
303+
crate_input.clone().into_crate_long_id(db).intern(db),
304+
config.clone().into_crate_configuration(db),
305+
)
306+
})
267307
.collect(),
268308
)
269309
}
@@ -277,6 +317,17 @@ fn file_input(db: &dyn FilesGroup, file_id: FileId) -> FileInput {
277317
file_id.lookup_intern(db).into_file_input(db)
278318
}
279319

320+
fn crate_input(db: &dyn FilesGroup, crt: CrateId) -> CrateInput {
321+
crt.lookup_intern(db).into_crate_input(db)
322+
}
323+
324+
fn crate_configuration_input(
325+
db: &dyn FilesGroup,
326+
config: CrateConfiguration,
327+
) -> CrateConfigurationInput {
328+
config.clone().into_crate_configuration_input(db)
329+
}
330+
280331
pub fn init_dev_corelib(db: &mut (dyn FilesGroup + 'static), core_lib_dir: PathBuf) {
281332
db.set_crate_config(
282333
CrateId::core(db),
@@ -313,10 +364,10 @@ pub trait FilesGroupEx: FilesGroup {
313364
}
314365
/// Sets the root directory of the crate. None value removes the crate.
315366
fn set_crate_config(&mut self, crt: CrateId, root: Option<CrateConfiguration>) {
316-
let crt = self.lookup_intern_crate(crt);
367+
let crt = self.crate_input(crt);
317368
let mut crate_configs = self.crate_configs_input().as_ref().clone();
318369
match root {
319-
Some(root) => crate_configs.insert(crt, root),
370+
Some(root) => crate_configs.insert(crt, self.crate_configuration_input(root)),
320371
None => crate_configs.swap_remove(&crt),
321372
};
322373
self.set_crate_configs_input(Arc::new(crate_configs));
@@ -398,19 +449,7 @@ fn get_flag(db: &dyn FilesGroup, id: FlagId) -> Option<Arc<Flag>> {
398449
}
399450

400451
fn blob_content(db: &dyn FilesGroup, blob: BlobId) -> Option<Arc<[u8]>> {
401-
match blob.lookup_intern(db) {
402-
BlobLongId::OnDisk(path) => {
403-
// This does not result in performance cost due to OS caching and the fact that salsa
404-
// will re-execute only this single query if the file content did not change.
405-
db.salsa_runtime().report_synthetic_read(Durability::LOW);
406-
407-
match fs::read(path) {
408-
Ok(content) => Some(content.into()),
409-
Err(_) => None,
410-
}
411-
}
412-
BlobLongId::Virtual(content) => Some(content),
413-
}
452+
blob.lookup_intern(db).content(db)
414453
}
415454

416455
/// Returns the location of the originating user code.

crates/cairo-lang-filesystem/src/ids.rs

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::sync::Arc;
44

55
use cairo_lang_utils::{Intern, LookupIntern, define_short_id};
66
use path_clean::PathClean;
7+
use salsa::Durability;
78
use serde::{Deserialize, Serialize};
89
use smol_str::SmolStr;
910

@@ -12,6 +13,38 @@ use crate::span::{TextOffset, TextSpan};
1213

1314
pub const CAIRO_FILE_EXTENSION: &str = "cairo";
1415

16+
/// Same as `CrateLongId`, but without internal interning.
17+
/// This is used as salsa database inputs.
18+
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
19+
pub enum CrateInput {
20+
Real {
21+
name: SmolStr,
22+
discriminator: Option<SmolStr>,
23+
},
24+
Virtual {
25+
name: SmolStr,
26+
file_long_id: FileInput,
27+
settings: String,
28+
cache_file: Option<BlobLongId>,
29+
},
30+
}
31+
32+
impl CrateInput {
33+
pub fn into_crate_long_id(self, db: &dyn FilesGroup) -> CrateLongId {
34+
match self {
35+
CrateInput::Real { name, discriminator } => CrateLongId::Real { name, discriminator },
36+
CrateInput::Virtual { name, file_long_id, settings, cache_file } => {
37+
CrateLongId::Virtual {
38+
name,
39+
file_id: file_long_id.into_file_long_id(db).intern(db),
40+
settings,
41+
cache_file: cache_file.map(|blob_long_id| blob_long_id.intern(db)),
42+
}
43+
}
44+
}
45+
}
46+
}
47+
1548
/// A crate is a standalone file tree representing a single compilation unit.
1649
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
1750
pub enum CrateLongId {
@@ -26,6 +59,18 @@ impl CrateLongId {
2659
CrateLongId::Real { name, .. } | CrateLongId::Virtual { name, .. } => name.clone(),
2760
}
2861
}
62+
63+
pub fn into_crate_input(self, db: &dyn FilesGroup) -> CrateInput {
64+
match self {
65+
CrateLongId::Real { name, discriminator } => CrateInput::Real { name, discriminator },
66+
CrateLongId::Virtual { name, file_id, settings, cache_file } => CrateInput::Virtual {
67+
name,
68+
file_long_id: file_id.lookup_intern(db).into_file_input(db),
69+
settings,
70+
cache_file: cache_file.map(|blob_id| blob_id.lookup_intern(db)),
71+
},
72+
}
73+
}
2974
}
3075
define_short_id!(CrateId, CrateLongId, FilesGroup, lookup_intern_crate, intern_crate);
3176
impl CrateId {
@@ -272,6 +317,33 @@ impl FileId {
272317
}
273318
}
274319

320+
/// Same as `Directory`, but without the interning inside virtual directories.
321+
/// This is used to avoid the need to intern the file id inside salsa database inputs.
322+
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
323+
pub enum DirectoryInput {
324+
Real(PathBuf),
325+
Virtual { files: BTreeMap<SmolStr, FileInput>, dirs: BTreeMap<SmolStr, Box<DirectoryInput>> },
326+
}
327+
328+
impl DirectoryInput {
329+
/// Converts the input into a [`Directory`].
330+
pub fn into_directory(self, db: &dyn FilesGroup) -> Directory {
331+
match self {
332+
DirectoryInput::Real(path) => Directory::Real(path),
333+
DirectoryInput::Virtual { files, dirs } => Directory::Virtual {
334+
files: files
335+
.into_iter()
336+
.map(|(name, file_input)| (name, file_input.into_file_long_id(db).intern(db)))
337+
.collect(),
338+
dirs: dirs
339+
.into_iter()
340+
.map(|(name, dir_input)| (name, Box::new(dir_input.into_directory(db))))
341+
.collect(),
342+
},
343+
}
344+
}
345+
}
346+
275347
#[derive(Clone, Debug, Hash, PartialEq, Eq)]
276348
pub enum Directory {
277349
/// A directory on the file system.
@@ -307,6 +379,23 @@ impl Directory {
307379
}
308380
}
309381
}
382+
383+
/// Converts the directory into an [`DirectoryInput`].
384+
pub fn into_directory_input(self, db: &dyn FilesGroup) -> DirectoryInput {
385+
match self {
386+
Directory::Real(path) => DirectoryInput::Real(path),
387+
Directory::Virtual { files, dirs } => DirectoryInput::Virtual {
388+
files: files
389+
.into_iter()
390+
.map(|(name, file_id)| (name, file_id.lookup_intern(db).into_file_input(db)))
391+
.collect(),
392+
dirs: dirs
393+
.into_iter()
394+
.map(|(name, dir)| (name, Box::new(dir.into_directory_input(db))))
395+
.collect(),
396+
},
397+
}
398+
}
310399
}
311400

312401
/// A FileId for data that is not necessarily a valid UTF-8 string.
@@ -316,6 +405,25 @@ pub enum BlobLongId {
316405
Virtual(Arc<[u8]>),
317406
}
318407

408+
impl BlobLongId {
409+
pub fn content(self, db: &dyn FilesGroup) -> Option<Arc<[u8]>> {
410+
match self {
411+
BlobLongId::OnDisk(path) => {
412+
// This does not result in performance cost due to OS caching and the fact that
413+
// salsa will re-execute only this single query if the file content
414+
// did not change.
415+
db.salsa_runtime().report_synthetic_read(Durability::LOW);
416+
417+
match std::fs::read(path) {
418+
Ok(content) => Some(Arc::from(content)),
419+
Err(_) => None,
420+
}
421+
}
422+
BlobLongId::Virtual(content) => Some(content),
423+
}
424+
}
425+
}
426+
319427
define_short_id!(BlobId, BlobLongId, FilesGroup, lookup_intern_blob, intern_blob);
320428

321429
impl BlobId {

0 commit comments

Comments
 (0)