Skip to content

Feat: --optimize flag for contract build#2045

Closed
BlaineHeffron wants to merge 16 commits into
stellar:mainfrom
theahaco:feat/build-optimize
Closed

Feat: --optimize flag for contract build#2045
BlaineHeffron wants to merge 16 commits into
stellar:mainfrom
theahaco:feat/build-optimize

Conversation

@BlaineHeffron
Copy link
Copy Markdown
Contributor

@BlaineHeffron BlaineHeffron commented Jun 12, 2025

What

Adds an --optimize flag to contract build. This uses the same logic from contract optimize; the optimized wasm is output with the usual name.

Example output:

ℹ️  Build Summary (Optimized):
   Wasm File: target/wasm32v1-none/release/stellar_example.wasm
   Wasm Hash: 98f44b624d792833e498b4ca33cdf2a8cebc0b84a0e402d35d17634a45969f20
   Wasm Size: 537 bytes
   Exported Functions: 2 found
     • _
     • hello
   Optimized. 574 → 537 bytes
✅ Build Complete

Why

Saves an extra CLI call to optimize.

Known limitations

N/A

@BlaineHeffron BlaineHeffron requested a review from a team as a code owner June 12, 2025 16:47
@github-project-automation github-project-automation Bot moved this to Backlog (Not Ready) in DevX Jun 12, 2025
Comment thread cmd/soroban-cli/src/commands/contract/build.rs Outdated
Comment thread cmd/soroban-cli/src/wasm.rs Outdated
Copy link
Copy Markdown
Member

@leighmcculloch leighmcculloch left a comment

Choose a reason for hiding this comment

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

Great idea to expose optimize functionality on the build command.

This change as is results in different behaviour between the build and optimize commands. The optimize command doesn't rewrite the file, instead creates a new file. While the build command with the optimize option rewrites the existing file. Can you make it so that they operate the same, and build results in a .optimized.wasm file? The build summary can then output the build info as is, then output an additional couple lines about the optimized file and its size.

Comment thread cmd/soroban-cli/src/commands/contract/build.rs Outdated
Comment thread cmd/soroban-cli/src/commands/contract/build.rs Outdated
@BlaineHeffron
Copy link
Copy Markdown
Contributor Author

I altered the PR to add the optimized wasm file just like with the optimize command. I also removed the arg refactor and just ignore the clippy warning instead. This is what the output looks like now:

ℹ️  Build Summary (Optimized):
   Wasm File: target/wasm32v1-none/release/stellar_example.wasm
   Wasm Hash: c30e7244eb261b2204f446d15c46b8efd364e9befb5c52921454774dc9edeed3
   Exported Functions: 2 found
     • _
     • hello
   Optimized Wasm File: target/wasm32v1-none/release/stellar_example.optimized.wasm
   Wasm Hash: 98f44b624d792833e498b4ca33cdf2a8cebc0b84a0e402d35d17634a45969f20
   Wasm Size: 537 bytes (reduced by 37 bytes)
✅ Build Complete

Copy link
Copy Markdown
Member

@leighmcculloch leighmcculloch left a comment

Choose a reason for hiding this comment

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

Nice. Small asks inline.

Suggestion for some small modifications to the output:

ℹ️  Build Summary:
   Wasm File: target/wasm32v1-none/release/stellar_example.wasm (574 bytes)
   Wasm Hash: c30e7244eb261b2204f446d15c46b8efd364e9befb5c52921454774dc9edeed3
   Exported Functions: 2 found
     • _
     • hello
   Optimized: reduced by 37 bytes
   Wasm File: target/wasm32v1-none/release/stellar_example.optimized.wasm (537 bytes)
   Wasm Hash: 98f44b624d792833e498b4ca33cdf2a8cebc0b84a0e402d35d17634a45969f20
✅ Build Complete

Comment thread cmd/soroban-cli/src/wasm.rs
Comment thread cmd/soroban-cli/src/commands/contract/build.rs
Comment thread cmd/soroban-cli/src/commands/contract/build.rs Outdated
Comment thread cmd/soroban-cli/src/commands/contract/build.rs Outdated
Comment thread cmd/soroban-cli/src/commands/contract/build.rs Outdated
Comment thread cmd/soroban-cli/src/commands/contract/build.rs Outdated
@willemneal
Copy link
Copy Markdown
Contributor

@leighmcculloch in stellar scaffold we place the resulting binary in target/stellar. What is your opinion of in the case where the is --out-dir and --optimize it overwrites the file put in this dir? This shouldn't cause the issues mentioned above with touching the binary generated by rustc.

BlaineHeffron and others added 4 commits June 18, 2025 14:05
Co-authored-by: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com>
Co-authored-by: Leigh McCulloch <351529+leighmcculloch@users.noreply.github.com>
Copy link
Copy Markdown
Member

@leighmcculloch leighmcculloch left a comment

Choose a reason for hiding this comment

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

👏🏻

Comment thread cmd/soroban-cli/src/commands/contract/build.rs Outdated
@leighmcculloch
Copy link
Copy Markdown
Member

@fnando wdyt about this command outputting the optimized file as a separate file? I suggested it to @BlaineHeffron (sorry!) because that would make this command more consistent with the optimize command.

But, with optimize being added here, we maybe want to deprecate the optimize command in favour of this. I can't think of a lot of reason to use the separate optimize command if you can just add --optimize to the build command. And consistency doesn't really matter if they both don't live on.

And, when I think about most compilers, never do compilers output an unoptimized and optimized version. They output whatever the version is that was requested and only that version.

So my earlier ask was probably misguided. We could flip it back after merging this, so that @BlaineHeffron can get this merged.

@fnando
Copy link
Copy Markdown
Member

fnando commented Oct 7, 2025

Closing in favor of #2241

@fnando fnando closed this Oct 7, 2025
@github-project-automation github-project-automation Bot moved this from Backlog (Not Ready) to Done in DevX Oct 7, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

4 participants