Your cart is currently empty!
February 6, 2025
Understanding Compute Unit Consumption on Solana
As a developer building applications on the Solana blockchain, you’re likely curious about how your code is executed. One aspect of this process is the compute unit (CU) usage, which can vary depending on the instructions used in your program. In this article, we’ll explore why instructions use different amounts of compute units on the Solana system and illustrate these differences with examples.
What are compute units?
In Solana, a compute unit is the amount of computational effort required to execute an instruction. It’s calculated by dividing the amount of gas (the native cryptocurrency used for transaction fees) required to execute the instruction by the block time (the time it takes to execute a transaction on the network).
Why do instructions use different amounts of compute units?
The main reason for this discrepancy is the difference in the amount of gas and the computational complexity of each instruction. Instructions can be divided into several types, including:
- Gas-efficient instructions: These instructions have a lower gas cost but require more computation. Examples include “call”, “load”, and “store”.
- High-gas-consuming instructions: These instructions have a higher gas cost but require less computation. Examples include “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 on Solana
To understand how these instructions consume compute units on Solana, we will create a TypeScript test that executes one of our Ix (custom instructions) and measures the 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 the custom Ix implementation
// Create a Solana instance with our test network (e.g. Solana Test Network)
const solanaInstance = new solana.Web3();
const network = 'testnet';
// Run the 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 measure compute unit consumption. You can install it using npm:
npm install solana-cpu
Create an index file (cpu.js
) with the following code:
import * as cpu from 'solana-cpu';
const Ix = require('./Ix');
// Create a Solana instance with our test network
const solanaInstance = new solana.Web3();
function measureCpuUsage() {
// Run custom Ix on the test network and measure CU consumption
const ctx = {};
return cpu.measureCpuUsage(
Ix.run,
'your_address',
data: 'your_data'
);
}
// Measure the unit consumption for different gas efficient instructions
const gasEfficientInstructions = ['call', 'load', 'store'];
console.log(CU usage on ${gasEfficientInstructions.join(', ')}
);
console.log(measureCpuUsage());
// Measure the unit consumption of high gas instructions
const highGasInstructions = ['transfer', 'send'];
console.log(CU usage on ${highGasInstructions.join(', ')}
);
console.log(measureCpuUsage());
Example Output
Running the above code will output a JSON object with the measured CU consumption. The exact results may vary depending on the Solana instance and testnet.
“` json
{
“gasEfficientInstructions”: {
“call”: {
“cpu_usage”: “0.01”
},
“load”: {
“cpu_usage”: “0.02”
},
“store”: {
“cpu_usage”: “0.03”
}
},
“highGasInstructions”: {
“transfer”: {
“cpu_usage”: “0.05”
},
“send”: {
“cpu_usage”: “0.