update vector and fix profile memory usage

v1
Brett 2023-03-06 21:29:04 -05:00
parent d50e13dcb6
commit b62d4bfe78
4 changed files with 66 additions and 57 deletions

View File

@ -37,13 +37,24 @@ namespace blt {
vec(const vec<T, size>& copy): vec(copy.elements) {}
[[nodiscard]] inline T x() const { return elements[0]; }
[[nodiscard]] inline T x() const {
return elements[0];
}
[[nodiscard]] inline T y() const { return elements[1]; }
[[nodiscard]] inline T y() const {
static_assert(size > 1);
return elements[1];
}
[[nodiscard]] inline T z() const { return elements[2]; }
[[nodiscard]] inline T z() const {
static_assert(size > 2);
return elements[2];
}
[[nodiscard]] inline T w() const { return elements[3]; }
[[nodiscard]] inline T w() const {
static_assert(size > 3);
return elements[3];
}
inline T& operator[](int index) {
return elements[index];
@ -53,15 +64,9 @@ namespace blt {
return elements[index];
}
inline vec<T, size>& operator=(T f) {
inline vec<T, size>& operator=(T v) {
for (int i = 0; i < size; i++)
elements[i] = f;
return *this;
}
inline vec<T, size>& operator=(int i) {
for (int _ = 0; _ < size; _++)
elements[_] = i;
elements[i] = v;
return *this;
}
@ -121,7 +126,7 @@ namespace blt {
template<typename T, unsigned long size>
inline vec<T, size> operator+(const vec<T, size>& left, const vec<T, size>& right) {
float initializer[size];
T initializer[size];
for (int i = 0; i < size; i++)
initializer[i] = left[i] + right[i];
return vec<T, size>{initializer};
@ -129,7 +134,7 @@ namespace blt {
template<typename T, unsigned long size>
inline vec<T, size> operator-(const vec<T, size>& left, const vec<T, size>& right) {
float initializer[size];
T initializer[size];
for (int i = 0; i < size; i++)
initializer[i] = left[i] - right[i];
return vec<T, size>{initializer};
@ -137,7 +142,7 @@ namespace blt {
template<typename T, unsigned long size>
inline vec<T, size> operator+(const vec<T, size>& left, float f) {
float initializer[size];
T initializer[size];
for (int i = 0; i < size; i++)
initializer[i] = left[i] + f;
return vec<T, size>{initializer};
@ -145,7 +150,7 @@ namespace blt {
template<typename T, unsigned long size>
inline vec<T, size> operator-(const vec<T, size>& left, float f) {
float initializer[size];
T initializer[size];
for (int i = 0; i < size; i++)
initializer[i] = left[i] + f;
return vec<T, size>{initializer};
@ -153,7 +158,7 @@ namespace blt {
template<typename T, unsigned long size>
inline vec<T, size> operator+(float f, const vec<T, size>& right) {
float initializer[size];
T initializer[size];
for (int i = 0; i < size; i++)
initializer[i] = f + right[i];
return vec<T, size>{initializer};
@ -161,7 +166,7 @@ namespace blt {
template<typename T, unsigned long size>
inline vec<T, size> operator-(float f, const vec<T, size>& right) {
float initializer[size];
T initializer[size];
for (int i = 0; i < size; i++)
initializer[i] = f - right[i];
return vec<T, size>{initializer};
@ -169,7 +174,7 @@ namespace blt {
template<typename T, unsigned long size>
inline vec<T, size> operator*(const vec<T, size>& left, const vec<T, size>& right) {
float initializer[size];
T initializer[size];
for (int i = 0; i < size; i++)
initializer[i] = left[i] * right[i];
return vec<T, size>{initializer};
@ -177,7 +182,7 @@ namespace blt {
template<typename T, unsigned long size>
inline vec<T, size> operator*(const vec<T, size>& left, float f) {
float initializer[size];
T initializer[size];
for (int i = 0; i < size; i++)
initializer[i] = left[i] * f;
return vec<T, size>{initializer};
@ -185,7 +190,7 @@ namespace blt {
template<typename T, unsigned long size>
inline vec<T, size> operator*(float f, const vec<T, size>& right) {
float initializer[size];
T initializer[size];
for (int i = 0; i < size; i++)
initializer[i] = f * right[i];
return vec<T, size>{initializer};
@ -193,7 +198,7 @@ namespace blt {
template<typename T, unsigned long size>
inline vec<T, size> operator/(const vec<T, size>& left, float f) {
float initializer[size];
T initializer[size];
for (int i = 0; i < size; i++)
initializer[i] = left[i] / f;
return vec<T, size>{initializer};

View File

@ -13,6 +13,8 @@
#include <unordered_map>
#include <blt/std/logging.h>
#include <fstream>
#include <cstdint>
/**
* Defines several disableable macros (#define BLT_DISABLE_PROFILING). If you do not use these macros profiling cannot be disabled!
@ -29,9 +31,14 @@ namespace blt::profiling {
long end;
};
struct capture_history {
std::uint64_t count;
std::uint64_t total;
};
struct profile {
std::unordered_map<std::string, capture_interval> intervals;
std::unordered_map<std::string, std::vector<capture_interval>> historicalIntervals;
std::unordered_map<std::string, capture_history> intervals_total;
blt::flat_queue<capture_point> points;
};
@ -43,10 +50,6 @@ namespace blt::profiling {
capture_interval getInterval(const std::string& profileName, const std::string& intervalName);
std::vector<capture_interval> getAllIntervals(
const std::string& profileName, const std::string& intervalName
);
profile getProfile(const std::string& profileName);
void printProfile(

View File

@ -53,42 +53,31 @@ namespace blt::profiling {
}
inline void averageIntervals(
const std::unordered_map<std::string, std::vector<capture_interval>>& intervals,
const std::unordered_map<std::string, capture_history>& intervals,
std::unordered_map<std::string, capture_interval>& averagedIntervals
) {
// average all intervals
for (const auto& i : intervals) {
const auto& name = i.first;
const auto& interval_vec = i.second;
long total_difference = 0;
// calculate total difference
for (const auto& value : interval_vec) {
auto difference = value.end - value.start;
// negatives make no sense. remove them to prevent errors
if (difference > 0)
total_difference += difference;
}
total_difference /= (long) interval_vec.size();
// we can exploit how the order func works by supplying the difference into end,
// which sorts correctly despite not being a true interval.
averagedIntervals.insert({name, capture_interval{0, total_difference}});
averagedIntervals.insert({name, capture_interval{0, (long)(interval_vec.total / interval_vec.count)}});
}
}
void writeProfile(std::ofstream& out, const std::string& profileName, bool averageHistory) {
auto& profile = profiles[profileName];
const auto& intervals = profile.intervals;
const auto& intervals_hist = profile.historicalIntervals;
const auto& intervals_total = profile.intervals_total;
const auto& points = profile.points;
std::vector<IntervalComparable> order_rows;
std::unordered_map<std::string, capture_interval> averaged_intervals;
if (averageHistory)
averageIntervals(profile.historicalIntervals, averaged_intervals);
averageIntervals(intervals_total, averaged_intervals);
orderIntervals(averageHistory ? averaged_intervals : intervals, order_rows);
@ -96,7 +85,7 @@ namespace blt::profiling {
int index = 1;
for (const auto& row : order_rows) {
out << std::to_string(index++) << ","
<< std::to_string(averageHistory ? intervals_hist.at(row.name).size() : 1) << ","
<< std::to_string(averageHistory ? intervals_total.at(row.name).count : 1) << ","
<< row.name << ","
<< std::to_string((double) row.difference / 1000000.0) << ","
<< std::to_string(row.difference) << "\n";
@ -109,14 +98,14 @@ namespace blt::profiling {
) {
auto& profile = profiles[profileName];
const auto& intervals = profile.intervals;
const auto& intervals_hist = profile.historicalIntervals;
const auto& intervals_total = profile.intervals_total;
const auto& points = profile.points;
std::vector<IntervalComparable> ordered_rows;
std::unordered_map<std::string, capture_interval> averaged_intervals;
if (averageHistory)
averageIntervals(profile.historicalIntervals, averaged_intervals);
averageIntervals(intervals_total, averaged_intervals);
orderIntervals(averageHistory ? averaged_intervals : intervals, ordered_rows);
@ -131,7 +120,7 @@ namespace blt::profiling {
for (const auto& row : ordered_rows) {
formatter.addRow(
{std::to_string(index++),
std::to_string(averageHistory ? intervals_hist.at(row.name).size() : 1),
std::to_string(averageHistory ? intervals_total.at(row.name).count : 1),
row.name,
std::to_string((double) row.difference / 1000000.0),
std::to_string(row.difference)}
@ -152,10 +141,16 @@ namespace blt::profiling {
void endInterval(const std::string& profileName, const std::string& intervalName) {
std::scoped_lock lock(profileLock);
profiles[profileName].intervals[intervalName].end = system::getCurrentTimeNanoseconds();
profiles[profileName].historicalIntervals[intervalName].push_back(
profiles[profileName].intervals[intervalName]
);
auto& prof = profiles[profileName];
auto& ref = prof.intervals[intervalName];
ref.end = system::getCurrentTimeNanoseconds();
auto difference = ref.end - ref.start;
auto& href = prof.intervals_total[intervalName];
href.total += difference;
href.count++;
}
void point(const std::string& profileName, const std::string& pointName) {
@ -180,17 +175,11 @@ namespace blt::profiling {
void discardIntervals(const std::string& profileName) {
profiles[profileName].intervals = {};
profiles[profileName].historicalIntervals = {};
profiles[profileName].intervals_total = {};
}
void discardPoints(const std::string& profileName) {
profiles[profileName].points = {};
}
std::vector<capture_interval> getAllIntervals(
const std::string& profileName, const std::string& intervalName
) {
return profiles[profileName].historicalIntervals[intervalName];
}
}

View File

@ -5,8 +5,20 @@
#include "profiling_tests.h"
#include "nbt_tests.h"
#include "queue_tests.h"
#include <bitset>
int main() {
int data = 0;
int index = 2;
data = data | index << (32 - 2);
auto fdata = *reinterpret_cast<float*>(&data);
auto back_to_int = *reinterpret_cast<int*>(&fdata);
std::string bits = std::bitset<32>((unsigned long long) back_to_int).to_string();
std::cout << bits << "\n";
return 0;
binaryTreeTest();
run_logging();