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

Anvil Write Performance Issue #4399

Closed
2 tasks done
PhilippLgh opened this issue Feb 20, 2023 · 8 comments
Closed
2 tasks done

Anvil Write Performance Issue #4399

PhilippLgh opened this issue Feb 20, 2023 · 8 comments
Assignees
Labels
C-anvil Command: anvil T-bug Type: bug
Milestone

Comments

@PhilippLgh
Copy link

PhilippLgh commented Feb 20, 2023

Component

Anvil

Have you ensured that all of these are up to date?

  • Foundry
  • Foundryup

What version of Foundry are you on?

forge 0.2.0 (e2fa2b5 2023-02-19T00:05:02.282096Z)

What command(s) is the bug in?

No response

Operating System

None

Describe the bug

Anvil seems to have big performance issues when lots of data are written:

Test Contract

contract TestContract{

  mapping(uint => uint) someData;

  function writeData(uint256[] memory numbersA, uint256[] memory numbersB) public {
    for (uint i = 0; i < numbersA.length; i++) {
      someData[numbersA[i]] = numbersB[i];
    }
  }

}

Writing Data in chunks of 150 + 150 uint

  let t0 = Date.now()
  for ([numbersA, numbersB] of chunk) {
    console.log('write data chunk', i++, data.length, 'after', Date.now()-t0, 'ms')
    tx = await contract.writeData(numbersA, numbersB)
    receipt = await tx.wait()
  }

Result console.log output

writing chunks 60
write data chunk 0 150 after 0 ms
write data chunk 1 150 after 26 ms
write data chunk 2 150 after 43 ms
write data chunk 3 150 after 62 ms
write data chunk 4 150 after 79 ms
write data chunk 5 150 after 93 ms
write data chunk 6 150 after 110 ms
write data chunk 7 150 after 126 ms
write data chunk 8 150 after 146 ms
write data chunk 9 150 after 164 ms
write data chunk 10 150 after 181 ms
write data chunk 11 150 after 195 ms
write data chunk 12 150 after 212 ms
write data chunk 13 150 after 227 ms
write data chunk 14 150 after 243 ms
write data chunk 15 150 after 263 ms
write data chunk 16 150 after 4287 ms
write data chunk 17 150 after 8322 ms
write data chunk 18 150 after 12368 ms
write data chunk 19 150 after 16414 ms
write data chunk 20 150 after 20449 ms
write data chunk 21 150 after 24474 ms
write data chunk 22 150 after 24507 ms
write data chunk 23 150 after 28534 ms
write data chunk 24 150 after 32570 ms
write data chunk 25 150 after 36616 ms
write data chunk 26 150 after 40657 ms

Subsequent writes on same node will be incredibly slow and node needs to be restarted.

@PhilippLgh PhilippLgh added the T-bug Type: bug label Feb 20, 2023
@gakonst gakonst added this to Foundry Feb 20, 2023
@github-project-automation github-project-automation bot moved this to Todo in Foundry Feb 20, 2023
@mattsse
Copy link
Member

mattsse commented Feb 20, 2023

could you perhaps prepare a repro for this?

@PhilippLgh
Copy link
Author

Can try. What's best way to do this?

@mattsse
Copy link
Member

mattsse commented Feb 20, 2023

smol repository with an example that I can just run via yarn

my js skills are incredibly limited so support on this would speed up debugging immensely.
ty!

@PhilippLgh
Copy link
Author

Let me know if you need assistance in running the test

https://github.com/PhilippLgh/anvil-perf

@mattsse mattsse self-assigned this Feb 21, 2023
@onbjerg onbjerg added the C-anvil Command: anvil label Feb 27, 2023
@DeRain
Copy link

DeRain commented May 22, 2024

@mattsse hey folks! faced the same issue there. Do you have any updates or maybe config recommendations to speedup the node?

Thank you!

@DeRain
Copy link

DeRain commented May 22, 2024

I think this issue is related as well #7631

@zerosnacks zerosnacks added this to the v1.0.0 milestone Jul 26, 2024
@grandizzy
Copy link
Collaborator

@PhilippLgh from what I can tell there is no perf issue with Anvil here, the bottleneck looks to be ethers library, code here
https://github.com/PhilippLgh/anvil-perf/blob/main/test/WriteData.test.ts#L66-L69

    for (const [numbersA, numbersB] of testData) {
      console.log('write data chunk', i++, numbersA.length, 'after', Date.now()-t0, 'ms')
      tx = await contract.writeData(numbersA, numbersB)
      await tx.wait()
    }

