Skip to content

Conversation

@Sahil-4555
Copy link
Contributor

@Sahil-4555 Sahil-4555 commented Nov 17, 2025

@taratorio @yperbasis @anacrolix @AskAlexSharov
Optimize findnodeByID function and improve benchmark

goos: linux
goarch: amd64
pkg: github.com/erigontech/erigon/p2p/discover
cpu: Intel(R) Core(TM) i7-8565U CPU @ 1.80GHz
                                                          │ new_bench.txt │             old_bench.txt             │
                                                          │    sec/op     │    sec/op     vs base                 │
_findnodeByID/SmallTable_5Results_NoPreferLive-8             1.270µ ± 10%   1.223µ ±  5%         ~ (p=0.240 n=10)
_findnodeByID/SmallTable_16Results_NoPreferLive-8            1.566µ ± 13%   1.605µ ±  8%         ~ (p=1.000 n=10)
_findnodeByID/SmallTable_5Results_PreferLive_AllLive-8       1.234µ ± 13%   2.255µ ±  7%   +82.77% (p=0.000 n=10)
_findnodeByID/SmallTable_16Results_PreferLive_AllLive-8      1.701µ ±  5%   3.006µ ±  3%   +76.74% (p=0.000 n=10)
_findnodeByID/SmallTable_5Results_PreferLive_HalfLive-8      1.330µ ± 12%   2.200µ ±  8%   +65.44% (p=0.000 n=10)
_findnodeByID/SmallTable_16Results_PreferLive_HalfLive-8     1.725µ ± 10%   3.232µ ±  8%   +87.42% (p=0.000 n=10)
_findnodeByID/MediumTable_5Results_NoPreferLive-8            1.260µ ± 13%   1.279µ ±  6%         ~ (p=0.897 n=10)
_findnodeByID/MediumTable_16Results_NoPreferLive-8           1.828µ ±  6%   1.583µ ± 13%   -13.38% (p=0.001 n=10)
_findnodeByID/MediumTable_5Results_PreferLive_AllLive-8      1.297µ ±  8%   2.228µ ±  7%   +71.81% (p=0.000 n=10)
_findnodeByID/MediumTable_16Results_PreferLive_AllLive-8     1.712µ ±  4%   3.056µ ±  6%   +78.50% (p=0.000 n=10)
_findnodeByID/MediumTable_5Results_PreferLive_HalfLive-8     1.255µ ± 13%   2.199µ ±  7%   +75.22% (p=0.000 n=10)
_findnodeByID/MediumTable_16Results_PreferLive_HalfLive-8    1.630µ ±  5%   2.944µ ± 11%   +80.61% (p=0.000 n=10)
_findnodeByID/LargeTable_5Results_NoPreferLive-8             1.592µ ±  9%   1.670µ ± 11%         ~ (p=0.109 n=10)
_findnodeByID/LargeTable_16Results_NoPreferLive-8            2.090µ ±  8%   2.162µ ± 10%         ~ (p=0.315 n=10)
_findnodeByID/LargeTable_5Results_PreferLive_AllLive-8       1.481µ ± 12%   3.218µ ±  7%  +117.25% (p=0.000 n=10)
_findnodeByID/LargeTable_16Results_PreferLive_AllLive-8      1.970µ ±  9%   3.964µ ±  4%  +101.19% (p=0.000 n=10)
_findnodeByID/LargeTable_5Results_PreferLive_HalfLive-8      1.361µ ±  7%   2.723µ ±  8%  +100.15% (p=0.000 n=10)
_findnodeByID/LargeTable_16Results_PreferLive_HalfLive-8     1.802µ ±  6%   3.655µ ±  6%  +102.91% (p=0.000 n=10)
_findnodeByID/FullTable_5Results_NoPreferLive-8              1.438µ ± 17%   1.563µ ± 13%         ~ (p=0.247 n=10)
_findnodeByID/FullTable_16Results_NoPreferLive-8             1.954µ ±  7%   2.290µ ± 13%   +17.23% (p=0.000 n=10)
_findnodeByID/FullTable_5Results_PreferLive_AllLive-8        1.477µ ±  5%   3.304µ ±  7%  +123.74% (p=0.000 n=10)
_findnodeByID/FullTable_16Results_PreferLive_AllLive-8       1.958µ ±  6%   4.172µ ±  7%  +113.07% (p=0.000 n=10)
geomean                                                      1.566µ         2.379µ         +51.90%

                                                          │ new_bench.txt │            old_bench.txt             │
                                                          │     B/op      │    B/op     vs base                  │
