NEWTON
Asked
5 months ago
119
views
2
This question has a bounty of $0.00 in testnet ETH. Answers to this question are eligible to win that bounty.
These are testnet contracts. Not real ETH.
$0.00
Hey, I’m learning Cairo and I want to know more about Gas Optimization. In Solidity compiler, there is a difference between writing uint128 and uint256. Similarly to C and other languages, for example:
contract ThisIsNotAnOptimizedContract{ uint128 Zero; uint256 One; uint128 Two; } contract ThisIsAGasOptimizedContract{ uint128 Zero; uint128 Two; uint256 One; }
uint128
variables each occupy a separate uint256
just for themselves, wasting 128 bits for each slot (Like the first contract). The Solidity compiler puts 2 uint128
in the one 256-bit memory slot when they are declared next to each other.
I’m curious if there is any similar issue in Cairo. Uint256
uses two felt objects for low/high bits of the value.
struct Uint256 { // The low 128 bits of the value. low: felt, // The high 128 bits of the value. high: felt, }
Is there any similar practice in Cairo to optimize felt
and Uint256 objects to save some compiler steps?
vargastartup
asked
5 months ago
1
Accepted answer
No there is no similar issue with Cairo.
The idea is that memory is a map that associates an address with a value.
We mainly use Uint256 for composability purposes (using ERC20/ERC721). But as a felt is a 251 bits number and a native type of cairo, I prefer to use it in almost all situations.
Here is an article to help you understand, felt and Uint256 in cairo.
Fricoben
answered
5 months ago
Is uint256 math operators like uint256_le safe? Why do I need to use uint256_check?
Cairo error "Expected expression of type 'starkware.cairo.common.uint256.Uint256' to have an address."
Cairo: How to reassign Uint256 in a conditional
Cairo Error: 'range_check_ptr' cannot be used as an implicit return value. Consider using a 'with' statement.
Fixed Point pow operation error
How can I send a Uint256 amount of ERC20 tokens from L1 to starknet? And how should I build my payload for "sendMessageToL2" to match the Uint256 format of Cairo?
How to import uint256 in Cairo?
Why abi of warp compiled contract does not match original solidity abi?
Cairo: Else if statement
How can I define two functions with the same name but not the same arguments?
Why am I having trouble deploying my Cairo contract?
How to fix invalid character error while invoking starknetjs frontend app in Cairo?
How do I connect my DAPP to Starknet mainnet using starknetjs?
test another new question new