Solana: Why do instructions consume varying compute units on each execution?

February 1, 2025 5:14 pm Published by

Understanding Solana Compute Unit Consumption

As a developer building applications on the Solana blockchain, you’re probably curious about how your code is executed. One aspect of this process involves compute unit (CU) consumption, which can vary depending on the instructions used in your application. In this article, we’ll explore why instructions use different numbers of compute units in Solana and provide examples that illustrate these differences.

What are compute units?

In Solana, a compute unit is a measure of the computational effort required to execute a command. It is calculated by dividing the amount of gas (the native cryptocurrency used for transaction fees) required to execute a command by the block time (the time it takes for a single transaction to be completed on the network).

Why do instructions use different amounts of compute units?

The main reason for this difference is the difference in the amount of gas and the computational complexity of each instruction. Instructions are divided into several types, including:

  • Gas-efficient instructions

    : These instructions have a lower gas cost, but require more computation. Examples: “call”, “load”, and “store”.

  • Gas-intensive instructions: These instructions have a higher gas cost, but require less computation. Examples: “transfer” and “send”.

The amount of gas required to execute an instruction is determined by its complexity, which includes factors such as address size, data type, and computational requirements.

Measuring CU Consumption with Solana

To understand how these instructions use computational units in Solana, we will create a TypeScript test that executes one of our Ixs (assigned instructions) and measures CU usage. We will compare the results for different gas-efficient and gas-intensive instructions.

import * as solana from '@solana/web3.js';

import { Ix } from './Ix'; // Import your custom Ix implementation

// Create a Solana instance using our test network (e.g. Solana Test Network)

const solanaInstance = new solana.Web3();

const network = 'testnet';

// Run your custom Ix on the test network

Ix.run(solanaInstance, {

address: "your_address",

data: "your_data",

});

Measuring CU Consumption

We will use a library called “solana-cpu” to calculate unit consumption. You can install it using npm:

npm install solana-cpu

Create an index file (“cpu.js”) with the following code:

import * as processor from "solana-cpu";

const Ix = require('./Ix');

// Create a Solana instance using our test network

const solanaInstance = new solana.Web3();

function measureCpuUsage() {

// Run a custom Ix on a test network and measure CU usage

const ctx = {};

return cpu.measureCpuUsage(

Ix.run,

"your_address",

data: "your_data"

);

}

// Calculate unit usage for different gas efficient instructions

const gasEfficientInstructions = ['call', 'load', 'store'];

console.log(CU Usage ${gasEfficientInstructions.join(', ')});

console.log(measureCpuUsage());

// Measure unit usage for high gas instructions

const highGasInstructions = ['transfer', 'send'];

console.log(CU Usage ${highGasInstructions.join(', ')});

console.log(measureCpuUsage());

Sample Output

Executing the above code will output a JSON object with the measured CU usage. The exact results may vary depending on your Solana instance and test network.

“` json

{

“gasEfficientInstructions”: {

“call”: {

“cpu_usage”: “0.01”

},

“upload”: {

“cpu_usage”: “0.02”

},

“store”: {

“cpu_usage”: “0.03”

}

},

“highGasInstructions”: {

“transmit”: {

“cpu_usage”: “0.05”

},

“send”: {

“cpu_usage”: “0.

ETHEREUM WEBSOCKET

Categorised in:

This post was written by Munna

Comments are closed here.