Hi! Your code generation for smart contracts is incredible.
But in a situation where it is necessary to create multiple instances of the generated contract class (for example, ERC-20 tokens in a crypto wallet), the current implementation is ineffective:
class Token extends _i1.GeneratedContract { Token( {required _i1.EthereumAddress address, required _i1.Web3Client client, int? chainId}) : super( _i1.DeployedContract( _i1.ContractAbi.fromJson( '<<contract ABI json in string literal>>', // THIS IS INEFFECTIVE 'Token'), address), client, chainId);
In current implementation for every ContractAbi instance creates string literal of ABI code, from which every ContractAbi instance parses, and it happens for every instance of Token class.
I suggest storing the abi code in a final top-level variable inside the generated file and referring to that variable in the DeployedContract constructor.
Below is a comparison of speed and memory efficiency between the current implementation and the proposed one (for ERC20 contract full ABI sample).
Speed measuring script:
for (var k in List<int>.generate(20, (index) => index)) { var sw = Stopwatch()..start(); var list = []; for (var i in List<int>.generate(1000, (index) => index)) { list.add(Erc20(address: EthereumAddress.fromHex('0x0000000000000000000000000000000000000000'), client: client)); } print(sw.elapsed); }
--- Time for Current implementation:
--- Time for proposed implementation:
Proposed implementation is 3-4x faster.
Memory usage measuring script:
var list = []; for (var i in List<int>.generate(1000, (index) => index)) { list.add(Erc20(address: EthereumAddress.fromHex('0x0000000000000000000000000000000000000000'), client: client)); }
--- Heap snapshot from Dart VM Observatory for Current implementation:
--- Heap snapshot from Dart VM Observatory for proposed implementation:
Hi! Your code generation for smart contracts is incredible.
But in a situation where it is necessary to create multiple instances of the generated contract class (for example, ERC-20 tokens in a crypto wallet), the current implementation is ineffective:
class Token extends _i1.GeneratedContract { Token( {required _i1.EthereumAddress address, required _i1.Web3Client client, int? chainId}) : super( _i1.DeployedContract( _i1.ContractAbi.fromJson( '<<contract ABI json in string literal>>', // THIS IS INEFFECTIVE 'Token'), address), client, chainId);
In current implementation for every ContractAbi instance creates string literal of ABI code, from which every ContractAbi instance parses, and it happens for every instance of Token class.
I suggest storing the abi code in a final top-level variable inside the generated file and referring to that variable in the DeployedContract constructor.
Below is a comparison of speed and memory efficiency between the current implementation and the proposed one (for ERC20 contract full ABI sample).
Speed measuring script:
for (var k in List<int>.generate(20, (index) => index)) { var sw = Stopwatch()..start(); var list = []; for (var i in List<int>.generate(1000, (index) => index)) { list.add(Erc20(address: EthereumAddress.fromHex('0x0000000000000000000000000000000000000000'), client: client)); } print(sw.elapsed); }
--- Time for Current implementation:
--- Time for proposed implementation:
Proposed implementation is 3-4x faster.
Memory usage measuring script:
var list = []; for (var i in List<int>.generate(1000, (index) => index)) { list.add(Erc20(address: EthereumAddress.fromHex('0x0000000000000000000000000000000000000000'), client: client)); }
--- Heap snapshot from Dart VM Observatory for Current implementation:
--- Heap snapshot from Dart VM Observatory for proposed implementation:
Result of generation for ERC20 contract ABI:
Old:
New:
Thanks.