Skip to content

use copy_from_slice over write_all #4437

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
merged 2 commits into from
Mar 26, 2025
Merged

use copy_from_slice over write_all #4437

merged 2 commits into from
Mar 26, 2025

Conversation

nkysg
Copy link
Collaborator

@nkysg nkysg commented Mar 25, 2025

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Other information

Summary by CodeRabbit

  • Refactor
    • Streamlined data processing by replacing intermediate write methods with direct slice copy operations for improved clarity and performance.
    • Simplified header and job response updates while maintaining existing functionality.

@nkysg nkysg requested a review from sanlee42 as a code owner March 25, 2025 06:53
Copy link

coderabbitai bot commented Mar 25, 2025

Walkthrough

This pull request streamlines how data is written into byte slices in two modules. In the consensus module, the function setting the header’s nonce now directly copies the little-endian bytes for both the nonce and additional data into the header. In the stratum module, updating the minting blob in the job response now directly copies the worker ID buffer into a slice, replacing the previous mutable borrow and write operations. No functional or public interface changes are made.

Changes

File(s) Change Summary
consensus/src/lib.rs Replaced write_u32 and write_all calls with direct copy_from_slice for copying the nonce (in little-endian format) and extra data.
stratum/src/rpc.rs Replaced mutable borrow with write_all by using copy_from_slice to update the minting_blob slice with worker_id.buff.

Sequence Diagram(s)

sequenceDiagram
    participant Caller as Function Caller
    participant SetHeader as set_header_nonce
    participant Header as Header Array

    Caller->>SetHeader: Call set_header_nonce(nonce, extra)
    SetHeader->>Header: Copy nonce bytes via copy_from_slice
    SetHeader->>Header: Copy extra data via copy_from_slice
    SetHeader-->>Caller: Return updated header
Loading
sequenceDiagram
    participant Caller as Function Caller
    participant FromMethod as StratumJobResponse::from
    participant MintingBlob as Minting Blob

    Caller->>FromMethod: Call from(worker_id)
    FromMethod->>MintingBlob: Copy worker_id.buff using copy_from_slice
    FromMethod-->>Caller: Return StratumJobResponse instance
Loading

Poem

I hopped through lines of code so neat,
Leaving clunky writes far behind my feet.
With copy_from_slice, the bytes align,
A smoother hop on the data line.
Celebrating each change with a joyful beat!
🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between eb20039 and b324bfc.

📒 Files selected for processing (2)
  • consensus/src/lib.rs (1 hunks)
  • stratum/src/rpc.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • consensus/src/lib.rs
  • stratum/src/rpc.rs
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build and test

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (1)
consensus/src/lib.rs (1)

4-11: Consider updating import statement

Since the refactoring has removed the usage of WriteBytesExt methods, you could also remove the related import:

-use byteorder::{LittleEndian, WriteBytesExt};
+use byteorder::LittleEndian;

The LittleEndian type is still used elsewhere in the file (e.g., line 114), so it should be kept.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 2505910 and eb20039.

