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

refactor: using unsafe.String and unsafe.SliceData #21412

Merged
merged 5 commits into from
Sep 4, 2024

Conversation

cuiweixie
Copy link
Contributor

@cuiweixie cuiweixie commented Aug 27, 2024

Description

Closes: #XXXX


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title, you can find examples of the prefixes below:
  • confirmed ! in the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • reviewed "Files changed" and left comments if necessary
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • updated the relevant documentation or specification, including comments for documenting Go code
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

Please see Pull Request Reviewer section in the contributing guide for more information on how to review a pull request.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic, API design and naming, documentation is accurate, tests and test coverage

Summary by CodeRabbit

  • Improvements
    • Enhanced string conversion processes for improved performance and memory handling across multiple components.
  • Bug Fixes
    • Resolved issue with halt height mechanism to ensure blocks are only committed below the specified halt height.

@cuiweixie cuiweixie requested a review from a team as a code owner August 27, 2024 05:23
Copy link
Contributor

coderabbitai bot commented Aug 27, 2024

Walkthrough

Walkthrough

The changes include updates to the CHANGELOG.md, highlighting improvements in string and slice data management through unsafe.String and unsafe.SliceData, along with a bug fix for the halt height mechanism to ensure it functions correctly. Additionally, several functions relying on UnsafeBytesToStr were modified across various files, shifting to direct unsafe operations for byte-to-string conversions.

Changes

File Change Summary
CHANGELOG.md Added entry for improvements using unsafe.String and unsafe.SliceData; documented bug fix for halt height mechanism.
internal/conv/string.go Updated UnsafeBytesToStr implementation to use unsafe.String.
codec/address/bech32_codec.go Updated BytesToString to use unsafe.String and unsafe.SliceData for byte-to-string conversion.
codec/address/bech32_codec_test.go Modified TestBech32Codec to replace conv.UnsafeBytesToStr with unsafe.String.
internal/conv/string_test.go Removed unsafeConvertBytes function and TestUnsafeBytesToStr from tests.
types/address.go Changed String() methods to use unsafe.String for address string generation.
types/address/hash.go Altered Derive function to process address using unsafe.String.
store/internal/conv/string.go Updated UnsafeBytesToStr implementation to use unsafe.String.
store/v2/internal/conv/string.go Updated UnsafeBytesToStr implementation to use unsafe.String.
x/authz/internal/conv/string.go Updated UnsafeBytesToStr implementation to use unsafe.String.
x/nft/internal/conv/string.go Updated UnsafeBytesToStr implementation to use unsafe.String.

Sequence Diagram(s)

sequenceDiagram
    participant Caller
    participant UnsafeBytesToStr
    participant Unsafe

    Caller->>UnsafeBytesToStr: Call with byte slice
    UnsafeBytesToStr->>Unsafe: Use unsafe.String and unsafe.SliceData
    Unsafe-->>UnsafeBytesToStr: Return constructed string
    UnsafeBytesToStr-->>Caller: Return string
Loading

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 resolve resolve all the CodeRabbit review comments.
  • @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.

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.

@cuiweixie cuiweixie changed the title refacor(internal/conv): using unsafe.String and unsafe.SliceData refacor: using unsafe.String and unsafe.SliceData Aug 27, 2024
@cuiweixie cuiweixie changed the title refacor: using unsafe.String and unsafe.SliceData refactor: using unsafe.String and unsafe.SliceData Aug 27, 2024
Copy link
Contributor

@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

Outside diff range, codebase verification and nitpick comments (2)
CHANGELOG.md (2)

49-49: Enhance documentation with impact explanation.

Consider adding a brief explanation of the potential impact or benefits of using unsafe.String and unsafe.SliceData to provide more context to the readers.

Example:

