Does importing interfaces increase contract size?

Hi there, since I got an impression that importing contracts can increase the contract code size, I was thinking about the same thing about interfaces. After doing testings, I realize only functions that are actually called in interfaces would have an effect on contract size. That means, however many functions there are in an interface contract, if a fixed number of functions are used, the code size of calling contract would not change. Is this correct? Again, any in-depth explanation is highly appreciated.

1 Like

Generally, if you enable the optimizer then any unused internal function gets removed from the bytecode. This applies to all internal/private functions: ones in your own contract, ones in inherited contracts, ones in libraries, free functions, etc. If you do not optimize, it should be mostly true too but it’s not guaranteed and there might be some cases where compiler includes stuff anyway with the assumption that it will be removed by the optimizer. If you care about bytecode size you should use the optimizer anyway.

External functions of course are not removed when unused but each contract is compiled into a separate binary so importing a file containing another contract with external functions will not affect your bytecode size. Not even if you call them (the call itself will of course be part of your bytecode but not function body).

Interfaces cannot have implemented methods so they do not affect code size at all.

By the way, I see you are asking a lot of questions related to the language (rather than specifically Open Zeppelin) and getting few answers. I hope it won’t be a breach of OZ forum etiquette if I invite you to the Solidity forum. It’s strictly about language design so any questions about the usage of tools, libraries and dapps are off-topic there but you’ll probably get some good answers for questions like this one.