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

perf: stop cloning ExternalBlock and related structs #772

Merged
merged 4 commits into from
May 4, 2024

Conversation

dinhani-cw
Copy link
Contributor

No description provided.

@dinhani-cw dinhani-cw requested a review from a team as a code owner May 3, 2024 23:39
Copy link

github-actions bot commented May 3, 2024

PR Review

⏱️ Estimated effort to review [1-5]

2, because the changes are straightforward and involve primarily changing the method signatures and calls to pass references instead of values, which is a common optimization pattern in Rust. The PR is focused and does not introduce complex logic changes.

🧪 Relevant tests

No

🔍 Possible issues

No

🔒 Security concerns

No

Code feedback:
relevant filesrc/eth/primitives/block_header.rs
suggestion      

Consider using value.extra_data.as_ref() instead of value.extra_data.clone().into() to avoid unnecessary cloning if extra_data supports this operation. This change would enhance performance by reducing memory usage and copy operations. [important]

relevant lineextra_data: value.extra_data.clone().into(),


✨ Review tool usage guide:

Overview:
The review tool scans the PR code changes, and generates a PR review which includes several types of feedbacks, such as possible PR issues, security threats and relevant test in the PR. More feedbacks can be added by configuring the tool.

The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on any PR.

  • When commenting, to edit configurations related to the review tool (pr_reviewer section), use the following template:
/review --pr_reviewer.some_config1=... --pr_reviewer.some_config2=...
[pr_reviewer]
some_config1=...
some_config2=...

See the review usage page for a comprehensive guide on using this tool.

Copy link

github-actions bot commented May 3, 2024

PR Code Suggestions

CategorySuggestions                                                                                                                                                       
Enhancement
Improve error handling in CSV import to ensure all necessary operations are completed.

Consider handling the potential error from import_external_to_csv instead of using ? which
might cause the function to return prematurely without executing further necessary logic.
This could be critical especially in scenarios where subsequent operations depend on the
completion of this CSV import.

src/bin/importer_offline.rs [178]

-import_external_to_csv(&stratus_storage, csv, mined_block.clone(), block_index, block_last_index).await?;
+match import_external_to_csv(&stratus_storage, csv, mined_block.clone(), block_index, block_last_index).await {
+    Ok(_) => {},
+    Err(e) => return Err(e),
+}
 
Use a specific error type for better error handling and clarity.

Replace anyhow::Result with a more specific error type in the from_external function to
provide clearer error information and improve error handling.

src/eth/primitives/block.rs [50]

-pub fn from_external(block: &ExternalBlock, executions: Vec<ExternalTransactionExecution>) -> anyhow::Result<Self> {
+pub fn from_external(block: &ExternalBlock, executions: Vec<ExternalTransactionExecution>) -> Result<Self, BlockCreationError> {
 
Performance
Reduce unnecessary data cloning to enhance performance.

Avoid cloning block in reexecute_external by passing a reference instead. This change
reduces unnecessary data duplication, enhancing performance especially with large data
structures.

src/eth/executor.rs [142]

-parallel_executions.push(self.reexecute_external(tx, receipt, block));
+parallel_executions.push(self.reexecute_external(tx, receipt, &block));
 
Optimize memory usage by avoiding unnecessary cloning of string data.

Use as_ref() to convert Option to Option<&str> for extra_data in the BlockHeader struct to avoid
unnecessary cloning.

src/eth/primitives/block_header.rs [183]

-extra_data: value.extra_data.clone().into(),
+extra_data: value.extra_data.as_ref().map(|s| s.as_str().into()),
 
Maintainability
Improve variable naming for better code readability and maintainability.

Consider using a more descriptive variable name than tx and receipt in the match arm for
ParallelExecutionDecision::Reexecute to improve code readability and maintainability.

src/eth/executor.rs [196]

-ParallelExecutionDecision::Reexecute(tx, receipt) => match self.reexecute_external(tx, receipt, block).await {
+ParallelExecutionDecision::Reexecute(transaction, receipt_details) => match self.reexecute_external(transaction, receipt_details, block).await {
 

✨ Improve tool usage guide:

Overview:
The improve tool scans the PR code changes, and automatically generates suggestions for improving the PR code. The tool can be triggered automatically every time a new PR is opened, or can be invoked manually by commenting on a PR.

  • When commenting, to edit configurations related to the improve tool (pr_code_suggestions section), use the following template:
/improve --pr_code_suggestions.some_config1=... --pr_code_suggestions.some_config2=...
[pr_code_suggestions]
some_config1=...
some_config2=...

See the improve usage page for a comprehensive guide on using this tool.

@dinhani-cw dinhani-cw changed the title perf: stop cloning ExternalBlock perf: stop cloning ExternalBlock and related structs May 3, 2024
@dinhani-cw dinhani-cw merged commit 1f00f74 into main May 4, 2024
25 checks passed
@dinhani-cw dinhani-cw deleted the perf-block-clone branch May 4, 2024 00:08
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.

1 participant