mirror of
https://github.com/Chia-Network/chia-blockchain.git
synced 2026-08-24 10:05:29 -05:00
<!-- Merging Requirements: - Please give your PR a title that is release-note friendly - In order to be merged, you must add the most appropriate category Label (Added, Changed, Fixed) to your PR --> <!-- Explain why this is an improvement (Does this add missing functionality, improve performance, or reduce complexity?) --> ### Purpose: This PR is an optimisation for the way that AddressManager and ExtendedPeerInfo are serialized, which happens periodically in a task called from node_discovery.py As well as the format of the serialization, the serialization algorithm itself has had a multitude of speedups, including removing redundant loops, minimising allocations, and so on. <!-- Does this PR introduce a breaking change? --> ### Current Behavior: Previously we were serialising ExtendedPeerInfo as a string and AddressManager as an `@streamable` `.to_bytes()`. Copying all the data to this class was taking a deal of time. The serialising function also had a lot of complicated pieces of redundant code, looping over data multiple times when 1 pass might do. ### New Behavior: This PR instead serialises AddressManager directly using a bytearray, and refactors the code to reduce the complexity and number of loops. More comments have been added in this area of the codebase as well. ### Testing Notes: This PR includes a new benchmark file for the AddressManager's serialisation. Here are my personal results comparing this PR with `main`: On main: `Average serialize time: 1.544302 seconds` Sum of my optimisations: `Average serialize time: 0.468669 seconds` Feel free to run `benchmarks/address_manager_store.py` to see for yourself. I have also added in a migration test in `chia/_tests/core/full_node/test_address_manager.py` which creates an old serialisation, loads it using the current loading code, then serialises it using the new serialization, and then loads that. It is also checked at each step along the way to ensure that the serialisation has remained good. <!-- Attach any visual examples, or supporting evidence (attach any .gif/video/console output below) -->