Skip to content

codegen.c: chunk array literals at 64 elements to reduce register pressure#6734

Merged
matz merged 1 commit intomasterfrom
fix/array-chunking
Mar 10, 2026
Merged

codegen.c: chunk array literals at 64 elements to reduce register pressure#6734
matz merged 1 commit intomasterfrom
fix/array-chunking

Conversation

@matz
Copy link
Copy Markdown
Member

@matz matz commented Mar 9, 2026

Summary

  • Array literals (e.g. 100 elements) were consuming up to 99 registers by loading all values before constructing the array
  • Restore 3.4-era chunking at GEN_LIT_ARY_MAX (64 elements), capping nregs at 64 for any-size array literal
  • This fixes compatibility with mruby/c which has a smaller register limit

fixes #6731

Co-authored-by: Claude noreply@anthropic.com

…ssure

Array literals were being built by loading all elements into registers
before constructing the array, requiring nregs proportional to the array
size (e.g. nregs=99 for 100 elements). This exceeds mruby/c's register
limit. Restore 3.4-era chunking at GEN_LIT_ARY_MAX (64) elements.

fixes #6731

Co-authored-by: Claude <noreply@anthropic.com>
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request optimizes the code generation process for array literals by re-implementing a chunking strategy. This modification ensures that large array literals do not consume an excessive number of registers, thereby improving performance and compatibility, particularly for systems with constrained register resources. The core change involves adjusting the limits and logic used during the compilation of array elements.

Highlights

  • Register Pressure Reduction: Array literals are now chunked at 64 elements (GEN_LIT_ARY_MAX) during code generation, which caps the number of registers used for any-sized array literal at 64. This prevents excessive register consumption that previously occurred when loading all values before array construction.
  • Compatibility Fix: This change restores a 3.4-era chunking mechanism, resolving compatibility issues with environments like mruby/c which have smaller register limits, specifically addressing issue Revert array construction behavior to 3.4 style (chunked via OP_ARYPUSH) #6731.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • mrbgems/mruby-compiler/core/codegen.c
    • Updated the initial stack limit variable slimit from GEN_VAL_STACK_MAX to GEN_LIT_ARY_MAX within the codegen_array function.
    • Modified the conditional logic that adjusts the slimit value, changing the comparison from cursp() >= GEN_LIT_ARY_MAX to cursp() >= slimit and the assigned value from INT16_MAX to GEN_VAL_STACK_MAX.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses an issue with excessive register usage for large array literals by reintroducing chunking. The change restores behavior from an older version, capping the number of registers used and fixing compatibility with platforms like mruby/c. The logic appears sound and correctly implements the intended fix. I have one minor suggestion to improve code clarity.

}

if (cursp() >= GEN_LIT_ARY_MAX) slimit = INT16_MAX;
if (cursp() >= slimit) slimit = GEN_VAL_STACK_MAX;
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.

medium

For better readability and to make the logic more self-contained, it's clearer to use GEN_LIT_ARY_MAX directly in this condition instead of slimit. This avoids having to look back to line 3824 to understand the condition's threshold.

  if (cursp() >= GEN_LIT_ARY_MAX) slimit = GEN_VAL_STACK_MAX;

@matz matz merged commit cfc83bb into master Mar 10, 2026
28 of 31 checks passed
@matz matz deleted the fix/array-chunking branch March 18, 2026 04:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Revert array construction behavior to 3.4 style (chunked via OP_ARYPUSH)

1 participant