summaryrefslogtreecommitdiff
path: root/absl/time/duration.cc
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2023-02-02 00:48:53 -0800
committerGravatar Copybara-Service <copybara-worker@google.com>2023-02-02 00:49:39 -0800
commit9858e5421b14c9d72f2103af10010c5452c6d55f (patch)
treeaf3a6297b0af2933fe1e4a7755b4214848c8e3c5 /absl/time/duration.cc
parent7c7cafef4ff11979a26702080a2a4bf0dad7d856 (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: 506543503 Change-Id: Ifeb2397c953a5d3da317a70ab49a3ebb85042344
Diffstat (limited to 'absl/time/duration.cc')
-rw-r--r--absl/time/duration.cc39
1 files changed, 22 insertions, 17 deletions
diff --git a/absl/time/duration.cc b/absl/time/duration.cc
index 911e80f8..b915d749 100644
--- a/absl/time/duration.cc
+++ b/absl/time/duration.cc
@@ -407,16 +407,18 @@ 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_;
- rep_hi_ =
- DecodeTwosComp(EncodeTwosComp(rep_hi_) + EncodeTwosComp(rhs.rep_hi_));
+ const int64_t orig_rep_hi = rep_hi_.Get();
+ rep_hi_ = DecodeTwosComp(EncodeTwosComp(rep_hi_.Get()) +
+ EncodeTwosComp(rhs.rep_hi_.Get()));
if (rep_lo_ >= kTicksPerSecond - rhs.rep_lo_) {
- rep_hi_ = DecodeTwosComp(EncodeTwosComp(rep_hi_) + 1);
+ rep_hi_ = DecodeTwosComp(EncodeTwosComp(rep_hi_.Get()) + 1);
rep_lo_ -= kTicksPerSecond;
}
rep_lo_ += rhs.rep_lo_;
- if (rhs.rep_hi_ < 0 ? rep_hi_ > orig_rep_hi : rep_hi_ < orig_rep_hi) {
- return *this = rhs.rep_hi_ < 0 ? -InfiniteDuration() : InfiniteDuration();
+ 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();
}
return *this;
}
@@ -424,18 +426,21 @@ 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_ >= 0 ? -InfiniteDuration() : InfiniteDuration();
+ return *this = rhs.rep_hi_.Get() >= 0 ? -InfiniteDuration()
+ : InfiniteDuration();
}
- const int64_t orig_rep_hi = rep_hi_;
- rep_hi_ =
- DecodeTwosComp(EncodeTwosComp(rep_hi_) - EncodeTwosComp(rhs.rep_hi_));
+ const int64_t orig_rep_hi = rep_hi_.Get();
+ rep_hi_ = DecodeTwosComp(EncodeTwosComp(rep_hi_.Get()) -
+ EncodeTwosComp(rhs.rep_hi_.Get()));
if (rep_lo_ < rhs.rep_lo_) {
- rep_hi_ = DecodeTwosComp(EncodeTwosComp(rep_hi_) - 1);
+ rep_hi_ = DecodeTwosComp(EncodeTwosComp(rep_hi_.Get()) - 1);
rep_lo_ += kTicksPerSecond;
}
rep_lo_ -= rhs.rep_lo_;
- if (rhs.rep_hi_ < 0 ? rep_hi_ < orig_rep_hi : rep_hi_ > orig_rep_hi) {
- return *this = rhs.rep_hi_ >= 0 ? -InfiniteDuration() : InfiniteDuration();
+ 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();
}
return *this;
}
@@ -446,7 +451,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_ < 0);
+ const bool is_neg = (r < 0) != (rep_hi_.Get() < 0);
return *this = is_neg ? -InfiniteDuration() : InfiniteDuration();
}
return *this = ScaleFixed<SafeMultiply>(*this, r);
@@ -454,7 +459,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) != 0) != (rep_hi_ < 0);
+ const bool is_neg = std::signbit(r) != (rep_hi_.Get() < 0);
return *this = is_neg ? -InfiniteDuration() : InfiniteDuration();
}
return *this = ScaleDouble<std::multiplies>(*this, r);
@@ -462,7 +467,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_ < 0);
+ const bool is_neg = (r < 0) != (rep_hi_.Get() < 0);
return *this = is_neg ? -InfiniteDuration() : InfiniteDuration();
}
return *this = ScaleFixed<std::divides>(*this, r);
@@ -470,7 +475,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) != 0) != (rep_hi_ < 0);
+ const bool is_neg = std::signbit(r) != (rep_hi_.Get() < 0);
return *this = is_neg ? -InfiniteDuration() : InfiniteDuration();
}
return *this = ScaleDouble<std::divides>(*this, r);