📒 Files selected for processing (2)
  • consensus/src/lib.rs (1 hunks)
  • stratum/src/rpc.rs (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: Generate code coverage report
🔇 Additional comments (2)
stratum/src/rpc.rs (1)

268-268: Cleaner implementation using copy_from_slice

The change from using borrow_mut().write_all(&worker_id.buff) to copy_from_slice(&worker_id.buff) is a good improvement. This simplifies the code by eliminating the need for mutable borrowing and the associated Write trait operations, making it more idiomatic Rust.

The operation is more direct and likely more efficient for this simple buffer copying scenario, as it avoids the additional abstraction layers and potential error handling of the Write trait.

consensus/src/lib.rs (1)

51-52: Code simplification with direct slice operations

This change replaces the previous implementation that used WriteBytesExt methods (write_u32 and write_all) with direct slice operations via copy_from_slice. This is a good refactoring because:

  1. It eliminates intermediate mutable references and method calls
  2. Makes the code more concise and easier to understand
  3. Potentially improves performance by using a more direct approach to memory copying

The implementation correctly handles both the extra bytes and the little-endian conversion of the nonce value, maintaining the original functionality.

Copy link

Benchmark for 8353f6f

Click to view benchmark
Test Base PR %
accumulator_append 726.0±63.23µs 726.9±45.51µs +0.12%
block_apply/block_apply_10 354.7±11.73ms 364.9±14.36ms +2.88%
block_apply/block_apply_1000 38.4±0.38s 40.3±2.31s +4.95%
get_with_proof/db_store 43.0±2.17µs 42.5±1.38µs -1.16%
get_with_proof/mem_store 39.3±7.28µs 35.1±1.89µs -10.69%
put_and_commit/db_store/1 115.8±8.94µs 112.2±6.99µs -3.11%
put_and_commit/db_store/10 1007.1±44.54µs 1044.4±107.24µs +3.70%
put_and_commit/db_store/100 9.3±0.49ms 9.5±0.72ms +2.15%
put_and_commit/db_store/5 529.7±37.62µs 523.2±30.38µs -1.23%
put_and_commit/db_store/50 4.9±0.64ms 4.8±0.23ms -2.04%
put_and_commit/mem_store/1 69.2±7.97µs 68.7±6.52µs -0.72%
put_and_commit/mem_store/10 649.8±64.98µs 675.4±107.68µs +3.94%
put_and_commit/mem_store/100 6.1±0.25ms 6.2±0.60ms +1.64%
put_and_commit/mem_store/5 329.3±39.36µs 319.8±29.26µs -2.88%
put_and_commit/mem_store/50 3.1±0.16ms 3.1±0.13ms 0.00%
query_block/query_block_in(10)_times(100) 8.1±0.29ms 8.2±0.39ms +1.23%
query_block/query_block_in(10)_times(1000) 81.7±3.01ms 81.6±2.47ms -0.12%
query_block/query_block_in(10)_times(10000) 822.8±50.60ms 812.7±34.36ms -1.23%
query_block/query_block_in(1000)_times(100) 1204.9±37.59µs 1165.4±29.26µs -3.28%
query_block/query_block_in(1000)_times(1000) 12.5±1.08ms 11.8±0.23ms -5.60%
query_block/query_block_in(1000)_times(10000) 122.7±7.62ms 125.3±18.57ms +2.12%
storage_transaction 1110.4±424.40µs 1086.8±417.42µs -2.13%
vm/transaction_execution/1 397.4±12.22ms 397.1±13.52ms -0.08%
vm/transaction_execution/10 130.0±9.86ms 128.9±9.91ms -0.85%
vm/transaction_execution/20 114.3±3.24ms 126.0±8.66ms +10.24%
vm/transaction_execution/5 158.9±32.29ms 152.2±4.25ms -4.22%
vm/transaction_execution/50 135.6±4.69ms 131.7±6.72ms -2.88%

Copy link

Benchmark for 2c7a21d

Click to view benchmark
Test Base PR %
accumulator_append 770.5±94.77µs 762.3±100.30µs -1.06%
block_apply/block_apply_10 365.2±12.36ms 363.7±4.36ms -0.41%
block_apply/block_apply_1000 40.8±2.44s 39.4±0.77s -3.43%
get_with_proof/db_store 42.6±1.26µs 43.0±1.40µs +0.94%
get_with_proof/mem_store 35.4±2.80µs 35.4±1.61µs 0.00%
put_and_commit/db_store/1 115.1±9.27µs 113.1±5.84µs -1.74%
put_and_commit/db_store/10 1102.2±122.61µs 1070.7±128.49µs -2.86%
put_and_commit/db_store/100 10.2±1.46ms 9.8±0.78ms -3.92%
put_and_commit/db_store/5 547.2±42.42µs 516.1±33.81µs -5.68%
put_and_commit/db_store/50 4.9±0.30ms 5.1±0.52ms +4.08%
put_and_commit/mem_store/1 70.0±6.71µs 68.2±6.17µs -2.57%
put_and_commit/mem_store/10 653.5±58.31µs 954.2±186.89µs +46.01%
put_and_commit/mem_store/100 6.5±0.51ms 6.4±0.37ms -1.54%
put_and_commit/mem_store/5 322.4±29.74µs 324.9±28.47µs +0.78%
put_and_commit/mem_store/50 3.3±0.57ms 3.2±0.22ms -3.03%
query_block/query_block_in(10)_times(100) 8.2±0.43ms 8.3±0.32ms +1.22%
query_block/query_block_in(10)_times(1000) 84.4±2.77ms 82.7±2.62ms -2.01%
query_block/query_block_in(10)_times(10000) 828.0±19.80ms 816.5±19.78ms -1.39%
query_block/query_block_in(1000)_times(100) 1249.9±112.26µs 1268.3±250.45µs +1.47%
query_block/query_block_in(1000)_times(1000) 12.3±0.38ms 11.9±0.18ms -3.25%
query_block/query_block_in(1000)_times(10000) 120.8±4.27ms 120.7±7.35ms -0.08%
storage_transaction 1117.9±439.29µs 1048.0±443.14µs -6.25%
vm/transaction_execution/1 410.7±18.03ms 417.4±23.97ms +1.63%
vm/transaction_execution/10 136.3±9.09ms 132.1±5.11ms -3.08%
vm/transaction_execution/20 118.4±2.38ms 122.7±3.64ms +3.63%
vm/transaction_execution/5 155.9±4.02ms 168.9±11.75ms +8.34%
vm/transaction_execution/50 139.1±7.09ms 142.2±8.18ms +2.23%

Copy link
Collaborator

@simonjiao simonjiao left a comment

Choose a reason for hiding this comment

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

LGTM

@nkysg nkysg merged commit d0a608a into dag-master Mar 26, 2025
4 of 5 checks passed
@nkysg nkysg deleted the use_copy_from_slice branch March 26, 2025 02:54
simonjiao pushed a commit that referenced this pull request Mar 26, 2025
* use copy_from_slice over write_all

* fix cargo check
simonjiao pushed a commit that referenced this pull request Apr 7, 2025
* use copy_from_slice over write_all

* fix cargo check
simonjiao added a commit that referenced this pull request Apr 7, 2025
* use rust 1.82.0 and add storage2/executor2

* Revert "select default vm executor to fix tests (#4435)"
* Revert "split transactions to different vm (#4432)"
* fix building error
* init storage2 when booting
* add executor2

* use copy_from_slice over write_all (#4437)

* use copy_from_slice over write_all

* fix cargo check

* fix rust upate warning (#4446)

* fix compiler warning

* fix commit

* update clippy

* update rust

* fix rust update warning

* remove unused manifest keys

* remove unncessary default-features

* in flavor of config.toml

---------

Co-authored-by: nk_ysg <[email protected]>
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.

2 participants