From b62d4bfe78930132ce649852f9fef5df72f17de6 Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 6 Mar 2023 21:29:04 -0500 Subject: [PATCH] update vector and fix profile memory usage --- include/blt/math/vectors.h | 49 ++++++++++++++++++-------------- include/blt/profiling/profiler.h | 13 +++++---- src/blt/profiling/profiler.cpp | 49 +++++++++++++------------------- src/tests/main.cpp | 12 ++++++++ 4 files changed, 66 insertions(+), 57 deletions(-) diff --git a/include/blt/math/vectors.h b/include/blt/math/vectors.h index ba85613..507ae68 100644 --- a/include/blt/math/vectors.h +++ b/include/blt/math/vectors.h @@ -37,13 +37,24 @@ namespace blt { vec(const vec& 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& operator=(T f) { + inline vec& operator=(T v) { for (int i = 0; i < size; i++) - elements[i] = f; - return *this; - } - - inline vec& operator=(int i) { - for (int _ = 0; _ < size; _++) - elements[_] = i; + elements[i] = v; return *this; } @@ -121,7 +126,7 @@ namespace blt { template inline vec operator+(const vec& left, const vec& right) { - float initializer[size]; + T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] + right[i]; return vec{initializer}; @@ -129,7 +134,7 @@ namespace blt { template inline vec operator-(const vec& left, const vec& right) { - float initializer[size]; + T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] - right[i]; return vec{initializer}; @@ -137,7 +142,7 @@ namespace blt { template inline vec operator+(const vec& left, float f) { - float initializer[size]; + T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] + f; return vec{initializer}; @@ -145,7 +150,7 @@ namespace blt { template inline vec operator-(const vec& left, float f) { - float initializer[size]; + T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] + f; return vec{initializer}; @@ -153,7 +158,7 @@ namespace blt { template inline vec operator+(float f, const vec& right) { - float initializer[size]; + T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = f + right[i]; return vec{initializer}; @@ -161,7 +166,7 @@ namespace blt { template inline vec operator-(float f, const vec& right) { - float initializer[size]; + T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = f - right[i]; return vec{initializer}; @@ -169,7 +174,7 @@ namespace blt { template inline vec operator*(const vec& left, const vec& right) { - float initializer[size]; + T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] * right[i]; return vec{initializer}; @@ -177,7 +182,7 @@ namespace blt { template inline vec operator*(const vec& left, float f) { - float initializer[size]; + T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] * f; return vec{initializer}; @@ -185,7 +190,7 @@ namespace blt { template inline vec operator*(float f, const vec& right) { - float initializer[size]; + T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = f * right[i]; return vec{initializer}; @@ -193,7 +198,7 @@ namespace blt { template inline vec operator/(const vec& left, float f) { - float initializer[size]; + T initializer[size]; for (int i = 0; i < size; i++) initializer[i] = left[i] / f; return vec{initializer}; diff --git a/include/blt/profiling/profiler.h b/include/blt/profiling/profiler.h index 6ef9005..59df91d 100644 --- a/include/blt/profiling/profiler.h +++ b/include/blt/profiling/profiler.h @@ -13,6 +13,8 @@ #include #include #include +#include + /** * 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 intervals; - std::unordered_map> historicalIntervals; + std::unordered_map intervals_total; blt::flat_queue points; }; @@ -43,10 +50,6 @@ namespace blt::profiling { capture_interval getInterval(const std::string& profileName, const std::string& intervalName); - std::vector getAllIntervals( - const std::string& profileName, const std::string& intervalName - ); - profile getProfile(const std::string& profileName); void printProfile( diff --git a/src/blt/profiling/profiler.cpp b/src/blt/profiling/profiler.cpp index 8a7e83f..a21bdbf 100644 --- a/src/blt/profiling/profiler.cpp +++ b/src/blt/profiling/profiler.cpp @@ -53,42 +53,31 @@ namespace blt::profiling { } inline void averageIntervals( - const std::unordered_map>& intervals, + const std::unordered_map& intervals, std::unordered_map& 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 order_rows; std::unordered_map 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 ordered_rows; std::unordered_map 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 getAllIntervals( - const std::string& profileName, const std::string& intervalName - ) { - return profiles[profileName].historicalIntervals[intervalName]; - } - } \ No newline at end of file diff --git a/src/tests/main.cpp b/src/tests/main.cpp index d4d0567..7936dc7 100644 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -5,8 +5,20 @@ #include "profiling_tests.h" #include "nbt_tests.h" #include "queue_tests.h" +#include int main() { + + int data = 0; + int index = 2; + data = data | index << (32 - 2); + auto fdata = *reinterpret_cast(&data); + + auto back_to_int = *reinterpret_cast(&fdata); + + std::string bits = std::bitset<32>((unsigned long long) back_to_int).to_string(); + std::cout << bits << "\n"; + return 0; binaryTreeTest(); run_logging();