Hi @miguelmtzinf,
Thanks for posting in the forum.
I would have expected a contract calling a library and linking to it would have smaller bytecode size based on https://docs.soliditylang.org/en/v0.7.5/using-the-compiler.html?highlight=library#library-linking
I would have expected a contract calling a library, using only internal functions would be inlined so the bytecode size would increase.
calls to internal functions use the internal calling convention, which means that all internal types can be passed and types stored in memory will be passed by reference and not copied. To realize this in the EVM, code of internal library functions and all functions called from therein will at compile time be included in the calling contract, and a regular JUMP
call will be used instead of a DELEGATECALL
.
_From: https://solidity.readthedocs.io/en/latest/contracts.html#libraries_
I tried to create a simple example but it didn't match my expectation. A more realistic example may be required.
Can you share what you have used?
A.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
library A {
function doStuff() public returns (uint256) {
return 42;
}
}
B.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import "./A.sol";
contract B {
event Stuff(uint256 value);
function doStuff() public {
uint256 value = A.doStuff();
emit Stuff(value);
}
}
C.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
library C {
function doStuff() internal returns (uint256) {
return 42;
}
}
D.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import "./C.sol";
contract D {
event Stuff(uint256 value);
function doStuff() public {
uint256 value = C.doStuff();
emit Stuff(value);
}
}
2_deploy.js
// migrations/2_deploy.js
const A = artifacts.require("A");
const B = artifacts.require("B");
const D = artifacts.require("D");
module.exports = async function (deployer) {
await deployer.deploy(A);
await deployer.link(A, B);
await deployer.deploy(B);
await deployer.deploy(D);
};
Bytecode size
I expected that D would have the larger byte code size.
$ jq '.deployedBytecode | length / 2 - 1' build/contracts/B.json
293
$ jq '.deployedBytecode | length / 2 - 1' build/contracts/D.json
184