Browse Source

DebugTools: remove redundant internal state in FrameProfiler.

Imagine spending 3 hours on recursive off-by-one and overflow errors.
Wonderful.
pull/442/head
Vladimír Vondruš 6 years ago
parent
commit
d471a7bebd
  1. 22
      src/Magnum/DebugTools/FrameProfiler.cpp
  2. 2
      src/Magnum/DebugTools/FrameProfiler.h

22
src/Magnum/DebugTools/FrameProfiler.cpp

@ -68,7 +68,6 @@ FrameProfiler::FrameProfiler(FrameProfiler&& other) noexcept:
#ifndef CORRADE_NO_ASSERT #ifndef CORRADE_NO_ASSERT
_beginFrameCalled{other._beginFrameCalled}, _beginFrameCalled{other._beginFrameCalled},
#endif #endif
_currentData{other._currentData},
_maxFrameCount{other._maxFrameCount}, _maxFrameCount{other._maxFrameCount},
_measuredFrameCount{other._measuredFrameCount}, _measuredFrameCount{other._measuredFrameCount},
_measurements{std::move(other._measurements)}, _measurements{std::move(other._measurements)},
@ -86,7 +85,6 @@ FrameProfiler& FrameProfiler::operator=(FrameProfiler&& other) noexcept {
#ifndef CORRADE_NO_ASSERT #ifndef CORRADE_NO_ASSERT
swap(_beginFrameCalled, other._beginFrameCalled); swap(_beginFrameCalled, other._beginFrameCalled);
#endif #endif
swap(_currentData, other._currentData);
swap(_maxFrameCount, other._maxFrameCount); swap(_maxFrameCount, other._maxFrameCount);
swap(_measuredFrameCount, other._measuredFrameCount); swap(_measuredFrameCount, other._measuredFrameCount);
swap(_measurements, other._measurements); swap(_measurements, other._measurements);
@ -136,7 +134,6 @@ void FrameProfiler::enable() {
#ifndef CORRADE_NO_ASSERT #ifndef CORRADE_NO_ASSERT
_beginFrameCalled = false; _beginFrameCalled = false;
#endif #endif
_currentData = 0;
_measuredFrameCount = 0; _measuredFrameCount = 0;
arrayResize(_data, 0); arrayResize(_data, 0);
@ -170,18 +167,9 @@ void FrameProfiler::beginFrame() {
} }
} }
/* For delay = 1 returns _currentData */
UnsignedInt FrameProfiler::delayedCurrentData(UnsignedInt delay) const { UnsignedInt FrameProfiler::delayedCurrentData(UnsignedInt delay) const {
CORRADE_INTERNAL_ASSERT(delay >= 1); CORRADE_INTERNAL_ASSERT(delay >= 1);
return (_measuredFrameCount - delay) % _maxFrameCount;
/* The delayed frame is current or before current */
if(_currentData >= delay - 1)
return _currentData - delay + 1;
/* If we have all data, wrap around. If we don't have all data yet, such
value doesn't exist and thus this will return an OOB index. If
everything is implemented correctly, it won't be accessed in any way. */
return _maxFrameCount + _currentData - delay + 1;
} }
void FrameProfiler::endFrame() { void FrameProfiler::endFrame() {
@ -193,10 +181,8 @@ void FrameProfiler::endFrame() {
#endif #endif
/* If we don't have all frames yet, enlarge the array */ /* If we don't have all frames yet, enlarge the array */
if(++_measuredFrameCount <= _maxFrameCount) { if(++_measuredFrameCount <= _maxFrameCount)
CORRADE_INTERNAL_ASSERT(_measurements.empty() || _currentData == _data.size()/_measurements.size());
arrayAppend(_data, Containers::NoInit, _measurements.size()); arrayAppend(_data, Containers::NoInit, _measurements.size());
}
/* Wrap up measurements for this frame */ /* Wrap up measurements for this frame */
for(std::size_t i = 0; i != _measurements.size(); ++i) { for(std::size_t i = 0; i != _measurements.size(); ++i) {
@ -247,10 +233,6 @@ void FrameProfiler::endFrame() {
if(_measuredFrameCount >= measurementDelay) if(_measuredFrameCount >= measurementDelay)
_measurements[i]._movingSum += _data[delayedCurrentData(measurementDelay)*_measurements.size() + i]; _measurements[i]._movingSum += _data[delayedCurrentData(measurementDelay)*_measurements.size() + i];
} }
/* Advance & wraparound the index where data will be saved for the next
frame */
_currentData = (_currentData + 1) % _maxFrameCount;
} }
std::string FrameProfiler::measurementName(const UnsignedInt id) const { std::string FrameProfiler::measurementName(const UnsignedInt id) const {

2
src/Magnum/DebugTools/FrameProfiler.h

@ -389,7 +389,7 @@ class MAGNUM_DEBUGTOOLS_EXPORT FrameProfiler {
asserts get disabled */ asserts get disabled */
bool _beginFrameCalled{}; bool _beginFrameCalled{};
#endif #endif
UnsignedInt _currentData{}, _maxFrameCount{1}, _measuredFrameCount{}; UnsignedInt _maxFrameCount{1}, _measuredFrameCount{};
Containers::Array<Measurement> _measurements; Containers::Array<Measurement> _measurements;
Containers::Array<UnsignedLong> _data; Containers::Array<UnsignedLong> _data;
}; };

Loading…
Cancel
Save