-
Notifications
You must be signed in to change notification settings - Fork 33
Optimize write artifacts to disk in watch mode #784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rbalicki2
merged 32 commits into
isographlabs:main
from
lucasmachadorj:lm/create-artifacts-hash
Nov 27, 2025
Merged
Changes from 16 commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
b580de7
Artifact lookup cache
8ef67c4
Generate and write to disk artifacts lookup hash
89cafc5
Rename artifact look cache to file system state
265c0a1
Remove the persisted file system state
92f455c
Write only changed files to disk in watch mode
bc1a23e
Remove serialization trait
6d4efc2
Remove unused imports
18406a5
Rename commited_artifacts to artifacts_to_write
9b65459
Merge branch 'main' into lm/create-artifacts-hash
0d10bff
fix from merge conflict
58eea9a
Add tracing to get_artifacts_to_write
3e1a5b6
Merge branch 'main' into lm/create-artifacts-hash
58ab26b
fix merge conflict
7bfef33
Replace ChangedFactory with a vector a FileSystemOperation
cb7151b
Merge branch 'main' into lm/create-artifacts-hash
cf8a101
tracing->skip_all
a8ba635
FileSystemState unit tests
04506a2
Merge branch 'main' into lm/create-artifacts-hash
f9053f5
refactor mutable state to pure functions
9322fa4
fix loop position
29fb325
reduce redundant CreateDirectory operations
4499ae3
Use FileContent index insteaf of cloning it
b7e5b3f
remove references to &Copy
7388fce
turn diff a pure function
5451d26
rewrite tests
e578ec0
fmt
e9dc514
rename unused arg
bf2297d
Remove unecessary delete operations
ab17a25
nits: remove type annotations and unnecessary variable declarations
979e70d
improve variable names in diff
d9ed1a6
a few more variable name improvements
75a8e99
some nit improvs
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,207 @@ | ||
| use std::{ | ||
| collections::{HashMap, HashSet}, | ||
| path::PathBuf, | ||
| }; | ||
|
|
||
| use crate::operation_text::hash; | ||
| use common_lang_types::{ | ||
| ArtifactFileName, ArtifactHash, ArtifactPathAndContent, FileContent, FileSystemOperation, | ||
| SelectableName, ServerObjectEntityName, | ||
| }; | ||
| use isograph_config::PersistedDocumentsHashAlgorithm; | ||
|
|
||
| #[derive(Debug, Clone, Default)] | ||
| pub struct FileSystemState { | ||
| root_files: HashMap<ArtifactFileName, (FileContent, ArtifactHash)>, | ||
| nested_files: HashMap< | ||
lucasmachadorj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ServerObjectEntityName, | ||
| HashMap<SelectableName, HashMap<ArtifactFileName, (FileContent, ArtifactHash)>>, | ||
| >, | ||
| } | ||
|
|
||
| impl FileSystemState { | ||
| pub fn insert(&mut self, path_and_content: &ArtifactPathAndContent) { | ||
| let value = ( | ||
| FileContent::from(path_and_content.file_content.clone()), | ||
| ArtifactHash::from(hash( | ||
| &path_and_content.file_content, | ||
| PersistedDocumentsHashAlgorithm::Md5, | ||
| )), | ||
| ); | ||
|
|
||
| match &path_and_content.artifact_path.type_and_field { | ||
| Some(type_and_field) => { | ||
| self.nested_files | ||
| .entry(type_and_field.parent_object_entity_name) | ||
| .or_default() | ||
| .entry(type_and_field.selectable_name) | ||
| .or_default() | ||
| .insert(path_and_content.artifact_path.file_name, value); | ||
| } | ||
| None => { | ||
| self.root_files | ||
| .insert(path_and_content.artifact_path.file_name, value); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub fn from_artifacts(artifacts: Vec<ArtifactPathAndContent>) -> Self { | ||
lucasmachadorj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let mut state = Self::default(); | ||
| for artifact in artifacts { | ||
| state.insert(&artifact); | ||
| } | ||
| state | ||
| } | ||
|
|
||
| pub fn is_empty(&self) -> bool { | ||
| self.root_files.is_empty() && self.nested_files.is_empty() | ||
lucasmachadorj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // Computes filesystem operations needed to transform this state into the target state. | ||
| // Returns operations to create, update, and delete files and directories. Files are updated | ||
| // only when their hash differs. If the current state is empty, emits a DeleteDirectory | ||
| // operation for the artifact root followed by recreation of all files. Empty directories | ||
| // are automatically removed after their files are deleted. | ||
| pub fn diff(&self, new: &Self, artifact_directory: &PathBuf) -> Vec<FileSystemOperation> { | ||
lucasmachadorj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let mut operations: Vec<FileSystemOperation> = Vec::new(); | ||
|
|
||
| if self.is_empty() { | ||
| operations.push(FileSystemOperation::DeleteDirectory( | ||
| artifact_directory.to_path_buf(), | ||
| )); | ||
|
|
||
| for (server_object, selectables) in &new.nested_files { | ||
| let server_object_path = artifact_directory.join(server_object.to_string()); | ||
| operations.push(FileSystemOperation::CreateDirectory( | ||
rbalicki2 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| server_object_path.clone(), | ||
| )); | ||
|
|
||
| for (selectable, files) in selectables { | ||
| let selectable_path = server_object_path.join(selectable.to_string()); | ||
| operations.push(FileSystemOperation::CreateDirectory( | ||
| selectable_path.clone(), | ||
| )); | ||
|
|
||
| for (file_name, (content, _)) in files { | ||
| let file_path = selectable_path.join(file_name.to_string()); | ||
| operations.push(FileSystemOperation::WriteFile(file_path, content.clone())); | ||
| } | ||
| } | ||
|
|
||
| for (file_name, (content, _)) in &new.root_files { | ||
lucasmachadorj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let file_path = artifact_directory.join(file_name.to_string()); | ||
| operations.push(FileSystemOperation::WriteFile(file_path, content.clone())); | ||
| } | ||
| } | ||
|
|
||
| return operations; | ||
| } | ||
|
|
||
| let mut new_server_objects: HashSet<&ServerObjectEntityName> = HashSet::new(); | ||
lucasmachadorj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let mut new_selectables: HashSet<(&ServerObjectEntityName, &SelectableName)> = | ||
| HashSet::new(); | ||
|
|
||
| for (server_object, selectables) in &new.nested_files { | ||
| new_server_objects.insert(server_object); | ||
| let server_object_path = artifact_directory.join(server_object.to_string()); | ||
lucasmachadorj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if !self.nested_files.contains_key(server_object) { | ||
| operations.push(FileSystemOperation::CreateDirectory( | ||
| server_object_path.clone(), | ||
| )); | ||
| } | ||
|
|
||
| for (selectable, files) in selectables { | ||
| new_selectables.insert((server_object, selectable)); | ||
| let selectable_path = server_object_path.join(selectable.to_string()); | ||
|
|
||
| let should_create_dir = self | ||
| .nested_files | ||
| .get(server_object) | ||
| .and_then(|s| s.get(selectable)) | ||
| .is_none(); | ||
|
|
||
| if should_create_dir { | ||
| operations.push(FileSystemOperation::CreateDirectory( | ||
| selectable_path.clone(), | ||
| )); | ||
| } | ||
|
|
||
| for (file_name, (new_content, new_hash)) in files { | ||
| let file_path = selectable_path.join(file_name.to_string()); | ||
|
|
||
| let should_write = self | ||
| .nested_files | ||
| .get(server_object) | ||
| .and_then(|s| s.get(selectable)) | ||
| .and_then(|f| f.get(file_name)) | ||
| .map(|(_, old_hash)| old_hash != new_hash) | ||
| .unwrap_or(true); | ||
|
|
||
| if should_write { | ||
| operations.push(FileSystemOperation::WriteFile( | ||
| file_path, | ||
| new_content.clone(), | ||
| )); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (file_name, (new_content, new_hash)) in &new.root_files { | ||
| let file_path = artifact_directory.join(file_name.to_string()); | ||
|
|
||
| let should_write = self | ||
| .root_files | ||
| .get(file_name) | ||
| .map(|(_old_content, old_hash)| old_hash != new_hash) | ||
| .unwrap_or(true); | ||
|
|
||
| if should_write { | ||
| operations.push(FileSystemOperation::WriteFile( | ||
| file_path, | ||
| new_content.clone(), | ||
| )); | ||
| } | ||
| } | ||
|
|
||
| for (server_object, selectables) in &self.nested_files { | ||
lucasmachadorj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let server_object_path = artifact_directory.join(server_object.to_string()); | ||
|
|
||
| for (selectable, files) in selectables { | ||
| let selectable_path = server_object_path.join(selectable.to_string()); | ||
|
|
||
| for file_name in files.keys() { | ||
| let exist_in_new = new | ||
| .nested_files | ||
| .get(server_object) | ||
| .and_then(|s| s.get(selectable)) | ||
| .and_then(|f| f.get(file_name)) | ||
| .is_some(); | ||
|
|
||
| if !exist_in_new { | ||
| let file_path = selectable_path.join(file_name.to_string()); | ||
| operations.push(FileSystemOperation::DeleteFile(file_path)); | ||
| } | ||
| } | ||
|
|
||
| if !new_selectables.contains(&(server_object, selectable)) { | ||
| operations.push(FileSystemOperation::DeleteDirectory(selectable_path)); | ||
| } | ||
| } | ||
|
|
||
| if !new_server_objects.contains(server_object) { | ||
lucasmachadorj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| operations.push(FileSystemOperation::DeleteDirectory(server_object_path)); | ||
| } | ||
| } | ||
|
|
||
| for file_name in self.root_files.keys() { | ||
| if !new.root_files.contains_key(file_name) { | ||
| let file_path = artifact_directory.join(file_name.to_string()); | ||
| operations.push(FileSystemOperation::DeleteFile(file_path)); | ||
| } | ||
| } | ||
|
|
||
| return operations; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,16 +1,18 @@ | ||
| mod eager_reader_artifact; | ||
| mod entrypoint_artifact; | ||
| mod file_system_state; | ||
| mod format_parameter_type; | ||
| pub mod generate_artifacts; | ||
| mod imperatively_loaded_fields; | ||
| mod import_statements; | ||
| mod iso_overload_file; | ||
| mod normalization_ast_text; | ||
| mod operation_text; | ||
| pub mod operation_text; | ||
| mod persisted_documents; | ||
| mod raw_response_type; | ||
| mod reader_ast; | ||
| mod refetch_reader_artifact; | ||
| mod ts_config; | ||
|
|
||
| pub use file_system_state::FileSystemState; | ||
| pub use generate_artifacts::get_artifact_path_and_content; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| use std::path::PathBuf; | ||
|
|
||
| use crate::FileContent; | ||
|
|
||
| #[derive(Debug, Clone)] | ||
| pub enum FileSystemOperation { | ||
lucasmachadorj marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| DeleteDirectory(PathBuf), | ||
| CreateDirectory(PathBuf), | ||
| WriteFile(PathBuf, FileContent), | ||
| DeleteFile(PathBuf), | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.