0

Use std::string_view in more PrefService methods.

This CL adds transparent hashing and transparent equality comparisons
to the std::unordered_map<std::string, Preference> that it keeps.

This should be a no-op for any interactions with the map that involve
a std::string:
- Comparisons just forward to std::string comparisons.
- Hashing for std::basic_string_view and std::basic_string is identical.

However, this CL allows to use a std::string_view when calling
std::unordered_map::find without requiring to convert the string_view
to a std::string first.

The CL takes advantage of that by replacing a few more const reference
to std::string by std::string_view. This avoids potential heap
allocations and reduces the binary size by ~3KB.

Bug: 349741884
Change-Id: Ifcfd7638ee27f641097ad3929bc7fa58c4260b3d
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5658436
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Commit-Queue: Jan Keitel <jkeitel@google.com>
Reviewed-by: Dominic Battré <battre@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1320204}
This commit is contained in:
Jan Keitel
2024-06-27 07:54:06 +00:00
committed by Chromium LUCI CQ
parent bbc7921421
commit f0eddf49f6
3 changed files with 25 additions and 20 deletions

@ -236,7 +236,7 @@ base::FilePath PrefService::GetFilePath(std::string_view path) const {
return *result;
}
bool PrefService::HasPrefPath(const std::string& path) const {
bool PrefService::HasPrefPath(std::string_view path) const {
const Preference* pref = FindPreference(path);
return pref && !pref->IsDefaultValue();
}
@ -285,17 +285,19 @@ PrefService::GetPreferencesValueAndStore() const {
}
const PrefService::Preference* PrefService::FindPreference(
const std::string& pref_name) const {
std::string_view path) const {
DCHECK_CALLED_ON_VALID_SEQUENCE(sequence_checker_);
auto it = prefs_map_.find(pref_name);
auto it = prefs_map_.find(path);
if (it != prefs_map_.end())
return &(it->second);
const base::Value* default_value = nullptr;
if (!pref_registry_->defaults()->GetValue(pref_name, &default_value))
if (!pref_registry_->defaults()->GetValue(path, &default_value)) {
return nullptr;
}
it = prefs_map_
.insert(std::make_pair(
pref_name, Preference(this, pref_name, default_value->type())))
std::string(path),
Preference(this, std::string(path), default_value->type())))
.first;
return &(it->second);
}
@ -327,20 +329,18 @@ PrefService::GetAllPrefStoresInitializationStatus() const {
return GetInitializationStatus();
}
bool PrefService::IsManagedPreference(const std::string& pref_name) const {
const Preference* pref = FindPreference(pref_name);
bool PrefService::IsManagedPreference(std::string_view path) const {
const Preference* pref = FindPreference(path);
return pref && pref->IsManaged();
}
bool PrefService::IsPreferenceManagedByCustodian(
const std::string& pref_name) const {
const Preference* pref = FindPreference(pref_name);
bool PrefService::IsPreferenceManagedByCustodian(std::string_view path) const {
const Preference* pref = FindPreference(path);
return pref && pref->IsManagedByCustodian();
}
bool PrefService::IsUserModifiablePreference(
const std::string& pref_name) const {
const Preference* pref = FindPreference(pref_name);
bool PrefService::IsUserModifiablePreference(std::string_view path) const {
const Preference* pref = FindPreference(path);
return pref && pref->IsUserModifiable();
}

@ -13,6 +13,7 @@
#include <stdint.h>
#include <functional>
#include <memory>
#include <set>
#include <string>
@ -236,19 +237,19 @@ class COMPONENTS_PREFS_EXPORT PrefService {
// Returns true if the preference for the given preference name is available
// and is managed.
bool IsManagedPreference(const std::string& pref_name) const;
bool IsManagedPreference(std::string_view path) const;
// Returns true if the preference for the given preference name is available
// and is controlled by the parent/guardian of the child Account.
bool IsPreferenceManagedByCustodian(const std::string& pref_name) const;
bool IsPreferenceManagedByCustodian(std::string_view path) const;
// Returns |true| if a preference with the given name is available and its
// value can be changed by the user.
bool IsUserModifiablePreference(const std::string& pref_name) const;
bool IsUserModifiablePreference(std::string_view path) const;
// Look up a preference. Returns NULL if the preference is not
// registered.
const PrefService::Preference* FindPreference(const std::string& path) const;
const PrefService::Preference* FindPreference(std::string_view path) const;
// If the path is valid and the value at the end of the path matches the type
// specified, it will return the specified value. Otherwise, the default
@ -334,7 +335,7 @@ class COMPONENTS_PREFS_EXPORT PrefService {
// NOTE: this is NOT the same as FindPreference. In particular
// FindPreference returns whether RegisterXXX has been invoked, where as
// this checks if a value exists for the path.
bool HasPrefPath(const std::string& path) const;
bool HasPrefPath(std::string_view path) const;
// Issues a callback for every preference value. The preferences must not be
// mutated during iteration.
@ -450,7 +451,11 @@ class COMPONENTS_PREFS_EXPORT PrefService {
// string comparisons. Order is unimportant, and deletions are rare.
// Confirmed on Android where this speeded Chrome startup by roughly 50ms
// vs. std::map, and by roughly 180ms vs. std::set of Preference pointers.
typedef std::unordered_map<std::string, Preference> PreferenceMap;
struct StringViewHasher : public std::hash<std::string_view> {
using is_transparent = void;
};
using PreferenceMap = std::
unordered_map<std::string, Preference, StringViewHasher, std::equal_to<>>;
// Give access to ReportUserPrefChanged() and GetMutableUserPref().
friend class subtle::ScopedUserPrefUpdateBase;

@ -282,7 +282,7 @@ void PrefServiceSyncable::RemoveSyncedPrefObserver(
void PrefServiceSyncable::AddRegisteredSyncablePreference(std::string_view path,
uint32_t flags) {
DCHECK(FindPreference(std::string(path)));
DCHECK(FindPreference(path));
if (flags & user_prefs::PrefRegistrySyncable::SYNCABLE_PREF) {
pref_sync_associator_.RegisterPref(path);
return;