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: incremental json parsing for AppGenesis #21249

Merged
merged 6 commits into from
Sep 10, 2024

Conversation

psiphi5
Copy link
Contributor

@psiphi5 psiphi5 commented Aug 11, 2024

Description

Closes: #19269

Incremental JSON parsing for AppGenesis.


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

  • New Features

    • Implemented incremental JSON parsing for improved efficiency during the application genesis process.
    • Enhanced functionality for handling various reader types, including a new fallback mechanism for JSON decoding.
  • Bug Fixes

    • Clarified halt height behavior to restore original functionality prior to version 0.50.0.
    • Improved error handling with more descriptive messages for clearer feedback during unmarshalling.
  • Documentation

    • Updated CHANGELOG.md to reflect recent enhancements and improvements.

Copy link
Contributor

coderabbitai bot commented Aug 11, 2024

Walkthrough

Walkthrough

The changes introduce incremental JSON parsing to the AppGenesisFromReader function in the x/genutil module. This enhancement allows for more efficient handling of io.Reader inputs by checking for the io.ReadSeeker interface, thereby reducing memory usage and improving performance during the application genesis process. The implementation includes improved error handling and a robust fallback mechanism for parsing.

Changes

Files Change Summary
CHANGELOG.md Added an entry for incremental JSON parsing enhancement in x/genutil, improving efficiency of AppGenesis handling.
x/genutil/types/genesis.go Modified AppGenesisFromReader to check for io.ReadSeeker, added fAppGenesis for efficient parsing and error handling.

Assessment against linked issues

