Merge pull request 'perf(window): ring buffer for CRAFTER_TIMING frame times (#44)' (#76) from claude/issue-44 into master

This commit is contained in:
catbot 2026-06-16 17:25:58 +02:00
commit 626cc6f5c8
2 changed files with 26 additions and 17 deletions

View file

@ -657,11 +657,12 @@ void Window::Update() {
#ifdef CRAFTER_TIMING
frameEnd = std::chrono::high_resolution_clock::now();
frameTimes.push_back(totalUpdate+totalRender);
// Keep only the last 100 frame times
if (frameTimes.size() > 100) {
frameTimes.erase(frameTimes.begin());
// Ring buffer: overwrite the oldest entry once full. Order doesn't matter
// to LogTiming, so we never need to shift elements.
frameTimes[frameTimesHead] = totalUpdate+totalRender;
frameTimesHead = (frameTimesHead + 1) % frameTimeCapacity;
if (frameTimesCount < frameTimeCapacity) {
++frameTimesCount;
}
#endif
lastFrameBegin = startTime;
@ -843,21 +844,22 @@ void Window::LogTiming() {
std::cout << std::format("Total: {}", duration_cast<std::chrono::milliseconds>(totalUpdate+totalRender)) << std::endl;
std::cout << std::format("Vblank: {}", duration_cast<std::chrono::milliseconds>(vblank)) << std::endl;
// Add 100-frame average and min-max timing info
if (!frameTimes.empty()) {
// Add 100-frame average and min-max timing info. Only the first
// frameTimesCount entries of the ring buffer hold valid samples.
if (frameTimesCount != 0) {
// Calculate average
std::chrono::nanoseconds sum(0);
for (const auto& frameTime : frameTimes) {
sum += frameTime;
for (std::size_t i = 0; i < frameTimesCount; ++i) {
sum += frameTimes[i];
}
auto average = sum / frameTimes.size();
auto average = sum / frameTimesCount;
// Find min and max
auto min = frameTimes.front();
auto max = frameTimes.front();
for (const auto& frameTime : frameTimes) {
if (frameTime < min) min = frameTime;
if (frameTime > max) max = frameTime;
auto min = frameTimes[0];
auto max = frameTimes[0];
for (std::size_t i = 0; i < frameTimesCount; ++i) {
if (frameTimes[i] < min) min = frameTimes[i];
if (frameTimes[i] > max) max = frameTimes[i];
}
std::cout << std::format("Last 100 Frame Times - Avg: {}, Min: {}, Max: {}",

View file

@ -164,7 +164,14 @@ export namespace Crafter {
std::chrono::nanoseconds vblank;
std::chrono::nanoseconds totalFrame;
std::chrono::time_point<std::chrono::high_resolution_clock> frameEnd;
std::vector<std::chrono::nanoseconds> frameTimes;
// Fixed-size ring buffer of the most recent frame times. LogTiming does
// order-independent sum/avg/min/max, so head position is irrelevant to
// the reported stats; this avoids the per-frame memmove a vector::erase
// at the front would incur once full.
static constexpr std::size_t frameTimeCapacity = 100;
std::array<std::chrono::nanoseconds, frameTimeCapacity> frameTimes{};
std::size_t frameTimesHead = 0;
std::size_t frameTimesCount = 0;
void LogTiming();
#endif