Yul: How to Concatenate String in Solidity Inline Assembly & How Much Gas Savings
String is often a very tricky data type to work with in Solidity as it consists and many other fields such as the identifier and the length of it. As string can go beyond 32 bytes, similarly to array, it is necessary to ensure the entire thread of strings are being stored and presented. Concatenation method is not available in Solidity inline assembly but it can be done by the manual displacement of the memory blocks. Below is how traditional concatenation works in Solidity: function concatTraditional() public pure returns(string memory) { string memory A = "Hello "; string memory B = "world"; return string.concat(A, B); } Without optimisation, this contract cost 196,783 gas and running it cost 1,127 gas. Below is how it is done with Yul (with explanation through commenting): function concatInAssembly() public pure returns(string memory) { string memory A = "Hello "; string memory B = "world"; // Strings can be define through functi...