Skip to content

Conversation

odeke-em
Copy link
Contributor

This change makes PeerSet.Remove much more efficient simply by using more idiomatic Go re-slicing to avoid the prior mechanisms of creating fresh peer set lists on just a single remove. While here also added a remedy for a found bug #2158 due to an abstraction that returns a stale slice to its caller in Switch.OnStop.

Benchmark results:

$ benchstat before.txt after.txt
name                 old time/op    new time/op    delta
PeerSetRemoveOne-8     90.5µs ± 4%    95.9µs ±13%     ~     (p=0.218 n=10+10)
PeerSetRemoveMany-8    1.58ms ± 4%    1.50ms ± 1%   -4.98%  (p=0.000 n=10+8)

name                 old alloc/op   new alloc/op   delta
PeerSetRemoveOne-8     8.48kB ± 0%    7.92kB ± 0%   -6.60%  (p=0.000 n=10+10)
PeerSetRemoveMany-8     149kB ± 0%      65kB ± 0%  -56.44%  (p=0.000 n=10+10)

name                 old allocs/op  new allocs/op  delta
PeerSetRemoveOne-8       85.0 ± 0%      73.0 ± 0%  -14.12%  (p=0.000 n=10+10)
PeerSetRemoveMany-8     1.32k ± 0%     1.22k ± 0%   -7.51%  (p=0.000 n=10+10)

which savings become so much more when peers are removed much more frequently for a longer period of time and could mitigate DOS vectors.

Fixes #2157
Fixes #2158

@odeke-em odeke-em requested a review from a team as a code owner January 28, 2024 15:41
@odeke-em odeke-em requested a review from a team January 28, 2024 15:41
@odeke-em odeke-em force-pushed the p2p-fix-inefficient-PeerSet.Remove branch 2 times, most recently from 0eeb681 to 58a353f Compare January 28, 2024 15:49
Copy link
Contributor

@melekes melekes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @odeke-em ❤️

@odeke-em odeke-em force-pushed the p2p-fix-inefficient-PeerSet.Remove branch from 58a353f to 4319780 Compare January 28, 2024 19:12
@odeke-em odeke-em requested a review from melekes January 28, 2024 21:09
This change makes PeerSet.Remove much more efficient simply
by using more idiomatic Go re-slicing to avoid the prior mechanisms
of creating fresh peer set lists on just a single remove.
While here also added a remedy for a found bug cometbft#2158 due to an
abstraction that returns a stale slice to its caller in Switch.OnStop.

Benchmark results:

```shell
$ benchstat before.txt after.txt
name                 old time/op    new time/op    delta
PeerSetRemoveOne-8     90.5µs ± 4%    95.9µs ±13%     ~     (p=0.218 n=10+10)
PeerSetRemoveMany-8    1.58ms ± 4%    1.50ms ± 1%   -4.98%  (p=0.000 n=10+8)

name                 old alloc/op   new alloc/op   delta
PeerSetRemoveOne-8     8.48kB ± 0%    7.92kB ± 0%   -6.60%  (p=0.000 n=10+10)
PeerSetRemoveMany-8     149kB ± 0%      65kB ± 0%  -56.44%  (p=0.000 n=10+10)

name                 old allocs/op  new allocs/op  delta
PeerSetRemoveOne-8       85.0 ± 0%      73.0 ± 0%  -14.12%  (p=0.000 n=10+10)
PeerSetRemoveMany-8     1.32k ± 0%     1.22k ± 0%   -7.51%  (p=0.000 n=10+10)
```

which savings become so much more when peers are removed much more frequently
for a longer period of time and could mitigate DOS vectors.

Fixes cometbft#2157
Fixes cometbft#2158
@odeke-em odeke-em force-pushed the p2p-fix-inefficient-PeerSet.Remove branch from 4319780 to 7854c3e Compare January 29, 2024 07:58
@melekes melekes added the backport-to-v1.x Tell Mergify to backport the PR to v1.x label Jan 29, 2024
@melekes
Copy link
Contributor

melekes commented Jan 29, 2024

@odeke-em can you update the PR or allow core developers to update your fork so we can merge latest main into your branch?

