No description provided.
bench: Assert that division by zero is unreachable #9547
pull practicalswift wants to merge 1 commits into bitcoin:master from practicalswift:avoid-potential-division-by-zero-in-benchmark-state-keeprunning changing 1 files +2 −0-
practicalswift commented at 9:00 PM on January 13, 2017: contributor
-
in src/bench/bench.cpp:None in 6f96b2e518 outdated
87 | @@ -88,7 +88,7 @@ bool benchmark::State::KeepRunning() 88 | lastCycles = nowCycles; 89 | ++count; 90 | 91 | - if (now - beginTime < maxElapsed) return true; // Keep going 92 | + if (now - beginTime < maxElapsed || count == 1) return true; // Keep going
gmaxwell commented at 9:56 PM on January 13, 2017:because of the ++count above I don't believe this can be 1 here.
practicalswift commented at 9:59 PM on January 13, 2017:@gmaxwell But there is a
--counton the line below :-)
gmaxwell commented at 10:00 PM on January 13, 2017:yes, my read it can't be 1 or 0, it must be >1 and then the decrements reduces it to 1. If it is possible to be 0 at the division then I believe there is a bug elsewhere (in addition).
practicalswift commented at 10:16 PM on January 13, 2017:If
countwould be guaranteed to be different from zero, then the check on line 50 (if (count == 0) {) wouldn't be needed, right? :-)If the branch
if (count == 0) {is taken then the relevant parts of the remaining code can be reduced to something like:count = 0; // implicit ++count; --count; double average = (now-beginTime)/count;Assuming I'm reading it correctly :-)
Let me know if I'm missing something obvious – this is one of my first contributions so please bear with me :-)
instagibbs commented at 6:05 PM on January 19, 2017:Looks like if
maxElapsedis > 0 then the logic here is redundant for that code path.count=0meansnowandbeginTimeare both 0.
isle2983 commented at 9:34 PM on January 20, 2017:It might be better to fix the issue in the output at the bottom of the function like so:
// Output results - double average = (now-beginTime)/count; - int64_t averageCycles = (nowCycles-beginCycles)/count; + double average = (count > 0) ? (now-beginTime)/count : 0.0; + int64_t averageCycles = (count > 0) ? (nowCycles-beginCycles)/count : 0; std::cout << std::fixed << std::setprecision(15) << name << "," << count << "," << minTime << "," << maxTime << "," << average << "," << minCycles << "," << maxCycles << "," << averageCycles << "\n";I would say that this reads ok in isolation as just dumb output code and avoids having to understand the branching in function body above.
fanquake added the label Tests on Jan 14, 2017practicalswift commented at 4:35 PM on January 18, 2017: contributorIf we're guaranteed that
countcannot be0in this context, should I remove the then redundantif (count == 0) {-check on line 50 instead?As said, please let me know if I'm missing something obvious here and I'll close the PR :-)
MarcoFalke commented at 2:24 AM on January 22, 2017: memberAgree with @instagibbs that the code is redundant. Maybe just add a comment or assert that says the count is never zero?
practicalswift force-pushed on Jan 22, 2017practicalswift force-pushed on Jan 22, 2017practicalswift force-pushed on Jan 22, 2017practicalswift commented at 9:24 AM on January 22, 2017: contributor@MarcoFalke Good point! Updated, squashed and pushed - looks good? :-)
jtimon commented at 12:07 AM on February 1, 2017: contributorutACK e91074b
laanwj commented at 9:07 AM on February 2, 2017: memberWe shouldn't be using an assertion for error handling.
When can this happen? What should be the result (instead of dividing by zero)?
laanwj commented at 9:08 AM on February 2, 2017: memberOkay, just re-read the post above, so this can never happen. That wasn't clear to me from the code change: please add a comment specifying that.
Assert that what might look like a possible division by zero is actually unreachable db07f91899practicalswift force-pushed on Feb 2, 2017practicalswift commented at 9:46 AM on February 2, 2017: contributor@laanwj Good point! Updated and pushed. Looks good? :-)
practicalswift commented at 10:19 PM on February 27, 2017: contributorAny changes needed before merge? :-)
laanwj commented at 11:54 AM on February 28, 2017: memberI think you should change the pull title first - it is incorrect to say that this is a potential division by zero, because it's avoided by previous code.
practicalswift renamed this:Avoid potential division by zero in benchmark::State::KeepRunning()
Assert that what might look like a possible division by zero is actually unreachable
on Feb 28, 2017practicalswift commented at 12:28 PM on February 28, 2017: contributor@laanwj Changed. Looks good? :-)
MarcoFalke renamed this:Assert that what might look like a possible division by zero is actually unreachable
bench: Assert that division by zero is unreachable
on Feb 28, 2017laanwj merged this on Mar 6, 2017laanwj closed this on Mar 6, 2017laanwj referenced this in commit d32581cc29 on Mar 6, 2017PastaPastaPasta referenced this in commit 39a55851a4 on Dec 30, 2018PastaPastaPasta referenced this in commit 0d9532d945 on Dec 31, 2018PastaPastaPasta referenced this in commit b11c70ca1b on Dec 31, 2018PastaPastaPasta referenced this in commit c070945512 on Dec 31, 2018PastaPastaPasta referenced this in commit f185b6187c on Jan 2, 2019PastaPastaPasta referenced this in commit 6f1882e077 on Jan 2, 2019PastaPastaPasta referenced this in commit 2b3b710699 on Jan 3, 2019PastaPastaPasta referenced this in commit d5e90b7723 on Jan 21, 2019PastaPastaPasta referenced this in commit 4bcd83ffe8 on Jan 25, 2019PastaPastaPasta referenced this in commit f23ea2307c on Jan 29, 2019PastaPastaPasta referenced this in commit 9ed9b73a5e on Feb 1, 2019PastaPastaPasta referenced this in commit 56d01657c1 on Feb 1, 2019PastaPastaPasta referenced this in commit 99739e72ae on Feb 1, 2019PastaPastaPasta referenced this in commit a61b747a29 on Feb 1, 2019zkbot referenced this in commit aa225ebb0b on Jan 24, 2020zkbot referenced this in commit 74ff73abab on Jan 24, 2020furszy referenced this in commit 4ed15cc69d on Jun 8, 2020practicalswift deleted the branch on Apr 10, 2021DrahtBot locked this on Aug 16, 2022
This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-15 18:15 UTC
More mirrored repositories can be found on mirror.b10c.me