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

Audit the use of Array.slice() #4341

Open
twoeths opened this issue Jul 24, 2022 · 0 comments
Open

Audit the use of Array.slice() #4341

twoeths opened this issue Jul 24, 2022 · 0 comments
Labels
prio-medium Resolve this some time soon (tm). scope-memory Issues to reduce and improve memory usage.

Comments

@twoeths
Copy link
Contributor

twoeths commented Jul 24, 2022

Is your feature request related to a problem? Please describe.

Some Array.slice() is used unnecessarily just for api convenience, this not only affects performance (as a new array is copied) but also cause gc to run more frequently. Some examples:

  1. archiveBlocks.ts:
for (let i = 0; i < blocks.length; i += BATCH_SIZE) {
    const toIdx = Math.min(i + BATCH_SIZE, blocks.length);
    const canonicalBlocks = blocks.slice(i, toIdx);

then we loop canonicalBlocks. In this case we can just do a for loop over i and toIdx

  1. stateContextCache
prune(headStateRootHex: RootHex): void {
    const keys = Array.from(this.cache.keys());
    if (keys.length > this.maxStates) {
      // object keys are stored in insertion order, delete keys starting from the front
      for (const key of keys.slice(0, keys.length - this.maxStates)) {

in this case we can just do a regular for loop and break if it goes beyond the limit.

Describe the solution you'd like

Audit the use of Array.slice() in lodestar, only use it if it's really needed

@philknows philknows added prio-medium Resolve this some time soon (tm). scope-memory Issues to reduce and improve memory usage. labels Aug 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
prio-medium Resolve this some time soon (tm). scope-memory Issues to reduce and improve memory usage.
Projects
None yet
Development

No branches or pull requests

2 participants