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) {} 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) { inline T& operator[](int index) {
return elements[index]; return elements[index];
@ -53,15 +64,9 @@ namespace blt {
return elements[index]; return elements[index];
} }
inline vec<T, size>& operator=(T f) { inline vec<T, size>& operator=(T v) {
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
elements[i] = f; elements[i] = v;
return *this;
}
inline vec<T, size>& operator=(int i) {
for (int _ = 0; _ < size; _++)
elements[_] = i;
return *this; return *this;
} }
@ -121,7 +126,7 @@ namespace blt {
template<typename T, unsigned long size> template<typename T, unsigned long size>
inline vec<T, size> operator+(const vec<T, size>& left, const vec<T, size>& right) { 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++) for (int i = 0; i < size; i++)
initializer[i] = left[i] + right[i]; initializer[i] = left[i] + right[i];
return vec<T, size>{initializer}; return vec<T, size>{initializer};
@ -129,7 +134,7 @@ namespace blt {
template<typename T, unsigned long size> template<typename T, unsigned long size>
inline vec<T, size> operator-(const vec<T, size>& left, const vec<T, size>& right) { 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++) for (int i = 0; i < size; i++)
initializer[i] = left[i] - right[i]; initializer[i] = left[i] - right[i];
return vec<T, size>{initializer}; return vec<T, size>{initializer};
@ -137,7 +142,7 @@ namespace blt {
template<typename T, unsigned long size> template<typename T, unsigned long size>
inline vec<T, size> operator+(const vec<T, size>& left, float f) { 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++) for (int i = 0; i < size; i++)
initializer[i] = left[i] + f; initializer[i] = left[i] + f;
return vec<T, size>{initializer}; return vec<T, size>{initializer};
@ -145,7 +150,7 @@ namespace blt {
template<typename T, unsigned long size> template<typename T, unsigned long size>
inline vec<T, size> operator-(const vec<T, size>& left, float f) { 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++) for (int i = 0; i < size; i++)
initializer[i] = left[i] + f; initializer[i] = left[i] + f;
return vec<T, size>{initializer}; return vec<T, size>{initializer};
@ -153,7 +158,7 @@ namespace blt {
template<typename T, unsigned long size> template<typename T, unsigned long size>
inline vec<T, size> operator+(float f, const vec<T, size>& right) { 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++) for (int i = 0; i < size; i++)
initializer[i] = f + right[i]; initializer[i] = f + right[i];
return vec<T, size>{initializer}; return vec<T, size>{initializer};
@ -161,7 +166,7 @@ namespace blt {
template<typename T, unsigned long size> template<typename T, unsigned long size>
inline vec<T, size> operator-(float f, const vec<T, size>& right) { 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++) for (int i = 0; i < size; i++)
initializer[i] = f - right[i]; initializer[i] = f - right[i];
return vec<T, size>{initializer}; return vec<T, size>{initializer};
@ -169,7 +174,7 @@ namespace blt {
template<typename T, unsigned long size> template<typename T, unsigned long size>
inline vec<T, size> operator*(const vec<T, size>& left, const vec<T, size>& right) { 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++) for (int i = 0; i < size; i++)
initializer[i] = left[i] * right[i]; initializer[i] = left[i] * right[i];
return vec<T, size>{initializer}; return vec<T, size>{initializer};
@ -177,7 +182,7 @@ namespace blt {
template<typename T, unsigned long size> template<typename T, unsigned long size>
inline vec<T, size> operator*(const vec<T, size>& left, float f) { 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++) for (int i = 0; i < size; i++)
initializer[i] = left[i] * f; initializer[i] = left[i] * f;
return vec<T, size>{initializer}; return vec<T, size>{initializer};
@ -185,7 +190,7 @@ namespace blt {
template<typename T, unsigned long size> template<typename T, unsigned long size>
inline vec<T, size> operator*(float f, const vec<T, size>& right) { 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++) for (int i = 0; i < size; i++)
initializer[i] = f * right[i]; initializer[i] = f * right[i];
return vec<T, size>{initializer}; return vec<T, size>{initializer};
@ -193,7 +198,7 @@ namespace blt {
template<typename T, unsigned long size> template<typename T, unsigned long size>
inline vec<T, size> operator/(const vec<T, size>& left, float f) { 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++) for (int i = 0; i < size; i++)
initializer[i] = left[i] / f; initializer[i] = left[i] / f;
return vec<T, size>{initializer}; return vec<T, size>{initializer};

View File

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

View File

@ -53,42 +53,31 @@ namespace blt::profiling {
} }
inline void averageIntervals( 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 std::unordered_map<std::string, capture_interval>& averagedIntervals
) { ) {
// average all intervals // average all intervals
for (const auto& i : intervals) { for (const auto& i : intervals) {
const auto& name = i.first; const auto& name = i.first;
const auto& interval_vec = i.second; 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, // we can exploit how the order func works by supplying the difference into end,
// which sorts correctly despite not being a true interval. // 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) { void writeProfile(std::ofstream& out, const std::string& profileName, bool averageHistory) {
auto& profile = profiles[profileName]; auto& profile = profiles[profileName];
const auto& intervals = profile.intervals; const auto& intervals = profile.intervals;
const auto& intervals_hist = profile.historicalIntervals; const auto& intervals_total = profile.intervals_total;
const auto& points = profile.points; const auto& points = profile.points;
std::vector<IntervalComparable> order_rows; std::vector<IntervalComparable> order_rows;
std::unordered_map<std::string, capture_interval> averaged_intervals; std::unordered_map<std::string, capture_interval> averaged_intervals;
if (averageHistory) if (averageHistory)
averageIntervals(profile.historicalIntervals, averaged_intervals); averageIntervals(intervals_total, averaged_intervals);
orderIntervals(averageHistory ? averaged_intervals : intervals, order_rows); orderIntervals(averageHistory ? averaged_intervals : intervals, order_rows);
@ -96,7 +85,7 @@ namespace blt::profiling {
int index = 1; int index = 1;
for (const auto& row : order_rows) { for (const auto& row : order_rows) {
out << std::to_string(index++) << "," 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 << "," << row.name << ","
<< std::to_string((double) row.difference / 1000000.0) << "," << std::to_string((double) row.difference / 1000000.0) << ","
<< std::to_string(row.difference) << "\n"; << std::to_string(row.difference) << "\n";
@ -109,14 +98,14 @@ namespace blt::profiling {
) { ) {
auto& profile = profiles[profileName]; auto& profile = profiles[profileName];
const auto& intervals = profile.intervals; const auto& intervals = profile.intervals;
const auto& intervals_hist = profile.historicalIntervals; const auto& intervals_total = profile.intervals_total;
const auto& points = profile.points; const auto& points = profile.points;
std::vector<IntervalComparable> ordered_rows; std::vector<IntervalComparable> ordered_rows;
std::unordered_map<std::string, capture_interval> averaged_intervals; std::unordered_map<std::string, capture_interval> averaged_intervals;
if (averageHistory) if (averageHistory)
averageIntervals(profile.historicalIntervals, averaged_intervals); averageIntervals(intervals_total, averaged_intervals);
orderIntervals(averageHistory ? averaged_intervals : intervals, ordered_rows); orderIntervals(averageHistory ? averaged_intervals : intervals, ordered_rows);
@ -131,7 +120,7 @@ namespace blt::profiling {
for (const auto& row : ordered_rows) { for (const auto& row : ordered_rows) {
formatter.addRow( formatter.addRow(
{std::to_string(index++), {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, row.name,
std::to_string((double) row.difference / 1000000.0), std::to_string((double) row.difference / 1000000.0),
std::to_string(row.difference)} std::to_string(row.difference)}
@ -152,10 +141,16 @@ namespace blt::profiling {
void endInterval(const std::string& profileName, const std::string& intervalName) { void endInterval(const std::string& profileName, const std::string& intervalName) {
std::scoped_lock lock(profileLock); std::scoped_lock lock(profileLock);
profiles[profileName].intervals[intervalName].end = system::getCurrentTimeNanoseconds(); auto& prof = profiles[profileName];
profiles[profileName].historicalIntervals[intervalName].push_back( auto& ref = prof.intervals[intervalName];
profiles[profileName].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) { void point(const std::string& profileName, const std::string& pointName) {
@ -180,17 +175,11 @@ namespace blt::profiling {
void discardIntervals(const std::string& profileName) { void discardIntervals(const std::string& profileName) {
profiles[profileName].intervals = {}; profiles[profileName].intervals = {};
profiles[profileName].historicalIntervals = {}; profiles[profileName].intervals_total = {};
} }
void discardPoints(const std::string& profileName) { void discardPoints(const std::string& profileName) {
profiles[profileName].points = {}; 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 "profiling_tests.h"
#include "nbt_tests.h" #include "nbt_tests.h"
#include "queue_tests.h" #include "queue_tests.h"
#include <bitset>
int main() { 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(); binaryTreeTest();
run_logging(); run_logging();