_findnodeByID/SmallTable_5Results_NoPreferLive-8               248.0 ± 0%   248.0 ± 0%        ~ (p=1.000 n=10) ¹
_findnodeByID/SmallTable_16Results_NoPreferLive-8              376.0 ± 0%   376.0 ± 0%        ~ (p=1.000 n=10) ¹
_findnodeByID/SmallTable_5Results_PreferLive_AllLive-8         248.0 ± 0%   368.0 ± 0%  +48.39% (p=0.000 n=10)
_findnodeByID/SmallTable_16Results_PreferLive_AllLive-8        376.0 ± 0%   624.0 ± 0%  +65.96% (p=0.000 n=10)
_findnodeByID/SmallTable_5Results_PreferLive_HalfLive-8        248.0 ± 0%   368.0 ± 0%  +48.39% (p=0.000 n=10)
_findnodeByID/SmallTable_16Results_PreferLive_HalfLive-8       376.0 ± 0%   624.0 ± 0%  +65.96% (p=0.000 n=10)
_findnodeByID/MediumTable_5Results_NoPreferLive-8              248.0 ± 0%   248.0 ± 0%        ~ (p=1.000 n=10) ¹
_findnodeByID/MediumTable_16Results_NoPreferLive-8             376.0 ± 0%   376.0 ± 0%        ~ (p=1.000 n=10) ¹
_findnodeByID/MediumTable_5Results_PreferLive_AllLive-8        248.0 ± 0%   368.0 ± 0%  +48.39% (p=0.000 n=10)
_findnodeByID/MediumTable_16Results_PreferLive_AllLive-8       376.0 ± 0%   624.0 ± 0%  +65.96% (p=0.000 n=10)
_findnodeByID/MediumTable_5Results_PreferLive_HalfLive-8       248.0 ± 0%   368.0 ± 0%  +48.39% (p=0.000 n=10)
_findnodeByID/MediumTable_16Results_PreferLive_HalfLive-8      376.0 ± 0%   624.0 ± 0%  +65.96% (p=0.000 n=10)
_findnodeByID/LargeTable_5Results_NoPreferLive-8               248.0 ± 0%   248.0 ± 0%        ~ (p=1.000 n=10) ¹
_findnodeByID/LargeTable_16Results_NoPreferLive-8              376.0 ± 0%   376.0 ± 0%        ~ (p=1.000 n=10) ¹
_findnodeByID/LargeTable_5Results_PreferLive_AllLive-8         248.0 ± 0%   368.0 ± 0%  +48.39% (p=0.000 n=10)
_findnodeByID/LargeTable_16Results_PreferLive_AllLive-8        376.0 ± 0%   624.0 ± 0%  +65.96% (p=0.000 n=10)
_findnodeByID/LargeTable_5Results_PreferLive_HalfLive-8        248.0 ± 0%   368.0 ± 0%  +48.39% (p=0.000 n=10)
_findnodeByID/LargeTable_16Results_PreferLive_HalfLive-8       376.0 ± 0%   624.0 ± 0%  +65.96% (p=0.000 n=10)
_findnodeByID/FullTable_5Results_NoPreferLive-8                248.0 ± 0%   248.0 ± 0%        ~ (p=1.000 n=10) ¹
_findnodeByID/FullTable_16Results_NoPreferLive-8               376.0 ± 0%   376.0 ± 0%        ~ (p=1.000 n=10) ¹
_findnodeByID/FullTable_5Results_PreferLive_AllLive-8          248.0 ± 0%   368.0 ± 0%  +48.39% (p=0.000 n=10)
_findnodeByID/FullTable_16Results_PreferLive_AllLive-8         376.0 ± 0%   624.0 ± 0%  +65.96% (p=0.000 n=10)
geomean                                                        305.4        406.8       +33.21%
¹ all samples are equal

                                                          │ new_bench.txt │             old_bench.txt             │
                                                          │   allocs/op   │  allocs/op   vs base                  │
