Skip to content

Use less memory for multichain indexers - #583

Merged
DZakh merged 2 commits into
mainfrom
dz/use-less-memory-for-multichain
Jun 12, 2025
Merged

Use less memory for multichain indexers#583
DZakh merged 2 commits into
mainfrom
dz/use-less-memory-for-multichain

Conversation

@DZakh

@DZakh DZakh commented Jun 10, 2025

Copy link
Copy Markdown
Member

Summary by CodeRabbit

  • Bug Fixes
    • Improved handling of the indexing buffer size configuration, allowing the related environment variable to be optional and providing smarter defaults when not set.
  • Refactor
    • Enhanced internal logic for determining buffer size, leading to more flexible and predictable behavior based on environment configuration.

@coderabbitai

coderabbitai Bot commented Jun 10, 2025

Copy link
Copy Markdown
Contributor

Walkthrough

The changes update how the target buffer size is determined for indexing. The environment variable for buffer size is now optional, and if unset, a new logic computes the buffer size based on the number of chain fetchers and batch size. Metrics and state now use this computed value.

Changes

Files Change Summary
codegenerator/cli/templates/static/codegen/src/Env.res Changed retrieval of ENVIO_INDEXING_MAX_BUFFER_SIZE to be optional, removing fallback logic.
codegenerator/cli/templates/static/codegen/src/globalState/GlobalState.res Added logic to compute buffer size if env var is unset; updated metric and state to use computed buffer size.

Sequence Diagram(s)

sequenceDiagram
    participant App
    participant Env
    participant GlobalState

    App->>Env: Get ENVIO_INDEXING_MAX_BUFFER_SIZE (optional)
    Env-->>App: Return Some(size) or None

    App->>GlobalState: Call make()
    GlobalState->>Env: Access targetBufferSize, maxProcessBatchSize
    GlobalState->>GlobalState: Compute targetBufferSize:
        alt ENVIO_INDEXING_MAX_BUFFER_SIZE is set
            GlobalState->>GlobalState: Use env value
        else Not set
            GlobalState->>GlobalState: Compute from batch size and fetchers
        end
    GlobalState->>Prometheus: Update IndexingTargetBufferSize metric
    GlobalState-->>App: Return state with computed buffer size
Loading

Poem

In fields of code where buffers grow,
A rabbit tweaks how sizes flow.
No fallback now, just check and see—
If unset, compute buffer size with glee!
Metrics align, the logic is neat,
This carrot patch is now complete. 🥕


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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>, please review it.
    • Explain this complex logic.
    • 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 explain this code block.
    • @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 explain its main purpose.
    • @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.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

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 generate sequence diagram to generate a sequence diagram of the changes in this 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.

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.

@DZakh
DZakh requested a review from JonoPrest June 10, 2025 13:50

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

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)
codegenerator/cli/templates/static/codegen/src/globalState/GlobalState.res (1)

73-77: Well-implemented adaptive buffer sizing logic.

The implementation correctly handles the optional environment variable and provides intelligent fallback logic that reduces memory usage for multichain indexers by using a smaller buffer multiplier when there are many chains.

Consider extracting the multiplier calculation for improved readability:

  let targetBatchesInBuffer = 3
+ let bufferMultiplier = chainManager.chainFetchers->ChainMap.size > targetBatchesInBuffer ? 1 : targetBatchesInBuffer
  let targetBufferSize = switch Env.targetBufferSize {
    | Some(size) => size
-   | None => Env.maxProcessBatchSize * (chainManager.chainFetchers->ChainMap.size > targetBatchesInBuffer ? 1 : targetBatchesInBuffer)
+   | None => Env.maxProcessBatchSize * bufferMultiplier
  }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 637496f and b9d9a61.

⛔ Files ignored due to path filters (1)
  • scenarios/test_codegen/pnpm-lock.yaml is excluded by !**/pnpm-lock.yaml
📒 Files selected for processing (2)
  • codegenerator/cli/templates/static/codegen/src/Env.res (1 hunks)
  • codegenerator/cli/templates/static/codegen/src/globalState/GlobalState.res (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: build_and_test
🔇 Additional comments (3)
codegenerator/cli/templates/static/codegen/src/Env.res (1)

16-16: LGTM: Environment variable now properly optional.

The change from a mandatory integer with fallback to an optional integer enables dynamic buffer size calculation in GlobalState.res, which aligns with the PR objective of optimizing memory usage for multichain indexers.

codegenerator/cli/templates/static/codegen/src/globalState/GlobalState.res (2)

79-79: Correct Prometheus metric update.

The metric now properly reflects the computed buffer size rather than the raw environment variable, ensuring accurate monitoring of the actual buffer size being used.


85-85: State record properly updated with computed value.

The targetBufferSize field correctly uses the computed value, maintaining consistency between the environment configuration, Prometheus metrics, and internal state.

Comment on lines +74 to +77
let targetBufferSize = switch Env.targetBufferSize {
| Some(size) => size
| None => Env.maxProcessBatchSize * (chainManager.chainFetchers->ChainMap.size > targetBatchesInBuffer ? 1 : targetBatchesInBuffer)
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We don't need multiple batches in the buffer, when there are multiple chains.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Ideally to calculate it dynamically, so it ignores fully synced chains.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

For better memory, it'd be possible to make it less than Env.maxProcessBatchSize per chain. However, I don't want to do it to keep the single-chain batch optimisation working.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice, do you want to add these notes in a comment for future implementation?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'll add them in another PR

@JonoPrest JonoPrest left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

🙏🏼

@DZakh
DZakh enabled auto-merge (squash) June 12, 2025 08:34
@DZakh
DZakh merged commit 5b2caf4 into main Jun 12, 2025
@DZakh
DZakh deleted the dz/use-less-memory-for-multichain branch June 12, 2025 08:44
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