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

perf: Speedup flowrate by removing time.Now() from every update call #3016

Merged
merged 10 commits into from
May 27, 2024

Conversation

ValarDragon
Copy link
Collaborator

@ValarDragon ValarDragon commented May 6, 2024

Closes #2958
Speeds up flowrate by removing time.Now() calls from every clock call, instead making one goroutine responsible for all clock reads. On my most recent mainnet benchmark, this should shave 14% off of the synchronous time in sending packets:

image

There may be slight differences at the boundary of these 20ms windows, but I do not think that is important.


PR checklist

  • Tests written/updated - existing tests pass
  • Changelog entry added in .changelog (we use unclog to manage our changelog)
  • Updated relevant documentation (docs/ or spec/) and code comments
  • Title follows the Conventional Commits spec

@ValarDragon ValarDragon requested review from a team as code owners May 6, 2024 19:42
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 @ValarDragon ❤️

// sleep, then increment the clock value
// This is the only place the clock value is updated.
// Ensures that the clock value starts at 0.
for {
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure how I feel about us not ever exiting here. I guess it's fine.

Copy link
Collaborator Author

@ValarDragon ValarDragon May 7, 2024

Choose a reason for hiding this comment

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

I can use some more CAS instructions to make it exit if all timers are done

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

EDIT didn't work

@cason
Copy link
Contributor

cason commented May 7, 2024

Not sure if I agree with this approach.

Using an atomic int field to keep time at higher granularity is good. Using sleeps to increase it is not great, we can't trust that sleep sleeps what it is supposed to.

An alternative is to use something like time.Tick, which also returns a time, and adjust the increment of the atomic counter, in case the tick is longer than expected. In other words, if we expect a sleep duration of 10ms, but it for some reason (and it will do that), sleeps for 30ms, we increase the atomic counter by 3.

@melekes
Copy link
Contributor

melekes commented May 8, 2024

Using an atomic int field to keep time at higher granularity is good. Using sleeps to increase it is not great, we can't trust that sleep sleeps what it is supposed to.

Good point 👍

@ValarDragon
Copy link
Collaborator Author

Great point! Fixed in latest commit

internal/flowrate/util.go Outdated Show resolved Hide resolved
internal/flowrate/util.go Outdated Show resolved Hide resolved
internal/flowrate/util.go Outdated Show resolved Hide resolved
internal/flowrate/util.go Outdated Show resolved Hide resolved
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.

👍

Copy link

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions bot added the stale For use by stalebot label May 25, 2024
@ValarDragon
Copy link
Collaborator Author

I think this PR is good to go!

Copy link
Contributor

@sergio-mena sergio-mena left a comment

Choose a reason for hiding this comment

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

LGTM

@melekes melekes added this pull request to the merge queue May 27, 2024
Merged via the queue into cometbft:main with commit 4513c68 May 27, 2024
36 checks passed
@sergio-mena sergio-mena removed the stale For use by stalebot label May 27, 2024
ValarDragon added a commit to osmosis-labs/cometbft that referenced this pull request May 31, 2024
…ometbft#3016)

Closes cometbft#2958
Speeds up flowrate by removing time.Now() calls from every `clock` call,
instead making one goroutine responsible for all clock reads. On my most
recent mainnet benchmark, this should shave 14% off of the synchronous
time in sending packets:

![image](https://github.com/cometbft/cometbft/assets/6440154/f2f65080-7bf1-4c4c-9775-774d97ed016a)

There may be slight differences at the boundary of these 20ms windows,
but I do not think that is important.

---

- [x] Tests written/updated - existing tests pass
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [x] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec
mergify bot pushed a commit to osmosis-labs/cometbft that referenced this pull request May 31, 2024
…ometbft#3016)

Closes cometbft#2958
Speeds up flowrate by removing time.Now() calls from every `clock` call,
instead making one goroutine responsible for all clock reads. On my most
recent mainnet benchmark, this should shave 14% off of the synchronous
time in sending packets:

![image](https://github.com/cometbft/cometbft/assets/6440154/f2f65080-7bf1-4c4c-9775-774d97ed016a)

There may be slight differences at the boundary of these 20ms windows,
but I do not think that is important.

---

- [x] Tests written/updated - existing tests pass
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [x] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec

(cherry picked from commit 8d51a10)
@adizere
Copy link
Member

adizere commented Jul 4, 2024

Should we backport anything from here into .37 or .38 ?

@ValarDragon
Copy link
Collaborator Author

Yes please, lets backport to 38!

@melekes
Copy link
Contributor

melekes commented Jul 10, 2024

@mergify backport v1.x

Copy link
Contributor

mergify bot commented Jul 10, 2024

backport v1.x

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Jul 10, 2024
…3016)

Closes #2958
Speeds up flowrate by removing time.Now() calls from every `clock` call,
instead making one goroutine responsible for all clock reads. On my most
recent mainnet benchmark, this should shave 14% off of the synchronous
time in sending packets:

![image](https://github.com/cometbft/cometbft/assets/6440154/f2f65080-7bf1-4c4c-9775-774d97ed016a)

There may be slight differences at the boundary of these 20ms windows,
but I do not think that is important.

---

#### PR checklist

- [x] Tests written/updated - existing tests pass
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [x] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec

(cherry picked from commit 4513c68)

# Conflicts:
#	.changelog/unreleased/improvements/3016-remove-expensive-calls-from-flowrate.md
melekes added a commit that referenced this pull request Jul 10, 2024
…(backport #3016) (#3467)

Closes #2958 
Speeds up flowrate by removing time.Now() calls from every `clock` call,
instead making one goroutine responsible for all clock reads. On my most
recent mainnet benchmark, this should shave 14% off of the synchronous
time in sending packets:


![image](https://github.com/cometbft/cometbft/assets/6440154/f2f65080-7bf1-4c4c-9775-774d97ed016a)

There may be slight differences at the boundary of these 20ms windows,
but I do not think that is important.

---

#### PR checklist

- [x] Tests written/updated - existing tests pass
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [x] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec
<hr>This is an automatic backport of pull request #3016 done by
[Mergify](https://mergify.com).

---------

Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
ValarDragon added a commit to osmosis-labs/cometbft that referenced this pull request Aug 19, 2024
…(backport cometbft#3016) (cometbft#3467)

Closes cometbft#2958
Speeds up flowrate by removing time.Now() calls from every `clock` call,
instead making one goroutine responsible for all clock reads. On my most
recent mainnet benchmark, this should shave 14% off of the synchronous
time in sending packets:

![image](https://github.com/cometbft/cometbft/assets/6440154/f2f65080-7bf1-4c4c-9775-774d97ed016a)

There may be slight differences at the boundary of these 20ms windows,
but I do not think that is important.

---

- [x] Tests written/updated - existing tests pass
- [x] Changelog entry added in `.changelog` (we use
[unclog](https://github.com/informalsystems/unclog) to manage our
changelog)
- [x] Updated relevant documentation (`docs/` or `spec/`) and code
comments
- [x] Title follows the [Conventional
Commits](https://www.conventionalcommits.org/en/v1.0.0/) spec
<hr>This is an automatic backport of pull request cometbft#3016 done by
[Mergify](https://mergify.com).

---------

Co-authored-by: Dev Ojha <ValarDragon@users.noreply.github.com>
Co-authored-by: Anton Kaliaev <anton.kalyaev@gmail.com>
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.

Change flowrate.clock() to be based on a variable that is scheduled to update every 20ms
5 participants