From e152ed2e616d591b7999cb5518c427e891bde7f9 Mon Sep 17 00:00:00 2001 From: Thomas Harte Date: Fri, 21 Jul 2017 18:20:27 -0400 Subject: [PATCH] Made an attempt to avoid GCD costs when accumulating `Time`s with a common clock rate (/divisor). --- Storage/Storage.hpp | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/Storage/Storage.hpp b/Storage/Storage.hpp index d2eb84b36..bb6ccd3aa 100644 --- a/Storage/Storage.hpp +++ b/Storage/Storage.hpp @@ -25,15 +25,13 @@ struct Time { Time() : length(0), clock_rate(1) {} Time(unsigned int unsigned_int_value) : length(unsigned_int_value), clock_rate(1) {} Time(int int_value) : Time((unsigned int)int_value) {} - Time(unsigned int length, unsigned int clock_rate) : length(length), clock_rate(clock_rate) { simplify(); } + Time(unsigned int length, unsigned int clock_rate) : length(length), clock_rate(clock_rate) {} Time(int length, int clock_rate) : Time((unsigned int)length, (unsigned int)clock_rate) {} Time(uint64_t length, uint64_t clock_rate) { install_result(length, clock_rate); - simplify(); } Time(float value) { install_float(value); - simplify(); } /*! @@ -94,6 +92,10 @@ struct Time { inline Time &operator += (const Time &other) { if(!other.length) return *this; + if(!length) { + *this = other; + return *this; + } uint64_t result_length; uint64_t result_clock_rate; @@ -207,6 +209,12 @@ struct Time { private: inline void install_result(uint64_t long_length, uint64_t long_clock_rate) { + if(long_length <= std::numeric_limits::max() && long_clock_rate <= std::numeric_limits::max()) { + length = (unsigned int)long_length; + clock_rate = (unsigned int)long_clock_rate; + return; + } + // TODO: switch to appropriate values if the result is too large or small to fit, even with trimmed accuracy. if(!long_length) { length = 0; @@ -214,6 +222,11 @@ struct Time { return; } + while(!(long_length&0xf) && !(long_clock_rate&0xf)) { + long_length >>= 4; + long_clock_rate >>= 4; + } + while(!(long_length&1) && !(long_clock_rate&1)) { long_length >>= 1; long_clock_rate >>= 1;