summaryrefslogtreecommitdiff
path: root/absl/time/duration.cc
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2023-04-14 04:27:57 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2023-04-14 04:29:04 -0700
commitbce5bec5517f8c9ec448db9c2ffdf977dfff18eb (patch)
tree8d2d1711ea353b27e5cef04ffd7441fa196e34ad /absl/time/duration.cc
parent124e4f4edd1ec2d32193a673f8228f5b5c0ff9fa (diff)
Reland "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: 524256689 Change-Id: Ibf40d9e5411020179fa34c972349c7b58aa9d908
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 26fadb31..634e5d58 100644
--- a/absl/time/duration.cc
+++ b/absl/time/duration.cc
@@ -400,16 +400,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;
}
@@ -417,18 +419,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;
}
@@ -439,7 +444,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);
@@ -447,7 +452,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);
@@ -455,7 +460,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);
@@ -463,7 +468,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);