diff options
author | Abseil Team <absl-team@google.com> | 2023-02-02 03:05:07 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-02-02 03:05:54 -0800 |
commit | 7005fede1e309d0a6070be3008b365112492aa80 (patch) | |
tree | 99968e37829bea518b344d13e286bd54508aa656 /absl/time/duration.cc | |
parent | 9858e5421b14c9d72f2103af10010c5452c6d55f (diff) |
Get rid of tail padding within `absl::Duration`. This reduces memory usage needs when storing duration in containers (e.g. `vector<absl::Duration>` uses 25% less memory), and allows classes with `absl::Duration` fields to fit other stuff in memory previously used by tail padding (e.g. `std::optional<absl::Duration>` is now 16 bytes instead of 24).
PiperOrigin-RevId: 506568782
Change-Id: Ic9e077f02a80da013fb2d312aff77761b970c07a
Diffstat (limited to 'absl/time/duration.cc')
-rw-r--r-- | absl/time/duration.cc | 39 |
1 files changed, 17 insertions, 22 deletions
diff --git a/absl/time/duration.cc b/absl/time/duration.cc index b915d749..911e80f8 100644 --- a/absl/time/duration.cc +++ b/absl/time/duration.cc @@ -407,18 +407,16 @@ int64_t IDivDuration(bool satq, const Duration num, const Duration den, Duration& Duration::operator+=(Duration rhs) { if (time_internal::IsInfiniteDuration(*this)) return *this; if (time_internal::IsInfiniteDuration(rhs)) return *this = rhs; - const int64_t orig_rep_hi = rep_hi_.Get(); - rep_hi_ = DecodeTwosComp(EncodeTwosComp(rep_hi_.Get()) + - EncodeTwosComp(rhs.rep_hi_.Get())); + const int64_t orig_rep_hi = rep_hi_; + rep_hi_ = + DecodeTwosComp(EncodeTwosComp(rep_hi_) + EncodeTwosComp(rhs.rep_hi_)); if (rep_lo_ >= kTicksPerSecond - rhs.rep_lo_) { - rep_hi_ = DecodeTwosComp(EncodeTwosComp(rep_hi_.Get()) + 1); + rep_hi_ = DecodeTwosComp(EncodeTwosComp(rep_hi_) + 1); rep_lo_ -= kTicksPerSecond; } rep_lo_ += rhs.rep_lo_; - if (rhs.rep_hi_.Get() < 0 ? rep_hi_.Get() > orig_rep_hi - : rep_hi_.Get() < orig_rep_hi) { - return *this = - rhs.rep_hi_.Get() < 0 ? -InfiniteDuration() : InfiniteDuration(); + if (rhs.rep_hi_ < 0 ? rep_hi_ > orig_rep_hi : rep_hi_ < orig_rep_hi) { + return *this = rhs.rep_hi_ < 0 ? -InfiniteDuration() : InfiniteDuration(); } return *this; } @@ -426,21 +424,18 @@ Duration& Duration::operator+=(Duration rhs) { Duration& Duration::operator-=(Duration rhs) { if (time_internal::IsInfiniteDuration(*this)) return *this; if (time_internal::IsInfiniteDuration(rhs)) { - return *this = rhs.rep_hi_.Get() >= 0 ? -InfiniteDuration() - : InfiniteDuration(); + return *this = rhs.rep_hi_ >= 0 ? -InfiniteDuration() : InfiniteDuration(); } - const int64_t orig_rep_hi = rep_hi_.Get(); - rep_hi_ = DecodeTwosComp(EncodeTwosComp(rep_hi_.Get()) - - EncodeTwosComp(rhs.rep_hi_.Get())); + const int64_t orig_rep_hi = rep_hi_; + rep_hi_ = + DecodeTwosComp(EncodeTwosComp(rep_hi_) - EncodeTwosComp(rhs.rep_hi_)); if (rep_lo_ < rhs.rep_lo_) { - rep_hi_ = DecodeTwosComp(EncodeTwosComp(rep_hi_.Get()) - 1); + rep_hi_ = DecodeTwosComp(EncodeTwosComp(rep_hi_) - 1); rep_lo_ += kTicksPerSecond; } rep_lo_ -= rhs.rep_lo_; - if (rhs.rep_hi_.Get() < 0 ? rep_hi_.Get() < orig_rep_hi - : rep_hi_.Get() > orig_rep_hi) { - return *this = rhs.rep_hi_.Get() >= 0 ? -InfiniteDuration() - : InfiniteDuration(); + if (rhs.rep_hi_ < 0 ? rep_hi_ < orig_rep_hi : rep_hi_ > orig_rep_hi) { + return *this = rhs.rep_hi_ >= 0 ? -InfiniteDuration() : InfiniteDuration(); } return *this; } @@ -451,7 +446,7 @@ Duration& Duration::operator-=(Duration rhs) { Duration& Duration::operator*=(int64_t r) { if (time_internal::IsInfiniteDuration(*this)) { - const bool is_neg = (r < 0) != (rep_hi_.Get() < 0); + const bool is_neg = (r < 0) != (rep_hi_ < 0); return *this = is_neg ? -InfiniteDuration() : InfiniteDuration(); } return *this = ScaleFixed<SafeMultiply>(*this, r); @@ -459,7 +454,7 @@ Duration& Duration::operator*=(int64_t r) { Duration& Duration::operator*=(double r) { if (time_internal::IsInfiniteDuration(*this) || !IsFinite(r)) { - const bool is_neg = std::signbit(r) != (rep_hi_.Get() < 0); + const bool is_neg = (std::signbit(r) != 0) != (rep_hi_ < 0); return *this = is_neg ? -InfiniteDuration() : InfiniteDuration(); } return *this = ScaleDouble<std::multiplies>(*this, r); @@ -467,7 +462,7 @@ Duration& Duration::operator*=(double r) { Duration& Duration::operator/=(int64_t r) { if (time_internal::IsInfiniteDuration(*this) || r == 0) { - const bool is_neg = (r < 0) != (rep_hi_.Get() < 0); + const bool is_neg = (r < 0) != (rep_hi_ < 0); return *this = is_neg ? -InfiniteDuration() : InfiniteDuration(); } return *this = ScaleFixed<std::divides>(*this, r); @@ -475,7 +470,7 @@ Duration& Duration::operator/=(int64_t r) { Duration& Duration::operator/=(double r) { if (time_internal::IsInfiniteDuration(*this) || !IsValidDivisor(r)) { - const bool is_neg = std::signbit(r) != (rep_hi_.Get() < 0); + const bool is_neg = (std::signbit(r) != 0) != (rep_hi_ < 0); return *this = is_neg ? -InfiniteDuration() : InfiniteDuration(); } return *this = ScaleDouble<std::divides>(*this, r); |