allocation-less history

This commit is contained in:
Patrick Nicolas
2025-02-06 20:05:21 +01:00
parent d1c2c24473
commit 39d5e6b87b
+35 -72
View File
@@ -21,17 +21,15 @@
#include "clock_offset.h" #include "clock_offset.h"
#include "os/os_time.h" #include "os/os_time.h"
#include "util/u_logging.h"
#include <algorithm> #include <algorithm>
#include <cstddef> #include <cstddef>
#include <list>
#include <mutex> #include <mutex>
#include <openxr/openxr.h> #include <openxr/openxr.h>
namespace wivrn namespace wivrn
{ {
template <typename Derived, typename Data, XrDuration extrapolation = 0, size_t MaxSamples = 10> template <typename Derived, typename Data, size_t MaxSamples = 10>
class history class history
{ {
struct TimedData : public Data struct TimedData : public Data
@@ -39,9 +37,9 @@ class history
XrTime produced_timestamp; XrTime produced_timestamp;
XrTime at_timestamp_ns; XrTime at_timestamp_ns;
}; };
std::array<TimedData, MaxSamples> data{};
std::mutex mutex; std::mutex mutex;
std::list<TimedData> data;
XrTime last_request; XrTime last_request;
XrTime last_produced; XrTime last_produced;
@@ -59,43 +57,26 @@ protected:
bool active = produced - last_request < 1'000'000'000; bool active = produced - last_request < 1'000'000'000;
last_produced = std::max(produced, last_produced); last_produced = std::max(produced, last_produced);
// Discard outdated data, packets could be reordered TimedData * target = data.data();
if (not data.empty()) if (offset)
{ {
// keep only one sample if the clock_offset is unreliable for (auto & item: data)
if (not offset)
{ {
U_LOG_T("not using history: clock_offset not stable"); // Discard reordered packets
data.clear(); if (item.produced_timestamp > produced)
data.emplace_back(sample, produced, t);
return active; return active;
}
if (data.back().produced_timestamp > produced) if (std::abs(item.at_timestamp_ns - t) < 2'000'000)
return active;
}
// Discard outdated predictions
for (auto it = data.begin(); it != data.end();)
{ {
if (std::abs(it->at_timestamp_ns - t) < 2'000'000 and it->produced_timestamp < produced) target = &item;
it = data.erase(it); break;
else
++it;
} }
// Insert the new sample if (item.at_timestamp_ns < target->at_timestamp_ns)
auto it = std::lower_bound(data.begin(), data.end(), t, [](TimedData & sample, int64_t t) { return sample.at_timestamp_ns < t; }); target = &item;
if (it == data.end()) }
data.emplace_back(sample, produced, t); }
else if (it->at_timestamp_ns == t) *target = TimedData(sample, produced, t);
*it = TimedData(sample, produced, t);
else
data.emplace(it, sample, produced, t);
while (data.size() > MaxSamples)
data.pop_front();
return active; return active;
} }
@@ -107,57 +88,39 @@ public:
last_request = os_monotonic_get_ns(); last_request = os_monotonic_get_ns();
if (data.empty()) TimedData * before = nullptr;
{ TimedData * after = nullptr;
return {};
}
if (at_timestamp_ns - data.back().at_timestamp_ns > 1'000'000'000) for (auto & item: data)
{ {
// stale data if (not item.at_timestamp_ns)
data.clear(); continue;
return {}; if (item.at_timestamp_ns < at_timestamp_ns)
}
if (data.size() == 1)
{ {
return {ex, data.front()}; if (not before or before->at_timestamp_ns < item.at_timestamp_ns)
} before = &item;
if (data.front().at_timestamp_ns > at_timestamp_ns)
{
if constexpr (extrapolation)
{
auto second = data.begin();
auto first = second++;
at_timestamp_ns = std::max(at_timestamp_ns, data.front().at_timestamp_ns - extrapolation);
return {ex, Derived::extrapolate(*first, *second, first->at_timestamp_ns, second->at_timestamp_ns, at_timestamp_ns)};
} }
else else
return {ex, data.front()}; {
if (not after or after->at_timestamp_ns > item.at_timestamp_ns)
after = &item;
}
} }
for (auto after = data.begin(), before = after++; after != data.end(); before = after++) if (before and after)
{
if (after->at_timestamp_ns > at_timestamp_ns)
{ {
float t = float(after->at_timestamp_ns - at_timestamp_ns) / float t = float(after->at_timestamp_ns - at_timestamp_ns) /
(after->at_timestamp_ns - before->at_timestamp_ns); (after->at_timestamp_ns - before->at_timestamp_ns);
return {ex, Derived::interpolate(*before, *after, t)}; return {ex, Derived::interpolate(*before, *after, t)};
} }
}
if constexpr (extrapolation) if (before)
{ return {ex, *before};
auto prev = data.rbegin();
auto last = prev++; if (after)
at_timestamp_ns = std::min(at_timestamp_ns, data.back().at_timestamp_ns + extrapolation); return {ex, *after};
return {ex, Derived::extrapolate(*prev, *last, prev->at_timestamp_ns, last->at_timestamp_ns, at_timestamp_ns)};
} return {};
else
{
return {ex, data.back()};
}
} }
}; };
} // namespace wivrn } // namespace wivrn