Objective Addressed Explanation
Implement incremental JSON parsing to improve efficiency (e.g., #19269)
Avoid unnecessary memory usage by slurping entire reader contents into RAM
Maintain functionality while optimizing performance
Ensure robust error handling and clear feedback during parsing

Tip

New review model

We have updated our review workflow to use the Anthropic's Claude family of models. Please share any feedback in the discussion post on our Discord.


New features

Walkthrough comment now includes:

  • Possibly related PRs: A list of potentially related PRs to help you recall past context.
  • Suggested labels: CodeRabbit can now suggest labels by learning from your past PRs. You can also provide custom labeling instructions in the UI or configuration file.

Notes:

  • Please share any feedback in the discussion post on our Discord.
  • Possibly related PRs, automatic label suggestions based on past PRs, learnings, and possibly related issues require data opt-in (enabled by default).

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.

@github-actions github-actions bot added the C:x/genutil genutil module issues label Aug 11, 2024
@psiphi5 psiphi5 marked this pull request as ready for review August 12, 2024 03:20
@psiphi5 psiphi5 requested a review from a team as a code owner August 12, 2024 03:20
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 (1)
x/genutil/types/genesis.go (1)

104-124: Well-structured function with robust error handling.

The genesisFromReadSeeker function is well-implemented, efficiently handling JSON decoding with a fallback mechanism. The error messages are descriptive and helpful.

Consider addressing the TODO for incremental parsing when the feature becomes available in cmtjson.

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between e30dc06 and eb78dd1.

Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • x/genutil/types/genesis.go (1 hunks)
Files skipped from review due to trivial changes (1)
  • CHANGELOG.md
Additional context used
Path-based instructions (1)
x/genutil/types/genesis.go (1)

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

Additional comments not posted (1)
x/genutil/types/genesis.go (1)

92-102: LGTM! But verify the function usage in the codebase.

The changes to AppGenesisFromReader improve efficiency by leveraging io.ReadSeeker. The fallback mechanism to handle non-seekable readers is well-implemented.

Ensure that all usages of AppGenesisFromReader are compatible with these changes.

Verification successful

Function usage verified and compatible.

The AppGenesisFromReader function is used with io.Reader types across the codebase, ensuring compatibility with the recent changes. No issues found with the current usages.

  • x/genutil/types/genesis.go uses bufio.NewReader(file).
  • x/genutil/types/chain_id_test.go uses strings.NewReader(BenchmarkGenesis).
  • testutil/sims/state_helpers.go uses bufio.NewReader(file).
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `AppGenesisFromReader` are compatible with the new logic.

# Test: Search for the function usage. Expect: Proper handling of io.Reader and io.ReadSeeker.
rg --type go -A 5 $'AppGenesisFromReader'

Length of output: 1564

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 eb78dd1 and 4c6a8e0.

Files selected for processing (1)
  • x/genutil/types/genesis.go (2 hunks)
Additional context used
Path-based instructions (1)
x/genutil/types/genesis.go (1)

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

Additional comments not posted (2)
x/genutil/types/genesis.go (2)

143-150: LGTM! Simplified file reading logic.

The function effectively delegates reading logic to AppGenesisFromReader, enhancing maintainability. The use of filepath.Clean ensures path sanitization.


91-132: LGTM! Incremental JSON parsing improves performance.

The changes effectively enhance performance by utilizing io.ReadSeeker for incremental parsing. The fallback mechanism and improved error handling add robustness.

Verification successful

Consistent use of errors.Join for error handling.

The usage of errors.Join in the AppGenesisFromReader function aligns with its application throughout the codebase, ensuring a consistent and robust error handling strategy.

  • x/genutil/types/genesis.go: Proper usage of errors.Join for combining errors.
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify usage of errors.Join in the codebase.

# Test: Search for occurrences of errors.Join. Expect: Proper usage in error handling.
rg --type go 'errors\.Join'

Length of output: 2224

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: 1

Review details

Configuration used: .coderabbit.yml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 4c6a8e0 and 52c5bb2.

Files selected for processing (1)
  • x/genutil/types/genesis.go (3 hunks)
Additional context used
Path-based instructions (1)
x/genutil/types/genesis.go (1)

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

golangci-lint
x/genutil/types/genesis.go

95-95: naked return in func AppGenesisFromReader with 43 lines of code

(nakedret)


101-101: naked return in func AppGenesisFromReader with 43 lines of code

(nakedret)


109-109: naked return in func AppGenesisFromReader with 43 lines of code

(nakedret)


116-116: naked return in func AppGenesisFromReader with 43 lines of code

(nakedret)


132-132: naked return in func AppGenesisFromReader with 43 lines of code

(nakedret)

Additional comments not posted (1)
x/genutil/types/genesis.go (1)

142-149: LGTM!

The function correctly handles file operations and error management.

x/genutil/types/genesis.go Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
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 52c5bb2 and 28efae2.

Files selected for processing (2)
  • CHANGELOG.md (1 hunks)
  • x/genutil/types/genesis.go (3 hunks)
Files skipped from review as they are similar to previous changes (2)
  • CHANGELOG.md
  • x/genutil/types/genesis.go

@psiphi5
Copy link
Contributor Author

psiphi5 commented Aug 28, 2024

@julienrbrt @kocubinski updated with latest main and resolved all comments

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 28efae2 and 11a3819.

Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Files skipped from review due to trivial changes (1)
  • CHANGELOG.md

@psiphi5
Copy link
Contributor Author

psiphi5 commented Aug 29, 2024

I'm not quite sure why test-system is failing, it ran fine previously (https://github.com/cosmos/cosmos-sdk/actions/runs/10381093612/job/28855511038) and also runs successfully on my local machine.

@julienrbrt
Copy link
Member

Hey, thanks! We'll review

@psiphi5
Copy link
Contributor Author

psiphi5 commented Sep 3, 2024

Hi @kocubinski I've answered the question about io.ReadAll, let me know if there's anything else, otherwise I think we should be good to merge.

Copy link
Member

@kocubinski kocubinski left a comment

Choose a reason for hiding this comment

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

So before the memory cost would've been sizeof(AppGenesis) + sizeof(input_file) but now we have sizeof(AppGenesis) + sizeof(input_file_buffer) which can be a significant when dealing with large files.

Thank you for that clarification, LGTM.

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 11a3819 and 7bccb45.

Files selected for processing (1)
  • CHANGELOG.md (1 hunks)
Files skipped from review due to trivial changes (1)
  • CHANGELOG.md

@psiphi5
Copy link
Contributor Author

psiphi5 commented Sep 10, 2024

ping @julienrbrt @cool-develope, since the rules require 2 approvals, could I please get an additional review on this PR so it can be merged?
JFYI: CI is failing because of Error: API rate limit exceeded for site ID installation

@julienrbrt julienrbrt added the backport/v0.52.x PR scheduled for inclusion in the v0.52's next stable release label Sep 10, 2024
@julienrbrt julienrbrt added this pull request to the merge queue Sep 10, 2024
Merged via the queue into cosmos:main with commit 3db444a Sep 10, 2024
67 of 71 checks passed
mergify bot pushed a commit that referenced this pull request Sep 10, 2024
julienrbrt pushed a commit that referenced this pull request Sep 10, 2024
Co-authored-by: psiphi5 <158285278+psiphi5@users.noreply.github.com>
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:x/genutil genutil module issues
Projects
None yet
4 participants