_findnodeByID/SmallTable_5Results_NoPreferLive-8               6.000 ± 0%    6.000 ± 0%        ~ (p=1.000 n=10) ¹
_findnodeByID/SmallTable_16Results_NoPreferLive-8              7.000 ± 0%    7.000 ± 0%        ~ (p=1.000 n=10) ¹
_findnodeByID/SmallTable_5Results_PreferLive_AllLive-8         6.000 ± 0%   10.000 ± 0%  +66.67% (p=0.000 n=10)
_findnodeByID/SmallTable_16Results_PreferLive_AllLive-8        7.000 ± 0%   12.000 ± 0%  +71.43% (p=0.000 n=10)
_findnodeByID/SmallTable_5Results_PreferLive_HalfLive-8        6.000 ± 0%   10.000 ± 0%  +66.67% (p=0.000 n=10)
_findnodeByID/SmallTable_16Results_PreferLive_HalfLive-8       7.000 ± 0%   12.000 ± 0%  +71.43% (p=0.000 n=10)
_findnodeByID/MediumTable_5Results_NoPreferLive-8              6.000 ± 0%    6.000 ± 0%        ~ (p=1.000 n=10) ¹
_findnodeByID/MediumTable_16Results_NoPreferLive-8             7.000 ± 0%    7.000 ± 0%        ~ (p=1.000 n=10) ¹
_findnodeByID/MediumTable_5Results_PreferLive_AllLive-8        6.000 ± 0%   10.000 ± 0%  +66.67% (p=0.000 n=10)
_findnodeByID/MediumTable_16Results_PreferLive_AllLive-8       7.000 ± 0%   12.000 ± 0%  +71.43% (p=0.000 n=10)
_findnodeByID/MediumTable_5Results_PreferLive_HalfLive-8       6.000 ± 0%   10.000 ± 0%  +66.67% (p=0.000 n=10)
_findnodeByID/MediumTable_16Results_PreferLive_HalfLive-8      7.000 ± 0%   12.000 ± 0%  +71.43% (p=0.000 n=10)
_findnodeByID/LargeTable_5Results_NoPreferLive-8               6.000 ± 0%    6.000 ± 0%        ~ (p=1.000 n=10) ¹
_findnodeByID/LargeTable_16Results_NoPreferLive-8              7.000 ± 0%    7.000 ± 0%        ~ (p=1.000 n=10) ¹
_findnodeByID/LargeTable_5Results_PreferLive_AllLive-8         6.000 ± 0%   10.000 ± 0%  +66.67% (p=0.000 n=10)
_findnodeByID/LargeTable_16Results_PreferLive_AllLive-8        7.000 ± 0%   12.000 ± 0%  +71.43% (p=0.000 n=10)
_findnodeByID/LargeTable_5Results_PreferLive_HalfLive-8        6.000 ± 0%   10.000 ± 0%  +66.67% (p=0.000 n=10)
_findnodeByID/LargeTable_16Results_PreferLive_HalfLive-8       7.000 ± 0%   12.000 ± 0%  +71.43% (p=0.000 n=10)
_findnodeByID/FullTable_5Results_NoPreferLive-8                6.000 ± 0%    6.000 ± 0%        ~ (p=1.000 n=10) ¹
_findnodeByID/FullTable_16Results_NoPreferLive-8               7.000 ± 0%    7.000 ± 0%        ~ (p=1.000 n=10) ¹
_findnodeByID/FullTable_5Results_PreferLive_AllLive-8          6.000 ± 0%   10.000 ± 0%  +66.67% (p=0.000 n=10)
_findnodeByID/FullTable_16Results_PreferLive_AllLive-8         7.000 ± 0%   12.000 ± 0%  +71.43% (p=0.000 n=10)
geomean                                                        6.481         9.051       +39.66%
¹ all samples are equal

@anacrolix anacrolix self-assigned this Dec 1, 2025
@anacrolix
Copy link
Contributor

Not sure why that tool shows improvements in timing in positive direction 😕.

Is this code actually a bottleneck at present?

@Sahil-4555
Copy link
Contributor Author

Sahil-4555 commented Dec 1, 2025

@anacrolix

The benchmark shows positive percentages because the comparison is done as original → optimized (optimized vs original version).
So a positive value here means: “old was X% slower than new”, which is why the tool displays it as a positive improvement.

If the comparison were reversed (optimized → original), the tool would show the same change as a negative percentage.

You can verify this by running the benchmark locally with swapped inputs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants