Оптимизировал агрегацию на процессоре

This commit is contained in:
2025-12-16 14:16:40 +00:00
parent a5aadbc774
commit e84d1e9fe3

View File

@@ -1,12 +1,17 @@
#include "aggregation.hpp"
#include "utils.hpp"
#include <map>
#include <algorithm>
#include <cstdint>
#include <limits>
#include <vector>
std::vector<PeriodStats> aggregate_periods(const std::vector<Record>& records) {
int64_t interval = get_aggregation_interval();
const int64_t interval = get_aggregation_interval();
std::vector<PeriodStats> result;
if (records.empty()) return result;
struct PeriodAccumulator {
double avg_sum = 0.0;
double open_min = std::numeric_limits<double>::max();
@@ -14,37 +19,57 @@ std::vector<PeriodStats> aggregate_periods(const std::vector<Record>& records) {
double close_min = std::numeric_limits<double>::max();
double close_max = std::numeric_limits<double>::lowest();
int64_t count = 0;
void add(const Record& r) {
const double avg = (r.low + r.high) / 2.0;
avg_sum += avg;
open_min = std::min(open_min, r.open);
open_max = std::max(open_max, r.open);
close_min = std::min(close_min, r.close);
close_max = std::max(close_max, r.close);
++count;
}
};
std::map<PeriodIndex, PeriodAccumulator> periods;
for (const auto& r : records) {
PeriodIndex period = static_cast<PeriodIndex>(r.timestamp) / interval;
auto& acc = periods[period];
double avg = (r.low + r.high) / 2.0;
acc.avg_sum += avg;
acc.open_min = std::min(acc.open_min, r.open);
acc.open_max = std::max(acc.open_max, r.open);
acc.close_min = std::min(acc.close_min, r.close);
acc.close_max = std::max(acc.close_max, r.close);
acc.count++;
PeriodIndex current_period =
static_cast<PeriodIndex>(records[0].timestamp) / interval;
PeriodAccumulator acc;
acc.add(records[0]);
for (size_t i = 1; i < records.size(); ++i) {
const Record& r = records[i];
const PeriodIndex period =
static_cast<PeriodIndex>(r.timestamp) / interval;
if (period != current_period) {
PeriodStats stats;
stats.period = current_period;
stats.avg = acc.avg_sum / static_cast<double>(acc.count);
stats.open_min = acc.open_min;
stats.open_max = acc.open_max;
stats.close_min = acc.close_min;
stats.close_max = acc.close_max;
stats.count = acc.count;
result.push_back(stats);
current_period = period;
acc = PeriodAccumulator{};
}
acc.add(r);
}
std::vector<PeriodStats> result;
result.reserve(periods.size());
for (const auto& [period, acc] : periods) {
PeriodStats stats;
stats.period = period;
stats.avg = acc.avg_sum / static_cast<double>(acc.count);
stats.open_min = acc.open_min;
stats.open_max = acc.open_max;
stats.close_min = acc.close_min;
stats.close_max = acc.close_max;
stats.count = acc.count;
result.push_back(stats);
}
// последний период
PeriodStats stats;
stats.period = current_period;
stats.avg = acc.avg_sum / static_cast<double>(acc.count);
stats.open_min = acc.open_min;
stats.open_max = acc.open_max;
stats.close_min = acc.close_min;
stats.close_max = acc.close_max;
stats.count = acc.count;
result.push_back(stats);
return result;
}