simolus3 / web3dart

Ethereum library, written in Dart.
MIT License
442 stars 270 forks source link

improve speed and memory usage in Token code generation #171

Closed andromeda911 closed 3 years ago

andromeda911 commented 3 years ago

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: image

--- Time for proposed implementation: image

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:

image

--- Heap snapshot from Dart VM Observatory for proposed implementation:

image

Result of generation for ERC20 contract ABI:

Old:

image

New:

image

Thanks.

andromeda911 commented 3 years ago

OK, i`ll do that a bit later. Thanks

simolus3 commented 3 years ago

Sorry about the one failing test - that's on me misconfiguring the secrets in GitHub actions. This looks good to me, thanks!