Skip to content

Commit

Permalink
Removed package interface from cache and made cached modules be recom…
Browse files Browse the repository at this point in the history
…piled on export. Removed redundant traits
  • Loading branch information
mine-tech-oficial committed Oct 8, 2024
1 parent 09d3e9e commit 2013248
Show file tree
Hide file tree
Showing 13 changed files with 49 additions and 69 deletions.
2 changes: 1 addition & 1 deletion compiler-cli/src/export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub fn package_interface(path: Utf8PathBuf) -> Result<()> {
// Build the project
let mut built = crate::build::main(
Options {
mode: Mode::Prod,
mode: Mode::PackageInterface,
target: None,
codegen: Codegen::All,
compile: Compile::All,
Expand Down
6 changes: 3 additions & 3 deletions compiler-core/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ pub enum Mode {
Dev,
Prod,
Lsp,
PackageInterface,
}

impl Mode {
Expand All @@ -193,7 +194,7 @@ impl Mode {
pub fn includes_tests(&self) -> bool {
match self {
Self::Dev | Self::Lsp => true,
Self::Prod => false,
Self::Prod | Self::PackageInterface => false,
}
}
}
Expand All @@ -209,7 +210,6 @@ fn mode_includes_tests() {
pub struct Package {
pub config: PackageConfig,
pub modules: Vec<Module>,
pub cached_metadata: Vec<package_compiler::CacheMetadata>,
}

impl Package {
Expand All @@ -227,7 +227,7 @@ impl Package {
}
}

#[derive(Debug, Clone)]
#[derive(Debug)]
pub struct Module {
pub name: EcoString,
pub code: EcoString,
Expand Down
2 changes: 1 addition & 1 deletion compiler-core/src/build/module_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ where
}
}

Ok(Input::Cached(self.cached(name, meta.clone()), meta))
Ok(Input::Cached(self.cached(name, meta)))
}

/// Read the timestamp file from the artefact directory for the given
Expand Down
2 changes: 0 additions & 2 deletions compiler-core/src/build/module_loader/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,11 @@ fn write_cache(
) {
let line_numbers = LineNumbers::new(source);
let cache_metadata = CacheMetadata {
name: "".into(),
mtime: SystemTime::UNIX_EPOCH + Duration::from_secs(seconds),
codegen_performed,
dependencies: vec![],
fingerprint: SourceFingerprint::new(source),
line_numbers,
interface: package_interface::ModuleInterface::default(),
};
let path = Utf8Path::new(path);
fs.write_bytes(&path, &cache_metadata.to_binary()).unwrap();
Expand Down
28 changes: 10 additions & 18 deletions compiler-core/src/build/package_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ where
stale_modules: &mut StaleTracker,
incomplete_modules: &mut HashSet<EcoString>,
telemetry: &dyn Telemetry,
) -> Outcome<(Vec<Module>, Vec<CacheMetadata>), Error> {
) -> Outcome<Vec<Module>, Error> {
let span = tracing::info_span!("compile", package = %self.config.name.as_str());
let _enter = span.enter();

Expand Down Expand Up @@ -186,9 +186,7 @@ where

let mut modules = match outcome {
Outcome::Ok(modules) => modules,
Outcome::PartialFailure(modules, err) => {
return Outcome::PartialFailure((modules, loaded.cached_metadata), err)
}
Outcome::PartialFailure(modules, err) => return Outcome::PartialFailure(modules, err),
Outcome::TotalFailure(err) => return Outcome::TotalFailure(err),
};

Expand All @@ -202,7 +200,7 @@ where
return error.into();
}

Outcome::Ok((modules, loaded.cached_metadata))
Outcome::Ok(modules)
}

fn compile_erlang_to_beam(&mut self, modules: &HashSet<Utf8PathBuf>) -> Result<(), Error> {
Expand Down Expand Up @@ -309,13 +307,11 @@ where
let name = format!("{}.cache_meta", &module_name);
let path = artefact_dir.join(name);
let info = CacheMetadata {
name: module.name.clone(),
mtime: module.mtime,
codegen_performed: self.perform_codegen,
dependencies: module.dependencies.clone(),
fingerprint: SourceFingerprint::new(&module.code),
line_numbers: module.ast.type_info.line_numbers.clone(),
interface: package_interface::ModuleInterface::from_module(module),
};
self.io.write_bytes(&path, &info.to_binary())?;

Expand Down Expand Up @@ -601,28 +597,28 @@ pub(crate) fn module_name(package_path: &Utf8Path, full_module_path: &Utf8Path)
#[derive(Debug)]
pub(crate) enum Input {
New(UncompiledModule),
Cached(CachedModule, CacheMetadata),
Cached(CachedModule),
}

impl Input {
pub fn name(&self) -> &EcoString {
match self {
Input::New(m) => &m.name,
Input::Cached(m, _) => &m.name,
Input::Cached(m) => &m.name,
}
}

pub fn source_path(&self) -> &Utf8Path {
match self {
Input::New(m) => &m.path,
Input::Cached(m, _) => &m.source_path,
Input::Cached(m) => &m.source_path,
}
}

pub fn dependencies(&self) -> Vec<EcoString> {
match self {
Input::New(m) => m.dependencies.iter().map(|(n, _)| n.clone()).collect(),
Input::Cached(m, _) => m.dependencies.iter().map(|(n, _)| n.clone()).collect(),
Input::Cached(m) => m.dependencies.iter().map(|(n, _)| n.clone()).collect(),
}
}

Expand Down Expand Up @@ -652,15 +648,13 @@ pub(crate) struct CachedModule {
pub line_numbers: LineNumbers,
}

#[derive(Debug, serde::Serialize, serde::Deserialize, Clone)]
#[derive(Debug, serde::Serialize, serde::Deserialize)]
pub struct CacheMetadata {
pub name: EcoString,
pub mtime: SystemTime,
pub codegen_performed: bool,
pub dependencies: Vec<(EcoString, SrcSpan)>,
pub fingerprint: SourceFingerprint,
pub line_numbers: LineNumbers,
pub interface: package_interface::ModuleInterface,
}

impl CacheMetadata {
Expand All @@ -673,24 +667,22 @@ impl CacheMetadata {
}
}

#[derive(Debug, Default)]
#[derive(Debug, Default, PartialEq, Eq)]
pub(crate) struct Loaded {
pub to_compile: Vec<UncompiledModule>,
pub cached: Vec<type_::ModuleInterface>,
pub cached_metadata: Vec<CacheMetadata>,
}

impl Loaded {
fn empty() -> Self {
Self {
to_compile: vec![],
cached: vec![],
cached_metadata: vec![],
}
}
}

#[derive(Debug, PartialEq, Eq, Clone)]
#[derive(Debug, PartialEq, Eq)]
pub(crate) struct UncompiledModule {
pub path: Utf8PathBuf,
pub name: EcoString,
Expand Down
22 changes: 14 additions & 8 deletions compiler-core/src/build/package_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ where
// A cached module with dependencies that are stale must be
// recompiled as the changes in the dependencies may have affect
// the output, making the cache invalid.
Input::Cached(info, _) if self.stale_modules.includes_any(&info.dependencies) => {
Input::Cached(info) if self.stale_modules.includes_any(&info.dependencies) => {
tracing::debug!(module = %info.name, "module_to_be_compiled");
self.stale_modules.add(info.name.clone());
let module = self.load_and_parse(info)?;
Expand All @@ -157,12 +157,18 @@ where

// A cached module with no stale dependencies can be used as-is
// and does not need to be recompiled.
Input::Cached(info, meta) => {
tracing::debug!(module = %info.name, "module_to_load_from_cache");
let module = self.load_cached_module(info)?;
loaded.cached.push(module);
loaded.cached_metadata.push(meta);
}
Input::Cached(info) => match self.mode {
Mode::PackageInterface => {
tracing::debug!(module = %info.name, "module_to_be_compiled");
let module = self.load_and_parse(info)?;
loaded.to_compile.push(module);
}
_ => {
tracing::debug!(module = %info.name, "module_to_load_from_cache");
let module = self.load_cached_module(info)?;
loaded.cached.push(module);
}
},
}
}

Expand Down Expand Up @@ -325,7 +331,7 @@ where
src: module.code.clone(),
}
}
Input::Cached(cached_module, _) => {
Input::Cached(cached_module) => {
let (_, location) = cached_module
.dependencies
.iter()
Expand Down
2 changes: 0 additions & 2 deletions compiler-core/src/build/package_loader/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,11 @@ fn write_cache(
let line_numbers = line_numbers::LineNumbers::new(src);
let mtime = SystemTime::UNIX_EPOCH + Duration::from_secs(seconds);
let cache_metadata = CacheMetadata {
name: name.into(),
mtime,
codegen_performed: true,
dependencies: deps,
fingerprint: SourceFingerprint::new(src),
line_numbers: line_numbers.clone(),
interface: package_interface::ModuleInterface::default(),
};
let path = Utf8Path::new("/artefact").join(format!("{name}.cache_meta"));
fs.write_bytes(&path, &cache_metadata.to_binary()).unwrap();
Expand Down
14 changes: 4 additions & 10 deletions compiler-core/src/build/project_compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,7 @@ where
pub fn compile_root_package(&mut self) -> Outcome<Package, Error> {
let config = self.config.clone();
self.compile_gleam_package(&config, true, self.paths.root().to_path_buf())
.map(|(modules, cached_metadata)| Package {
config,
modules,
cached_metadata,
})
.map(|modules| Package { config, modules })
}

/// Checks that version file found in the build directory matches the
Expand Down Expand Up @@ -292,9 +288,7 @@ where
// longer need to have the package borrowed from self.packages.
let package = self.packages.get(name).expect("Missing package").clone();
let result = match usable_build_tools(&package)?.as_slice() {
&[BuildTool::Gleam] => self
.compile_gleam_dep_package(&package)
.map(|(modules, _)| modules),
&[BuildTool::Gleam] => self.compile_gleam_dep_package(&package),
&[BuildTool::Rebar3] => self.compile_rebar3_dep_package(&package).map(|_| vec![]),
&[BuildTool::Mix] => self.compile_mix_dep_package(&package).map(|_| vec![]),
&[BuildTool::Mix, BuildTool::Rebar3] => self
Expand Down Expand Up @@ -495,7 +489,7 @@ where
fn compile_gleam_dep_package(
&mut self,
package: &ManifestPackage,
) -> Result<(Vec<Module>, Vec<package_compiler::CacheMetadata>), Error> {
) -> Result<Vec<Module>, Error> {
// TODO: Test
let package_root = match &package.source {
// If the path is relative it is relative to the root of the
Expand Down Expand Up @@ -526,7 +520,7 @@ where
config: &PackageConfig,
is_root: bool,
root_path: Utf8PathBuf,
) -> Outcome<(Vec<Module>, Vec<package_compiler::CacheMetadata>), Error> {
) -> Outcome<(Vec<Module>), Error> {
let out_path =
self.paths
.build_directory_for_package(self.mode(), self.target(), &config.name);
Expand Down
2 changes: 1 addition & 1 deletion compiler-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ impl PackageConfig {
pub fn dependencies_for(&self, mode: Mode) -> Result<Dependencies> {
match mode {
Mode::Dev | Mode::Lsp => self.all_drect_dependencies(),
Mode::Prod => Ok(self.dependencies.clone()),
Mode::Prod | Mode::PackageInterface => Ok(self.dependencies.clone()),
}
}

Expand Down
4 changes: 2 additions & 2 deletions compiler-core/src/docs/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ fn compile_with_markdown_pages(
)
.unwrap();

for module in &mut modules.0 {
for module in &mut modules {
module.attach_doc_and_module_comments();
}

Expand All @@ -79,7 +79,7 @@ fn compile_with_markdown_pages(
super::generate_html(
&paths,
&config,
&modules.0,
&modules,
&docs_pages,
pages_fs,
SystemTime::UNIX_EPOCH,
Expand Down
Loading

0 comments on commit 2013248

Please sign in to comment.