Export of internal Abseil changes

--
daa829a331a2316713681b5fe7630d1951e0fdec by Gennadiy Rozental <rogeeff@google.com>:

Eliminate Flag's destroy method.

The Abseil Flags are never destroyed. The only place where Destroy method was invoked was in some obscure place during flag registration, where we faces with duplicate retired flag registration. Regired Flag destruction is empty anyway. so we can just delete the duplicate object. The FLagImpl::Destroy is never invoked.

PiperOrigin-RevId: 294472413

--
3c159499ccde8ccdd6907b3a1ddb26be7d3f016f by Abseil Team <absl-team@google.com>:

Internal change.

PiperOrigin-RevId: 294401573

--
25910db425c50d9b9a8f8275af5a67c2935934fd by Shahriar Rouf <nafi@google.com>:

Optimize absl::string_view::compare.

Motivation: https://godbolt.org/z/Uz8DWV
PiperOrigin-RevId: 294286196
GitOrigin-RevId: daa829a331a2316713681b5fe7630d1951e0fdec
Change-Id: I818dad27ac5eb61bb7632e01224953cd882803bf
This commit is contained in:
Abseil Team 2020-02-11 10:47:28 -08:00 committed by Mark Barolak
parent bf78e97730
commit 98eb410c93
6 changed files with 26 additions and 58 deletions

View file