@melekes melekes enabled auto-merge January 30, 2024 07:13
@melekes melekes disabled auto-merge January 30, 2024 07:13
@melekes melekes added the p2p label Jan 30, 2024
@melekes
Copy link
Contributor

melekes commented Feb 6, 2024

Closing in favor of #2246

@melekes melekes closed this Feb 6, 2024
github-merge-queue bot pushed a commit that referenced this pull request Feb 12, 2024
…rformance (#2246)

Original PR: #2159

## Breaking changes

- `[p2p]` Rename `IPeerSet#List` to `Copy`, add `Random`, `ForEach`
methods.
   Rename `PeerSet#List` to `Copy`, add `Random`, `ForEach` methods.

Fixes #2158

## Performance improvement

This change makes PeerSet.Remove much more efficient simply by using
more idiomatic Go re-slicing to avoid the prior mechanisms of creating
fresh peer set lists on just a single remove. While here also added a
remedy for a found bug #2158 due to an abstraction that returns a stale
slice to its caller in Switch.OnStop.

Benchmark results:

```shell
$ benchstat before.txt after.txt
name                 old time/op    new time/op    delta
PeerSetRemoveOne-8     90.5µs ± 4%    95.9µs ±13%     ~     (p=0.218 n=10+10)
PeerSetRemoveMany-8    1.58ms ± 4%    1.50ms ± 1%   -4.98%  (p=0.000 n=10+8)

name                 old alloc/op   new alloc/op   delta
PeerSetRemoveOne-8     8.48kB ± 0%    7.92kB ± 0%   -6.60%  (p=0.000 n=10+10)
PeerSetRemoveMany-8     149kB ± 0%      65kB ± 0%  -56.44%  (p=0.000 n=10+10)

name                 old allocs/op  new allocs/op  delta
PeerSetRemoveOne-8       85.0 ± 0%      73.0 ± 0%  -14.12%  (p=0.000 n=10+10)
PeerSetRemoveMany-8     1.32k ± 0%     1.22k ± 0%   -7.51%  (p=0.000 n=10+10)
```

which savings become so much more when peers are removed much more
frequently for a longer period of time and could mitigate DOS vectors.

Fixes #2157

---------

Co-authored-by: Emmanuel T Odeke <emmanuel@orijtech.com>
Co-authored-by: Sergio Mena <sergio@informal.systems>
mergify bot pushed a commit that referenced this pull request Feb 12, 2024
…rformance (#2246)

Original PR: #2159

## Breaking changes

- `[p2p]` Rename `IPeerSet#List` to `Copy`, add `Random`, `ForEach`
methods.
   Rename `PeerSet#List` to `Copy`, add `Random`, `ForEach` methods.

Fixes #2158

## Performance improvement

This change makes PeerSet.Remove much more efficient simply by using
more idiomatic Go re-slicing to avoid the prior mechanisms of creating
fresh peer set lists on just a single remove. While here also added a
remedy for a found bug #2158 due to an abstraction that returns a stale
slice to its caller in Switch.OnStop.

Benchmark results:

```shell
$ benchstat before.txt after.txt
name                 old time/op    new time/op    delta
PeerSetRemoveOne-8     90.5µs ± 4%    95.9µs ±13%     ~     (p=0.218 n=10+10)
PeerSetRemoveMany-8    1.58ms ± 4%    1.50ms ± 1%   -4.98%  (p=0.000 n=10+8)

name                 old alloc/op   new alloc/op   delta
PeerSetRemoveOne-8     8.48kB ± 0%    7.92kB ± 0%   -6.60%  (p=0.000 n=10+10)
PeerSetRemoveMany-8     149kB ± 0%      65kB ± 0%  -56.44%  (p=0.000 n=10+10)

name                 old allocs/op  new allocs/op  delta
PeerSetRemoveOne-8       85.0 ± 0%      73.0 ± 0%  -14.12%  (p=0.000 n=10+10)
PeerSetRemoveMany-8     1.32k ± 0%     1.22k ± 0%   -7.51%  (p=0.000 n=10+10)
```

which savings become so much more when peers are removed much more
frequently for a longer period of time and could mitigate DOS vectors.

Fixes #2157

---------

Co-authored-by: Emmanuel T Odeke <emmanuel@orijtech.com>
Co-authored-by: Sergio Mena <sergio@informal.systems>
(cherry picked from commit e1ee71c)
mergify bot pushed a commit that referenced this pull request Feb 12, 2024
…rformance (#2246)

Original PR: #2159

## Breaking changes

- `[p2p]` Rename `IPeerSet#List` to `Copy`, add `Random`, `ForEach`
methods.
   Rename `PeerSet#List` to `Copy`, add `Random`, `ForEach` methods.

Fixes #2158

## Performance improvement

This change makes PeerSet.Remove much more efficient simply by using
more idiomatic Go re-slicing to avoid the prior mechanisms of creating
fresh peer set lists on just a single remove. While here also added a
remedy for a found bug #2158 due to an abstraction that returns a stale
slice to its caller in Switch.OnStop.

Benchmark results:

```shell
$ benchstat before.txt after.txt
name                 old time/op    new time/op    delta
PeerSetRemoveOne-8     90.5µs ± 4%    95.9µs ±13%     ~     (p=0.218 n=10+10)
PeerSetRemoveMany-8    1.58ms ± 4%    1.50ms ± 1%   -4.98%  (p=0.000 n=10+8)

name                 old alloc/op   new alloc/op   delta
PeerSetRemoveOne-8     8.48kB ± 0%    7.92kB ± 0%   -6.60%  (p=0.000 n=10+10)
PeerSetRemoveMany-8     149kB ± 0%      65kB ± 0%  -56.44%  (p=0.000 n=10+10)

name                 old allocs/op  new allocs/op  delta
PeerSetRemoveOne-8       85.0 ± 0%      73.0 ± 0%  -14.12%  (p=0.000 n=10+10)
PeerSetRemoveMany-8     1.32k ± 0%     1.22k ± 0%   -7.51%  (p=0.000 n=10+10)
```

which savings become so much more when peers are removed much more
frequently for a longer period of time and could mitigate DOS vectors.

Fixes #2157

---------

Co-authored-by: Emmanuel T Odeke <emmanuel@orijtech.com>
Co-authored-by: Sergio Mena <sergio@informal.systems>
(cherry picked from commit e1ee71c)
mattac21 pushed a commit that referenced this pull request Sep 5, 2025
…rformance (#2246)

Original PR: #2159

- `[p2p]` Rename `IPeerSet#List` to `Copy`, add `Random`, `ForEach`
methods.
   Rename `PeerSet#List` to `Copy`, add `Random`, `ForEach` methods.

Fixes #2158

This change makes PeerSet.Remove much more efficient simply by using
more idiomatic Go re-slicing to avoid the prior mechanisms of creating
fresh peer set lists on just a single remove. While here also added a
remedy for a found bug #2158 due to an abstraction that returns a stale
slice to its caller in Switch.OnStop.

Benchmark results:

```shell
$ benchstat before.txt after.txt
name                 old time/op    new time/op    delta
PeerSetRemoveOne-8     90.5µs ± 4%    95.9µs ±13%     ~     (p=0.218 n=10+10)
PeerSetRemoveMany-8    1.58ms ± 4%    1.50ms ± 1%   -4.98%  (p=0.000 n=10+8)

name                 old alloc/op   new alloc/op   delta
PeerSetRemoveOne-8     8.48kB ± 0%    7.92kB ± 0%   -6.60%  (p=0.000 n=10+10)
PeerSetRemoveMany-8     149kB ± 0%      65kB ± 0%  -56.44%  (p=0.000 n=10+10)

name                 old allocs/op  new allocs/op  delta
PeerSetRemoveOne-8       85.0 ± 0%      73.0 ± 0%  -14.12%  (p=0.000 n=10+10)
PeerSetRemoveMany-8     1.32k ± 0%     1.22k ± 0%   -7.51%  (p=0.000 n=10+10)
```

which savings become so much more when peers are removed much more
frequently for a longer period of time and could mitigate DOS vectors.

Fixes #2157

---------

Co-authored-by: Emmanuel T Odeke <emmanuel@orijtech.com>
Co-authored-by: Sergio Mena <sergio@informal.systems>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-to-v1.x Tell Mergify to backport the PR to v1.x p2p
Projects
None yet
2 participants