Skip to content
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

feat: program.pkg Module to Arc<Module> #1715

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

He1pa
Copy link
Contributor

@He1pa He1pa commented Oct 24, 2024

program.pkg HashMap<String, Vec> -> HashMap<String, Vec<Arc>> .
Reduce the performance loss of module ast clone

1. Does this PR affect any open issues?(Y/N) and add issue references (e.g. "fix #123", "re #123".):

…<Arc<Module>>> . Reduce the performance loss of module ast clone

Signed-off-by: he1pa <[email protected]>
@@ -233,7 +233,11 @@ impl<'ctx> TypedResultWalker<'ctx> for Evaluator<'ctx> {
if let Some(modules) = self.program.pkgs.get(&import_stmt.path.node) {
self.push_pkgpath(&pkgpath);
self.init_scope(&pkgpath);
self.compile_ast_modules(modules);
let modules: Vec<Module> = modules
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's still deep clone here. :(

@@ -209,7 +211,7 @@ pub fn validate(val_opt: ValidateOption) -> Result<bool> {
match compile_res.program.pkgs.get_mut(kclvm_ast::MAIN_PKG) {
Some(pkg) => {
if let Some(module) = pkg.first_mut() {
module.body.insert(0, assign_stmt);
Arc::make_mut(module).body.insert(0, assign_stmt);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Arc::make_mut may lead to potential deep copies, consider using Arc<Mutex<Module>>

Makes a mutable reference into the given Arc.

If there are other Arc pointers to the same allocation, then make_mut will [clone] the inner value to a new allocation to ensure unique ownership. This is also referred to as clone-on-write.

However, if there are no other Arc pointers to this allocation, but some [Weak](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-sandbox/workbench/workbench.esm.html) pointers, then the [Weak](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-sandbox/workbench/workbench.esm.html) pointers will be dissociated and the inner value will not be cloned.

See also [get_mut], which will fail rather than cloning the inner value or dissociating [Weak](vscode-file://vscode-app/Applications/Visual%20Studio%20Code.app/Contents/Resources/app/out/vs/code/electron-sandbox/workbench/workbench.esm.html) pointers.

@coveralls
Copy link
Collaborator

coveralls commented Oct 24, 2024

Pull Request Test Coverage Report for Build 11497068535

Details

  • 63 of 85 (74.12%) changed or added relevant lines in 15 files are covered.
  • 19 unchanged lines in 5 files lost coverage.
  • Overall coverage decreased (-0.005%) to 70.533%

Changes Missing Coverage Covered Lines Changed/Added Lines %
kclvm/query/src/override.rs 0 1 0.0%
kclvm/runner/src/lib.rs 0 1 0.0%
kclvm/sema/src/pre_process/config.rs 8 9 88.89%
kclvm/api/src/service/service_impl.rs 0 4 0.0%
kclvm/ast/src/ast.rs 2 17 11.76%
Files with Coverage Reduction New Missed Lines %
kclvm/parser/src/lib.rs 1 96.12%
kclvm/tools/src/LSP/src/tests.rs 1 98.57%
kclvm/tools/src/LSP/src/request.rs 3 64.97%
kclvm/tools/src/LSP/src/state.rs 7 83.86%
kclvm/tools/src/LSP/src/dispatcher.rs 7 65.71%
Totals Coverage Status
Change from base Build 11494468330: -0.005%
Covered Lines: 51799
Relevant Lines: 73439

💛 - Coveralls

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants