Skip to content

docs: rewrite gas optimization guide for clarity#1777

Open
Dominion116 wants to merge 1 commit intostacks-network:masterfrom
Dominion116:feat/rewrite-gas-docs
Open

docs: rewrite gas optimization guide for clarity#1777
Dominion116 wants to merge 1 commit intostacks-network:masterfrom
Dominion116:feat/rewrite-gas-docs

Conversation

@Dominion116
Copy link

This PR introduces a new documentation guide: Gas Optimization for Clarity Smart Contracts. This guide provides developers with practical strategies to minimize transaction costs and optimize the performance of their contracts on the Stacks blockchain.

While based on standard optimization principles, the content has been written from scratch to align perfectly with the Stacks repository's styling and technical standards.

Key Features
Actionable Strategies: Detailed sections on minimizing storage hits (caching/batching), logic efficiency (early returns), and right-sizing data types.
GitBook Integration: Follows the repository's native styling using {% hint %} blocks and {% code %} titles for a consistent UI/UX.
Performance Patterns: Provides clear "Good" vs. "Bad" code examples to illustrate the impact of different Clarity implementations.
Tooling Guidance: Includes a dedicated section on measuring gas costs using Clarinet’s --costs analysis tool.
New File
docs/build/more-guides/gas-optimization.md

@Dominion116 Dominion116 requested a review from a team as a code owner January 21, 2026 15:51
@CLAassistant
Copy link

CLAassistant commented Jan 21, 2026

CLA assistant check
All committers have signed the CLA.

Copy link
Contributor

@friedger friedger left a comment

Choose a reason for hiding this comment

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

I like the idea of this page, but I guess it is backwards looking generated AI text.

Optimize Clarity smart contracts to minimize execution costs and improve performance.

{% hint style="info" %}
While Stacks transaction costs are significantly lower than Ethereum, efficient gas management ensures a better user experience and enables higher throughput for complex operations like DeFi or NFT mints.
Copy link
Contributor

Choose a reason for hiding this comment

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

Why comparing tx costs with Ethereum? Remove that.

4. **Write Length**: Bytes written to storage.
5. **Runtime**: CPU execution cycles.

Modern Clarity development relies on minimizing storage interactions, as these are the most expensive operations.
Copy link
Contributor

Choose a reason for hiding this comment

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

How are storage interactions most expensive?


Avoid calling `var-get` or `map-get?` multiple times for the same value in a single execution. Cache the value in a `let` binding instead.

{% code title="caching.clar" %}
Copy link
Contributor

Choose a reason for hiding this comment

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

Example does not reduce the costs. Best to share the costs for bad and good in this doc


Consolidate related data into tuples to minimize industrial map writes.

{% code title="batching.clar" %}
Copy link
Contributor

Choose a reason for hiding this comment

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

Best to share the costs for bad and good in this doc


## Efficient Logic

Writing computationally efficient code reduces the `runtime` cost component and prevents reaching execution limits.
Copy link
Contributor

Choose a reason for hiding this comment

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

What are the limits?


### Right-Size Data Types

Smaller data types reduce read/write lengths. Use `buff` instead of `string-utf8` for identifiers or fixed-length hex data.
Copy link
Contributor

Choose a reason for hiding this comment

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

All data is fixed-length in Clarity


## Loops and Iterations

Clarity uses functional patterns like `fold`, `map`, and `filter` for iteration. These should be used cautiously.
Copy link
Contributor

Choose a reason for hiding this comment

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

Some example costs for map and fold might be helpful

Use `define-read-only` for any operation that does not modify state.

{% hint style="info" %}
Read-only functions are "free" when called via API/RPC as they don't require an on-chain transaction.
Copy link
Contributor

Choose a reason for hiding this comment

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

api nodes also have limits.
Best to list the default limits for read-only

Read-only functions are "free" when called via API/RPC as they don't require an on-chain transaction.
{% endhint %}

For complex frontend data needs, create a read-only "view" function that batches multiple data points into a single tuple return, reducing the number of network requests.
Copy link
Contributor

Choose a reason for hiding this comment

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

More read-only view functions come at the costs of higher deploy costs and higher read costs on contract-call execution.


```bash
# Run tests with cost analysis
clarinet test --costs
Copy link
Contributor

Choose a reason for hiding this comment

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

That is historical code.

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.

3 participants