* (internal) [#21412](https://github.com/cosmos/cosmos-sdk/pull/21412) Using unsafe.String and unsafe.SliceData for improved performance and memory handling.

51-51: Specify the exact version where behavior was changed.

Consider specifying the exact version where the behavior was changed to provide more context to the readers.

Example:

* (baseapp) [#21256](https://github.com/cosmos/cosmos-sdk/pull/21256) Halt height will not commit the block indicated, meaning that if halt-height is set to 10, only blocks until 9 (included) will be committed. This is to go back to the original behavior before a change was introduced in v0.50.0 (exact version).
Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8dd6948 and 74cedeae7f7b436a3b2081b2a3249baadefd155e.

Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • internal/conv/string.go (1 hunks)
Additional context used
Path-based instructions (2)
internal/conv/string.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

CHANGELOG.md (1)

Pattern **/*.md: "Assess the documentation for misspellings, grammatical errors, missing documentation and correctness"

Additional comments not posted (2)
internal/conv/string.go (2)

Line range hint 7-9: LGTM!

The function is correctly implemented and the warning about altering the returned bytes is appropriate.

The code changes are approved.


18-18: LGTM!

The function is correctly implemented and the use of unsafe is appropriate for the intended purpose.

The code changes are approved.

Copy link
Member

@julienrbrt julienrbrt left a comment

Choose a reason for hiding this comment

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

UnsafeBytesToStr is internal to many components, could you do a search and modify it there?

Copy link
Contributor

@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

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 74cedeae7f7b436a3b2081b2a3249baadefd155e and e8e596f2e9c27fe2d4d463d0ee1caeb032ce9f15.

Files selected for processing (6)
  • codec/address/bech32_codec.go (2 hunks)
  • codec/address/bech32_codec_test.go (2 hunks)
  • internal/conv/string.go (1 hunks)
  • internal/conv/string_test.go (1 hunks)
  • types/address.go (4 hunks)
  • types/address/hash.go (2 hunks)
Files skipped from review due to trivial changes (1)
  • internal/conv/string_test.go
Additional context used
Path-based instructions (5)
internal/conv/string.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

types/address/hash.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

codec/address/bech32_codec.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

codec/address/bech32_codec_test.go (2)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.


Pattern **/*_test.go: "Assess the unit test code assessing sufficient code coverage for the changes associated in the pull request"

types/address.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (7)
internal/conv/string.go (1)

Line range hint 5-7: LGTM!

The function is correctly implemented with appropriate warnings about potential segmentation faults.

The code changes are approved.

types/address/hash.go (1)

93-93: LGTM! But ensure thorough testing.

The use of unsafe.String and unsafe.SliceData can improve performance but may impact memory safety. Ensure that the function is tested thoroughly.

The code changes are approved.

codec/address/bech32_codec.go (1)

118-118: LGTM! But ensure thorough testing.

The use of unsafe.String and unsafe.SliceData can improve performance but may impact memory safety. Ensure that the method is tested thoroughly.

The code changes are approved.

codec/address/bech32_codec_test.go (1)

Line range hint 143-148: Verify the safety and correctness of using unsafe.String and unsafe.SliceData.

The new approach directly manipulates memory for conversion, which can improve performance but introduces potential risks related to memory safety. Ensure that the byte slices are not modified after the conversion and that the length is correctly calculated.

types/address.go (3)

Line range hint 289-295: Verify the safety and correctness of using unsafe.String and unsafe.SliceData.

The new approach directly manipulates memory for conversion, which can improve performance but introduces potential risks related to memory safety. Ensure that the byte slices are not modified after the conversion and that the length is correctly calculated.


Line range hint 440-446: Verify the safety and correctness of using unsafe.String and unsafe.SliceData.

The new approach directly manipulates memory for conversion, which can improve performance but introduces potential risks related to memory safety. Ensure that the byte slices are not modified after the conversion and that the length is correctly calculated.


Line range hint 587-593: Verify the safety and correctness of using unsafe.String and unsafe.SliceData.

The new approach directly manipulates memory for conversion, which can improve performance but introduces potential risks related to memory safety. Ensure that the byte slices are not modified after the conversion and that the length is correctly calculated.

codec/address/bech32_codec.go Fixed Show fixed Hide fixed
types/address.go Fixed Show fixed Hide fixed
types/address/hash.go Fixed Show fixed Hide fixed
@julienrbrt
Copy link
Member

julienrbrt commented Aug 27, 2024

Hey, sorry if I wasn't clear, I didn't suggest that we to remove this helper, it is actually useful.
I meant we should update its implementation in other modules, such as x/authz that has it as well in an internal package.

Copy link
Contributor

@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

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e8e596f2e9c27fe2d4d463d0ee1caeb032ce9f15 and f5305fb8c29b6924dd4ad993beab334e6f32d9b0.

Files selected for processing (4)
  • store/internal/conv/string.go (1 hunks)
  • store/v2/internal/conv/string.go (1 hunks)
  • x/authz/internal/conv/string.go (1 hunks)
  • x/nft/internal/conv/string.go (1 hunks)
Additional context used
Path-based instructions (4)
x/authz/internal/conv/string.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/internal/conv/string.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

store/v2/internal/conv/string.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

x/nft/internal/conv/string.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (8)
x/authz/internal/conv/string.go (2)

Line range hint 5-7: LGTM!

The function is correctly implemented and the comment provides a necessary warning.

The code changes are approved.


13-16: LGTM!

The function is correctly implemented and uses unsafe.String for a safer and more explicit conversion.

The code changes are approved.

store/internal/conv/string.go (2)

Line range hint 7-9: LGTM!

The function is correctly implemented and the comment provides a necessary warning.

The code changes are approved.


Line range hint 13-18: LGTM!

The function is correctly implemented and uses unsafe.String for a safer and more explicit conversion.

The code changes are approved.

store/v2/internal/conv/string.go (2)

Line range hint 7-9: LGTM!

The function is correctly implemented and the comment provides a necessary warning.

The code changes are approved.


Line range hint 13-18: LGTM!

The function is correctly implemented and uses unsafe.String for a safer and more explicit conversion.

The code changes are approved.

x/nft/internal/conv/string.go (2)

Line range hint 6-8: LGTM!

The function is correctly implemented and includes a useful warning comment.

The code changes are approved.


17-18: LGTM!

The function is correctly implemented using unsafe.String and unsafe.SliceData, which enhances safety and clarity.

The code changes are approved.

@cuiweixie
Copy link
Contributor Author

Hey, sorry if I wasn't clear, I didn't suggest that we to remove this helper, it is actually useful. I meant we should update its implementation in other modules, such as x/authz that has it as well in an internal package.

Done

Copy link
Contributor

@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

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between f5305fb8c29b6924dd4ad993beab334e6f32d9b0 and 7d1df8a.

Files selected for processing (6)
  • CHANGELOG.md (1 hunks)
  • internal/conv/string.go (1 hunks)
  • store/internal/conv/string.go (1 hunks)
  • store/v2/internal/conv/string.go (1 hunks)
  • x/authz/internal/conv/string.go (1 hunks)
  • x/nft/internal/conv/string.go (1 hunks)
Files skipped from review as they are similar to previous changes (5)
  • CHANGELOG.md
  • internal/conv/string.go
  • store/internal/conv/string.go
  • x/authz/internal/conv/string.go
  • x/nft/internal/conv/string.go
Additional context used
Path-based instructions (1)
store/v2/internal/conv/string.go (1)

Pattern **/*.go: Review the Golang code for conformity with the Uber Golang style guide, highlighting any deviations.

Additional comments not posted (2)
store/v2/internal/conv/string.go (2)

Line range hint 6-8: LGTM!

The function is correctly implemented, and the comment provides necessary warnings about potential segmentation faults.

The code changes are approved.


Line range hint 12-18: LGTM!

The function is correctly implemented, and the comment provides necessary context about its intended use.

The code changes are approved.

@cuiweixie
Copy link
Contributor Author

Hello, Please Review this pr.

Copy link
Contributor

@testinginprod testinginprod left a comment

Choose a reason for hiding this comment

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

lgtm

@julienrbrt julienrbrt added this pull request to the merge queue Sep 4, 2024
@julienrbrt julienrbrt added the backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release label Sep 4, 2024
Merged via the queue into cosmos:main with commit d56bbb8 Sep 4, 2024
72 of 73 checks passed
mergify bot pushed a commit that referenced this pull request Sep 4, 2024
(cherry picked from commit d56bbb8)

# Conflicts:
#	store/internal/conv/string.go
#	store/v2/internal/conv/string.go
julienrbrt added a commit that referenced this pull request Sep 4, 2024
…21545)

Co-authored-by: cui <523516579@qq.com>
Co-authored-by: Julien Robert <julien@rbrt.fr>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release C:Store C:x/authz C:x/nft
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants