nervosnetwork / ckb

The Nervos CKB is a public permissionless blockchain, and the layer 1 of Nervos network.
https://www.nervos.org
MIT License
1.16k stars 228 forks source link

Fix the performance issue caused by track_entry_statics #4382

Closed chenyukang closed 7 months ago

chenyukang commented 7 months ago

What problem does this PR solve?

When the number of entries is large(>= 100000), we observed a performance regression for _update_tx_pool_for_reorg, the delay may longer than a duration of 10s, the RPC send_transaction also has a noticeable delay when the number of entries increasing a lot.

What is changed and how it works?

The performance regression is caused by a slow implementation of track_entry_statics, which will be called when adding a new entry or changing entry status.

entries.get_by_status(&Status::Pending).len() is slow when entries number is large, _update_tx_pool_for_reorg will iterate the proposals to invoke track_entry_statics multiple times(1000+ in corner case).

This PR will add 3 counts to track the number of pending, gap, proposed to resolve the performance issue.

Related changes

Check List

Tests

Release note

Title Only: Include only the PR title in the release note.