Use QLinkedList instead of a static array.
Use QElapsedTimer instead of QDateTime for elapsed time.
Details
- Reviewers
dfaure fvogt - Commits
- R241:4e2a815b9a10: Refactor SlaveInterface::calcSpeed
Copied several large files(5-20Gb). The difference between current and previous speed calculation was within 0-300Kb.
Diff Detail
- Repository
- R241 KIO
- Branch
- master
- Lint
No Linters Available - Unit
No Unit Test Coverage - Build Status
Buildable 6018 Build 6036: arc lint + arc unit
I'm surprised, how can a QLinkedList (with nodes allocated all over the memory) be better than a static array (which fits into the same memory cache) ?
IMO It is better only in terms of how the code looks. I saw the for loop shifting elements and immediately felt the urge to use QLinkedList. I never thought of the memory allocation.
src/core/slaveinterface_p.h | ||
---|---|---|
59 ↗ | (On Diff #47518) | You could use a struct for both, e.g.: struct SpeedSample { KIO::filesize_t remainder; qint64 elapsedTime; } QVector<SpeedSample> transferSpeed; This would emphasize the two values always come in pairs, and would even remove some code above. |
src/core/slaveinterface.cpp | ||
---|---|---|
112 | why not just call "last" the (currently unnamed) TransferInfo created 2 lines above, rather than extracting it out of the vector just after appending? | |
src/core/slaveinterface_p.h | ||
39 | global namespace pollution, better keep this within KIO::SlaveInterfacePrivate. | |
48 | the old "nums" is now the vector size, right? nums was initialized to 0, so this should not initialize the vector to max_count items |
src/core/slaveinterface.cpp | ||
---|---|---|
102 | .. at most ... | |
203 | Strange way of writing {0, 0} ... | |
src/core/slaveinterface_p.h | ||
48 | The result is the same in this case - after 7 seconds, the initial items are shifted out of the array. During these 7 seconds, the first element is always {0, 0}. It does not matter if last() is the 2nd or 7th element. |
Removed vector initialization.
Increased vector capacity in constructor.
Appended {0,0} at first.
Stored time and size before appending to prevent extraction.
endless refactoring, for the better :-)
src/core/slaveinterface.cpp | ||
---|---|---|
117 ↗ | (On Diff #47518) | .append(last) would do the same in a more compact way. ... and then this means we could reduce code size even further by calculating lspeed *before* doing the append. first = ... last = ... lspeed = ... if (!lspeed) { d->transfer_details.clear(); } d->transfer_details.append(last); |
We're getting some SIGFPE crash reports in this area: Bug 402665
I failed to reproduce them, though. Best is probably to revert this patch because Frameworks release is imminent.
src/core/slaveinterface.cpp | ||
---|---|---|
117 ↗ | (On Diff #47518) | We likely get a division by zero here for some reason |
src/core/slaveinterface.cpp | ||
---|---|---|
114 ↗ | (On Diff #47518) | That wasn't there in the old code because it has this guard: const qint64 diff = currentTime - d->start_time; (effectively calculating elapsed) Then if (diff - d->last_time >= 900) { So we known first-last is non-zero Should the new code be https://phabricator.kde.org/P287 ? It seemed to work for me. |
src/core/slaveinterface.cpp | ||
---|---|---|
114 ↗ | (On Diff #47518) | Indeed that should be the code here. |
117 ↗ | (On Diff #47518) | I doubt it will be zero here. elapsed_time stores elapsed time since the timer started so it will continue to grow (?) and the difference will always be > 0. |
src/core/slaveinterface.cpp | ||
---|---|---|
111 ↗ | (On Diff #47518) | This comment should refer to max_count instead of hardcoding the number. |
115 ↗ | (On Diff #47518) | This is undefined behaviour if the vector is empty. |
115–116 ↗ | (On Diff #47518) | What does this actually test for? |
117 ↗ | (On Diff #47518) | Doubt is not enough - it is absolutely necessary to special case that. A div / 0 is an instant crash on x86_64. |
src/core/slaveinterface_p.h | ||
39 | I absolutely agree here. There is no reason to have this as a global variable, especially not a "static" one in a header. Please move it into the class. | |
62 ↗ | (On Diff #47518) | Is there any reason to use a qvector instead of a std::array/c array? |
src/core/slaveinterface.cpp | ||
---|---|---|
115–116 ↗ | (On Diff #47518) | This results in a fast ramp up when the transfer has stalled for more than ~8 seconds, like at the begin of a transfer. Likely needs a comment. |
src/core/slaveinterface_p.h | ||
62 ↗ | (On Diff #47518) | std::array can not be resized. At first, there is only one element. This requirement could be avoided around by filling the the array with {0,0} elements, which results in identical behavior. We would also need a method for shifting the entries. (.removeFirst(), append()). |
src/core/slaveinterface.cpp | ||
---|---|---|
115 ↗ | (On Diff #47518) | Initial item is set to {0,0} before calcSpeed is called. |