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

Replace .map instances with for loops in Repository class (abstractRepository.ts) #2749

Closed
3xtr4t3rr3str14l opened this issue Jun 24, 2021 · 3 comments
Labels
prio-low This is nice to have. scope-performance Performance issue and ideas to improve performance.

Comments

@3xtr4t3rr3str14l
Copy link
Contributor

3xtr4t3rr3str14l commented Jun 24, 2021

When I use the following benchmark to compare for loops and maps:

import {BenchmarkRunner} from "@chainsafe/lodestar-utils/test_utils/benchmark";

describe.only("", () => {
  it("", async () => {
    const runner = new BenchmarkRunner("produce block", {
      minMs: 1000,
      runs: 1024,
    });
    
    const length = 100000;
    const data = Array.from({length})
    await runner.run({
      id: "for loop",
      run: async () => {
        const values = [];
        for (let i = 0; i < length; i++) {
          values.push(data[i])
        }
      },
    });
    await runner.run({
      id: "map",
      run: async () => {
        data.map(i => i)
      },
    });
  })
})

I get the following results:

for loop    527.2788 ops/s      1.896530 ms/op   1025 runs    1.950 s
map         320.8606 ops/s      3.116618 ms/op    640 runs    2.001 s

We are currently using .map in keys(), values(), entries(). Maybe we can optimize by replacing the .maps with for loops.

@dapplion
Copy link
Contributor

The time difference is only relevant when looping over large arrays. If the length i less than 1000 it really doesn't matter unless it's a super hot path. Try to analyze how frequently something is looped, if it's a hot path then we should 100% switch to raw for loops

@dapplion dapplion added the scope-performance Performance issue and ideas to improve performance. label Jun 25, 2021
@stale
Copy link

stale bot commented Sep 7, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed in 15 days if no further activity occurs. Thank you for your contributions.

@stale stale bot added the bot:stale label Sep 7, 2021
@dapplion dapplion added the prio-low This is nice to have. label Sep 9, 2021
@stale stale bot removed the bot:stale label Sep 9, 2021
@dapplion
Copy link
Contributor

Most hot paths looping over the entire validator set have been covered with #3760

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
prio-low This is nice to have. scope-performance Performance issue and ideas to improve performance.
Projects
None yet
Development

No branches or pull requests

2 participants