blocks at some point in tx.wait() and requests done on Anvil side are starting to slow down. If you remove the await tx.wait() you can see in console txes quickly done and mined. As an experiment I replaced the await tx.wait() part with

  fetch("http://localhost:8543", {
          method: "POST",
          body: JSON.stringify({
              method: "eth_getTransactionReceipt",
              params: [hash],
              id: 1,
              jsonrpc: "2.0"
          }),
          headers: {
              "Content-type": "application/json; charset=UTF-8"
          }
      })
      .then((response) => response.json())
      .then((json) => console.log(json));

to make sure receipts are properly retrieved and your test went really fast (see output below)

What's likely to happen here is ethers-io/ethers.js#4224 and a possible fix in ethers ethers-io/ethers.js#4229

Wonder if you could retest this given this info, thank you

test data ready 66
write data chunk 0 150 before 0 ms
write data chunk 1 150 before 48 ms
write data chunk 2 150 before 71 ms
write data chunk 3 150 before 92 ms
write data chunk 4 150 before 114 ms
write data chunk 5 150 before 135 ms
write data chunk 6 150 before 155 ms
write data chunk 7 150 before 175 ms
write data chunk 8 150 before 196 ms
write data chunk 9 150 before 217 ms
write data chunk 10 150 before 239 ms
write data chunk 11 150 before 258 ms
write data chunk 12 150 before 277 ms
write data chunk 13 150 before 296 ms
write data chunk 14 150 before 315 ms
write data chunk 15 150 before 333 ms
write data chunk 16 150 before 352 ms
write data chunk 17 150 before 371 ms
write data chunk 18 150 before 389 ms
write data chunk 19 150 before 406 ms
write data chunk 20 150 before 426 ms
write data chunk 21 150 before 445 ms
write data chunk 22 150 before 464 ms
write data chunk 23 150 before 482 ms
write data chunk 24 150 before 501 ms
write data chunk 25 150 before 520 ms
write data chunk 26 150 before 537 ms
write data chunk 27 150 before 554 ms
write data chunk 28 150 before 572 ms
write data chunk 29 150 before 592 ms
write data chunk 30 150 before 612 ms
write data chunk 31 150 before 629 ms
write data chunk 32 150 before 649 ms
write data chunk 33 150 before 668 ms
write data chunk 34 150 before 687 ms
write data chunk 35 150 before 704 ms
write data chunk 36 150 before 721 ms
write data chunk 37 150 before 740 ms
write data chunk 38 150 before 758 ms
write data chunk 39 150 before 777 ms
write data chunk 40 150 before 796 ms
write data chunk 41 150 before 814 ms
write data chunk 42 150 before 833 ms
write data chunk 43 150 before 852 ms
write data chunk 44 150 before 871 ms
write data chunk 45 150 before 891 ms
write data chunk 46 150 before 908 ms
write data chunk 47 150 before 928 ms
write data chunk 48 150 before 946 ms
write data chunk 49 150 before 965 ms
write data chunk 50 150 before 983 ms
write data chunk 51 150 before 1004 ms
write data chunk 52 150 before 1023 ms
write data chunk 53 150 before 1044 ms
write data chunk 54 150 before 1064 ms
write data chunk 55 150 before 1085 ms
write data chunk 56 150 before 1105 ms
write data chunk 57 150 before 1126 ms
write data chunk 58 150 before 1146 ms
write data chunk 59 150 before 1166 ms
write data chunk 60 150 before 1187 ms
write data chunk 61 150 before 1210 ms
write data chunk 62 150 before 1230 ms
write data chunk 63 150 before 1253 ms
write data chunk 64 150 before 1275 ms
write data chunk 65 150 before 1296 ms

@grandizzy
Copy link
Collaborator

Closing this per comment above, likely related to ethersjs wait lock. Please reopen if you still see the issue after updating test driver. Thank you!

@grandizzy grandizzy closed this as not planned Won't fix, can't repro, duplicate, stale Oct 19, 2024
@github-project-automation github-project-automation bot moved this from Todo to Done in Foundry Oct 19, 2024
@grandizzy grandizzy moved this from Done to Completed in Foundry Oct 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-anvil Command: anvil T-bug Type: bug
Projects
Status: Completed
Development

No branches or pull requests

6 participants