@ -1906,8 +1906,7 @@ inline auto btree<P>::insert_hint_unique(iterator position, const key_type &key,
-> std::pair<iterator, bool> { -> std::pair<iterator, bool> {
if (!empty()) { if (!empty()) {
if (position == end() || compare_keys(key, position.key())) { if (position == end() || compare_keys(key, position.key())) {
iterator prev = position; if (position == begin() || compare_keys(std::prev(position).key(), key)) {
if (position == begin() || compare_keys((--prev).key(), key)) {
// prev.key() < key < position.key() // prev.key() < key < position.key()
return {internal_emplace(position, std::forward<Args>(args)...), true}; return {internal_emplace(position, std::forward<Args>(args)...), true};
} }
@ -1953,17 +1952,16 @@ auto btree<P>::insert_hint_multi(iterator position, ValueType &&v) -> iterator {
if (!empty()) { if (!empty()) {
const key_type &key = params_type::key(v); const key_type &key = params_type::key(v);
if (position == end() || !compare_keys(position.key(), key)) { if (position == end() || !compare_keys(position.key(), key)) {
iterator prev = position; if (position == begin() ||
if (position == begin() || !compare_keys(key, (--prev).key())) { !compare_keys(key, std::prev(position).key())) {
// prev.key() <= key <= position.key() // prev.key() <= key <= position.key()
return internal_emplace(position, std::forward<ValueType>(v)); return internal_emplace(position, std::forward<ValueType>(v));
} }
} else { } else {
iterator next = position; ++position;
++next; if (position == end() || !compare_keys(position.key(), key)) {
if (next == end() || !compare_keys(next.key(), key)) { // {original `position`}.key() < key < {current `position`}.key()
// position.key() < key <= next.key() return internal_emplace(position, std::forward<ValueType>(v));
return internal_emplace(next, std::forward<ValueType>(v));
} }
} }
} }

View file

@ -178,9 +178,6 @@ class CommandLineFlag {
public: public:
constexpr CommandLineFlag() = default; constexpr CommandLineFlag() = default;
// Virtual destructor
virtual void Destroy() = 0;
// Not copyable/assignable. // Not copyable/assignable.
CommandLineFlag(const CommandLineFlag&) = delete; CommandLineFlag(const CommandLineFlag&) = delete;
CommandLineFlag& operator=(const CommandLineFlag&) = delete; CommandLineFlag& operator=(const CommandLineFlag&) = delete;

View file

@ -120,27 +120,6 @@ absl::Mutex* FlagImpl::DataGuard() const {
return reinterpret_cast<absl::Mutex*>(&data_guard_); return reinterpret_cast<absl::Mutex*>(&data_guard_);
} }
void FlagImpl::Destroy() {
{
absl::MutexLock l(DataGuard());
// Values are heap allocated for Abseil Flags.
if (value_.dynamic) Delete(op_, value_.dynamic);
// Release the dynamically allocated default value if any.
if (DefaultKind() == FlagDefaultKind::kDynamicValue) {
Delete(op_, default_src_.dynamic_value);
}
// If this flag has an assigned callback, release callback data.
if (callback_) delete callback_;
}
absl::MutexLock l(&flag_mutex_lifetime_guard);
DataGuard()->~Mutex();
is_data_guard_inited_ = false;
}
void FlagImpl::AssertValidType(const flags_internal::FlagOpFn op) const { void FlagImpl::AssertValidType(const flags_internal::FlagOpFn op) const {
// `op` is the unmarshaling operation corresponding to the declaration // `op` is the unmarshaling operation corresponding to the declaration
// visibile at the call site. `op_` is the Flag's defined unmarshalling // visibile at the call site. `op_` is the Flag's defined unmarshalling

View file

@ -290,9 +290,6 @@ class FlagImpl {
default_src_(default_value_gen), default_src_(default_value_gen),
data_guard_{} {} data_guard_{} {}
// Forces destruction of the Flag's data.
void Destroy();
// Constant access methods // Constant access methods
absl::string_view Name() const; absl::string_view Name() const;
std::string Filename() const; std::string Filename() const;
@ -515,8 +512,6 @@ class Flag final : public flags_internal::CommandLineFlag {
private: private:
friend class FlagState<T>; friend class FlagState<T>;
void Destroy() override { impl_.Destroy(); }
void Read(void* dst) const override { impl_.Read(dst); } void Read(void* dst) const override { impl_.Read(dst); }
FlagOpFn TypeId() const override { return &FlagOps<T>; } FlagOpFn TypeId() const override { return &FlagOps<T>; }

View file

@ -57,11 +57,7 @@ namespace flags_internal {
class FlagRegistry { class FlagRegistry {
public: public:
FlagRegistry() = default; FlagRegistry() = default;
~FlagRegistry() { ~FlagRegistry() = default;
for (auto& p : flags_) {
p.second->Destroy();
}
}
// Store a flag in this registry. Takes ownership of *flag. // Store a flag in this registry. Takes ownership of *flag.
void RegisterFlag(CommandLineFlag* flag); void RegisterFlag(CommandLineFlag* flag);
@ -113,6 +109,7 @@ class FlagRegistryLock {
FlagRegistry* const fr_; FlagRegistry* const fr_;
}; };
void DestroyRetiredFlag(CommandLineFlag* flag);
} // namespace } // namespace
void FlagRegistry::RegisterFlag(CommandLineFlag* flag) { void FlagRegistry::RegisterFlag(CommandLineFlag* flag) {
@ -140,8 +137,8 @@ void FlagRegistry::RegisterFlag(CommandLineFlag* flag) {
flag->Typename(), "', respectively."), flag->Typename(), "', respectively."),
true); true);
} else if (old_flag->IsRetired()) { } else if (old_flag->IsRetired()) {
// Retired definitions are idempotent. Just keep the old one. // Retired flag can just be deleted.
flag->Destroy(); DestroyRetiredFlag(flag);
return; return;
} else if (old_flag->Filename() != flag->Filename()) { } else if (old_flag->Filename() != flag->Filename()) {
flags_internal::ReportUsageError( flags_internal::ReportUsageError(
@ -291,11 +288,6 @@ class RetiredFlagObj final : public flags_internal::CommandLineFlag {
: name_(name), op_(ops) {} : name_(name), op_(ops) {}
private: private:
void Destroy() override {
// Values are heap allocated for Retired Flags.
delete this;
}
absl::string_view Name() const override { return name_; } absl::string_view Name() const override { return name_; }
std::string Filename() const override { return "RETIRED"; } std::string Filename() const override { return "RETIRED"; }
absl::string_view Typename() const override { return ""; } absl::string_view Typename() const override { return ""; }
@ -328,6 +320,11 @@ class RetiredFlagObj final : public flags_internal::CommandLineFlag {
const FlagOpFn op_; const FlagOpFn op_;
}; };
void DestroyRetiredFlag(flags_internal::CommandLineFlag* flag) {
assert(flag->IsRetired());
delete static_cast<RetiredFlagObj*>(flag);
}
} // namespace } // namespace
bool Retire(const char* name, FlagOpFn ops) { bool Retire(const char* name, FlagOpFn ops) {

View file

@ -398,12 +398,11 @@ class string_view {
// on the respective sizes of the two `string_view`s to determine which is // on the respective sizes of the two `string_view`s to determine which is
// smaller, equal, or greater. // smaller, equal, or greater.
constexpr int compare(string_view x) const noexcept { constexpr int compare(string_view x) const noexcept {
return CompareImpl( return CompareImpl(length_, x.length_,
length_, x.length_, Min(length_, x.length_) == 0
length_ == 0 || x.length_ == 0
? 0 ? 0
: ABSL_INTERNAL_STRING_VIEW_MEMCMP( : ABSL_INTERNAL_STRING_VIEW_MEMCMP(
ptr_, x.ptr_, length_ < x.length_ ? length_ : x.length_)); ptr_, x.ptr_, Min(length_, x.length_)));
} }
// Overload of `string_view::compare()` for comparing a substring of the // Overload of `string_view::compare()` for comparing a substring of the
@ -541,12 +540,15 @@ class string_view {
#endif #endif
} }
static constexpr size_t Min(size_type length_a, size_type length_b) {
return length_a < length_b ? length_a : length_b;
}
static constexpr int CompareImpl(size_type length_a, size_type length_b, static constexpr int CompareImpl(size_type length_a, size_type length_b,
int compare_result) { int compare_result) {
return compare_result == 0 ? static_cast<int>(length_a > length_b) - return compare_result == 0 ? static_cast<int>(length_a > length_b) -
static_cast<int>(length_a < length_b) static_cast<int>(length_a < length_b)
: static_cast<int>(compare_result > 0) - : (compare_result < 0 ? -1 : 1);
static_cast<int>(compare_result < 0);
} }
const char* ptr_; const char* ptr_;