Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: improvement overall performance of the lib #1

Open
1 of 3 tasks
MASDXI opened this issue Sep 4, 2024 · 0 comments
Open
1 of 3 tasks

perf: improvement overall performance of the lib #1

MASDXI opened this issue Sep 4, 2024 · 0 comments
Assignees
Labels
enhancement New feature or request performance Improvements performance

Comments

@MASDXI
Copy link

MASDXI commented Sep 4, 2024

Currently, code can address high memory usage and heavy async await.

 constructor(web3) {
        this.web3 = typeof web3.eth != 'undefined' ? web3 : { eth: web3 };
        this.isViem = web3 && 'request' in web3 && 'chain' in web3 && 'transport' in web3;
        this.checkedBlocks = {};
        this.savedBlocks = {};
        this.requests = 0;
    }

from above checkedBlocks and savedBlocks has no limit.

    async getBoundaries() {
        this.latestBlock = await this.getBlockWrapper('latest');
        this.firstBlock = await this.getBlockWrapper(1);
        this.blockTime = (parseInt(this.latestBlock.timestamp, 10) - parseInt(this.firstBlock.timestamp, 10)) / (parseInt(this.latestBlock.number, 10) - 1);
    }
    
    // ...
    
     async getBlockWrapper(block) {
        if (this.savedBlocks[block]) return this.savedBlocks[block];
        const { number, timestamp } = await this.web3.eth.getBlock(this.isViem ? (block == 'latest' ? { blockTag: 'latest' } : { blockNumber: block }) : block);
        this.savedBlocks[number] = {
            timestamp: Number(timestamp),
            number: Number(number)
        };
        this.requests++;
        return this.savedBlocks[number];
    }

form above the getBlockWrapper is async await it should be batched together with promise all if it's possible

Migration and Resolution

  • change from web3.eth to directly call by HTTPS| HTTP library like axios, or got
  • change from sequence async await in getBoundaries to promise all commit
  • limit the size of checkedBlocks and savedBlocks or implementing Least Recently Used (LRU)
@MASDXI MASDXI added enhancement New feature or request performance Improvements performance labels Sep 4, 2024
@MASDXI MASDXI self-assigned this Sep 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request performance Improvements performance
Projects
None yet
Development

No branches or pull requests

1 participant