0

Introduce TransparentUnorderedStringMap.

The added file contains a simple alias of an std::unordered_map
that supports looking up std::string_view without incurring a copy.

Bug: 349741884
Change-Id: I50ef7faa2a1c04b03d4ea9c3d9bbb26aff2c6b77
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5676786
Reviewed-by: Colin Blundell <blundell@chromium.org>
Commit-Queue: Jan Keitel <jkeitel@google.com>
Reviewed-by: Dominic Battré <battre@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1323590}
This commit is contained in:
Jan Keitel
2024-07-05 08:34:42 +00:00
committed by Chromium LUCI CQ
parent 53454082a8
commit 562889db3f
5 changed files with 35 additions and 19 deletions

@ -47,6 +47,7 @@ component("prefs") {
"scoped_user_pref_update.h",
"segregated_pref_store.cc",
"segregated_pref_store.h",
"transparent_unordered_string_map.h",
"value_map_pref_store.cc",
"value_map_pref_store.h",
"wrap_with_prefix_pref_store.cc",

@ -34,6 +34,7 @@
#include "components/prefs/persistent_pref_store.h"
#include "components/prefs/pref_value_store.h"
#include "components/prefs/prefs_export.h"
#include "components/prefs/transparent_unordered_string_map.h"
#if BUILDFLAG(IS_ANDROID)
#include "base/android/scoped_java_ref.h"
@ -451,11 +452,7 @@ 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.
struct StringViewHasher : public std::hash<std::string_view> {
using is_transparent = void;
};
using PreferenceMap = std::
unordered_map<std::string, Preference, StringViewHasher, std::equal_to<>>;
using PreferenceMap = TransparentUnorderedStringMap<Preference>;
// Give access to ReportUserPrefChanged() and GetMutableUserPref().
friend class subtle::ScopedUserPrefUpdateBase;

@ -0,0 +1,28 @@
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_PREFS_TRANSPARENT_UNORDERED_STRING_MAP_H_
#define COMPONENTS_PREFS_TRANSPARENT_UNORDERED_STRING_MAP_H_
#include <functional>
#include <string>
#include <string_view>
#include <unordered_map>
namespace internal {
struct StringViewHasher : public std::hash<std::string_view> {
using is_transparent = void;
};
} // namespace internal
// A `std::unordered_map` from `std::string` to `ValueType` that allows
// copy-less find for `std::string_view`.
template <typename ValueType>
using TransparentUnorderedStringMap =
std::unordered_map<std::string,
ValueType,
internal::StringViewHasher,
std::equal_to<>>;
#endif // COMPONENTS_PREFS_TRANSPARENT_UNORDERED_STRING_MAP_H_

@ -18,6 +18,7 @@
#include "base/observer_list.h"
#include "base/sequence_checker.h"
#include "base/values.h"
#include "components/prefs/transparent_unordered_string_map.h"
#include "components/prefs/writeable_pref_store.h"
#include "components/sync/base/model_type.h"
#include "components/sync/model/sync_data.h"
@ -206,13 +207,7 @@ class PrefModelAssociator final : public syncer::SyncableService,
// from sync.
using SyncedPrefObserverList =
base::ObserverList<SyncedPrefObserver>::Unchecked;
struct StringViewHasher : public std::hash<std::string_view> {
using is_transparent = void;
};
std::unordered_map<std::string,
std::unique_ptr<SyncedPrefObserverList>,
StringViewHasher,
std::equal_to<>>
TransparentUnorderedStringMap<std::unique_ptr<SyncedPrefObserverList>>
synced_pref_observers_;
SEQUENCE_CHECKER(sequence_checker_);

@ -21,6 +21,7 @@
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "base/values.h"
#include "components/prefs/transparent_unordered_string_map.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "mojo/public/cpp/bindings/remote.h"
#include "services/preferences/public/mojom/preferences.mojom.h"
@ -138,14 +139,8 @@ class PrefHashFilter final : public InterceptablePrefFilter {
// A map of paths to TrackedPreferences; this map owns this individual
// TrackedPreference objects.
struct StringViewHasher : public std::hash<std::string_view> {
using is_transparent = void;
};
using TrackedPreferencesMap =
std::unordered_map<std::string,
std::unique_ptr<TrackedPreference>,
StringViewHasher,
std::equal_to<>>;
TransparentUnorderedStringMap<std::unique_ptr<TrackedPreference>>;
// A map from changed paths to their corresponding TrackedPreferences (which
// aren't owned by this map).