-
Notifications
You must be signed in to change notification settings - Fork 74
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
Correctly implement the cpuRateLimit feature #2189
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Automatically approving tomaka's pull requests. This auto-approval will be removed once more maintainers are active.
@@ -38,6 +38,7 @@ const client = smoldot.start({ | |||
forbidWs: false, | |||
forbidNonLocalWs: false, | |||
forbidWss: false, | |||
cpuRateLimit: 0.5, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: I've intentionally left this in the demo so that it mimics what substrate-connect will now do (paritytech/substrate-connect#900)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
poll_duration.as_secs_f64() * *this.max_divided_by_rate_limit_minus_one; | ||
debug_assert!(after_poll_sleep >= 0.0 && !after_poll_sleep.is_nan()); | ||
let max_duration_float: f64 = Duration::MAX.as_secs_f64(); // TODO: turn this into a `const` once `as_secs_f64` is `const` | ||
if !(after_poll_sleep < max_duration_float) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if !(after_poll_sleep < max_duration_float) { | |
if after_poll_sleep >= max_duration_float { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So this is intentional because it's not exactly the same!
If after_poll_sleep
is NaN, then !(after_poll_sleep < max_duration_float)
will be true
but after_poll_sleep >= max_duration_float
will be false
.
It's not super important because it's never supposed to be NaN
, but IMO it doesn't hurt to have it this way.
let max_divided_by_rate_limit_minus_one = | ||
(f64::from(u32::max_value()) / f64::from(rate_limit)) - 1.0; | ||
// Note that it is legal for `max_divided_by_rate_limit_minus_one` to be +infinite | ||
debug_assert!( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there a guidance on when to use debug_asserts? don't you think they tend to clutter the code?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think I've written this down, but I use them for internal-bug-detecting purposes.
In other words, what you put in the debug_assert!
is something that, if false, indicates that there's a bug in the internal logic of the current module.
However if it's an incorrect API usage, then it should be assert!
instead.
The reason is that incorrect API usages tend to happen not because of silly programming mistakes but because it's sometimes hard to understand or keep in mind how the API that you're using is supposed to be used, and thus are more likely to happen and to pass code reviews.
twiggy diff reportDifference in .wasm size before and after this pull request.
|
Correctly implements #2151
The code currently does
u32::max_value().checked_sub(rate_limit)
. This is wrong, it should bechecked_div
.I remember that when I was testing #2151 I was doing
u32::max_value() / rate_limit
, and then at the last second before opening the PR I replaced this with thechecked_
function in order to handle the situation whererate_limit
is 0.In addition to fixing this mistake, I've also changed the code to use floating points instead of integers.
Right now the precision is actually very low: because we round the division to the nearest integer, the possible values for the CPU bound can only be 100%, 50%, 25%, 12.5%, etc. Using floating points fixes this.