solidity – Techniques for Bit Packing Return Values in Solidity

solidity

Setting variables like this can pack them into a single slot:

uint128 v1 =1;
uint128 v2 =2;

But does return values also pack in this way? Does

returns (uint128, uint128)

also pack data?

Will experiment it later myself

Best Answer

Setting variables like this can pack them into a single slot

Your example doesn't work, uint is an alias for uint256, so in that specific example both variable use a full slot each. Maybe it was a typo and you meant uint128 ?

But does return values also pack in this way ?

No. Input and outputs are abi encoded (see the documentation), that is, padded to 32 bytes each with some caveats for dynamic types. Thus, the following function would return two 32 bytes values :

function test() public view returns (uint128, uint128) {
  uint128 a = 1;
  uint128 b = 2;

  return (a, b);
}

I hope that answers your question.

Related Topic