Sitelet https://github.com/starkware-libs/cairo/pull/10126
Skip to content

performance(strings): Using less interim strings. - #10126

Merged
orizi merged 1 commit into
mainfrom
orizi/06-18-performance_strings_using_less_interim_strings
Jun 22, 2026
Merged

orizi merged 1 commit into
mainfrom
orizi/06-18-performance_strings_using_less_interim_strings

Conversation

@orizi

@orizi orizi commented Jun 18, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

Replaces .map(|x| format!(...)).format(sep) and .map(|x| format!(...)).join(sep) patterns with .format_with(sep, |x, f| f(&format_args!(...))) across several crates. This avoids allocating an intermediate String for each element during formatting.


Type of change

Please check one:

  • Bug fix (fixes incorrect behavior)
  • New feature
  • Performance improvement
  • Documentation change with concrete technical impact
  • Style, wording, formatting, or typo-only change

Why is this change needed?

The previous pattern called .map(|x| format!(...)) to produce a String per element before joining or formatting them together. itertools::format_with allows formatting each element directly into the output buffer via format_args!, eliminating the per-element heap allocation.


What was the behavior or documentation before?

Each formatted element was first heap-allocated as a String, then those strings were joined or formatted into the final output string.


What is the behavior or documentation after?

Each element is formatted directly into the output using format_args!, avoiding intermediate String allocations per element.


Related issue or discussion (if any)

None.


Additional context

The affected sites are all in diagnostic/debug formatting paths (DiagnosticEntry, Debug impls, assertion messages, and test output), so correctness is unchanged — only allocation behavior differs.

orizi commented Jun 18, 2026 •

Copy link
Copy Markdown
Collaborator Author

@reviewable-StarkWare

Copy link
Copy Markdown

This change is Reviewable

@cursor

cursor Bot commented Jun 18, 2026 •

Copy link
Copy Markdown

PR Summary

Low Risk
Allocation-only refactor on diagnostic, debug, and codegen string building with no logic or API changes.

Overview
Replaces .map(|…| format!(…)).format(…) / .join(…) with itertools::format_with and format_args! so list formatting writes directly into the output buffer instead of heap-allocating a String per element.

Touches lowering (EnumMatch debug, CSE assert), derive plugins (Destruct / PanicDestruct struct bodies), semantic diagnostics and inference ambiguity messages, Sierra debug printing, Starknet dispatcher return-decode indentation, and test runner resource maps. Output text should stay the same; only allocation behavior changes.

Reviewed by Cursor Bugbot for commit 3d9e6b3. Bugbot is set up for automated code reviews on this repo. Configure here.

@eytan-starkware eytan-starkware 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.

@eytan-starkware reviewed 7 files and all commit messages, and made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on orizi and TomerStarkware).


crates/cairo-lang-sierra-generator/src/program_generator.rs line 256 at r1 (raw file):

                        f,
                        "{}",
                        loc.split('\n').format_with("\n", |l, out| out(&format_args!("// {l}")))

Why out? better to rename f to keep the format_with style the same IMO

@orizi
orizi changed the base branch from orizi/06-18-refactor_utils_removed_write_comma_separated_using_equivalent_.format_all_around to graphite-base/10126 June 18, 2026 09:29
@orizi
orizi force-pushed the graphite-base/10126 branch from 9bba9d0 to 8914649 Compare June 18, 2026 09:41
@orizi
orizi force-pushed the orizi/06-18-performance_strings_using_less_interim_strings branch from 76f2fca to 3d9e6b3 Compare June 18, 2026 09:41
@orizi
orizi changed the base branch from graphite-base/10126 to main June 18, 2026 09:41

@orizi orizi left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@orizi made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on eytan-starkware and TomerStarkware).


crates/cairo-lang-sierra-generator/src/program_generator.rs line 256 at r1 (raw file):

Previously, eytan-starkware wrote…

Why out? better to rename f to keep the format_with style the same IMO

i think the AI decided it was confusing since there's another f here.
but Done.

@TomerStarkware TomerStarkware 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.

:lgtm:

@TomerStarkware reviewed 3 files and all commit messages, and made 1 comment.
Reviewable status: all files reviewed, 1 unresolved discussion (waiting on eytan-starkware).

@orizi
orizi enabled auto-merge June 18, 2026 10:02

@eytan-starkware eytan-starkware 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.

:lgtm:

@eytan-starkware reviewed 3 files and all commit messages, made 1 comment, and resolved 1 discussion.
Reviewable status: :shipit: complete! all files reviewed, all discussions resolved (waiting on orizi).

@orizi
orizi added this pull request to the merge queue Jun 22, 2026
Merged via the queue into main with commit 186de1b Jun 22, 2026
106 checks passed
@orizi
orizi deleted the orizi/06-18-performance_strings_using_less_interim_strings branch June 23, 2026 07:45
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.

4 participants