Understanding Compute Unit Consumption in Solana
As a developer building applications on the Solana blockchain, you are probably curious about how your code executes. One aspect of this process is the Compute Unit (CU) consumption, which can vary depending on the instructions used in the program. In this article, we will look at why instructions consume different amounts of Compute Units in Solana and provide examples to illustrate these differences.
What are Compute Units?
In Solana, a Compute Unit is a measure of the computational effort required to execute an instruction. It is 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 complete one transaction on the network).
Why do instructions consume 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 classified into several types, including:
- Low Gas Instructions: These instructions have a lower gas cost but require more computation. Examples include
call
,load
, andstore
.
- High Gas Instructions: These instructions have a higher gas cost but require less computation. Examples include
transfer
andsend
.
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 in Solana
To understand how these instructions consume compute units in Solana, we will create a TypeScript test that executes one of our Ix (a custom instruction) and measure CU consumption. We will compare the results for different low gas instructions and high gas instructions.
import * as solana from '@solana/web3.js';
import { Ix } from './Ix'; // Import your own Ix implementation
// Create a Solana instance with our test network (e.g. Solana Test Network)
const solanaInstance = new solana.Web3();
const network = 'testnet';
// Run your own Ix implementation in the test network
Ix.run(solanaInstance, {
address: 'your_address',
data: 'your_data',
});
CU usage measurement
We will use a library called solana-cpu
to measure the usage of compute units. 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() {
// Execute a custom Ix on the test network and measure CU usage
const ctx = {};
return cpu.measureCpuUsage(
Ix.run,
'your_address',
data: 'your_data'
);
}
// Measuring compute unit usage for various high gas efficiency instructions
const gasEfficientInstructions = ['call', 'load', 'store'];
console.log(CU usage on ${gasEfficientInstructions.join(', ')}
);
console.log(measureCpuUsage());
// Measuring compute unit usage for high gas efficiency instructions
const highGasInstructions = ['transfer', 'send'];
console.log(CU usage on ${highGasInstructions.join(', ')}
);
console.log(measureCpuUsage());
Sample output
Running the above code will generate a JSON object with the measured CU usage. Exact results may vary depending on Solana instance and test network.
“`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.