Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(54)

Issue 753603002: Change preference APIs to take std::string instead of const char*. (Closed)

Created:
6 years, 1 month ago by Georges Khalil
Modified:
6 years ago
Reviewers:
gab, Nico
CC:
chromium-reviews, erikwright+watch_chromium.org
Base URL:
https://chromium.googlesource.com/chromium/src.git@master
Project:
chromium
Visibility:
Public.

Description

Change preference APIs to take std::string instead of const char*. The string can't be NULL, and most callers have std::strings already, so this makes the intent clearer and avoid unnecessary switch between std::string and const char*. Right now, the prefs are stored in char*, then converted to strings, then passed again as char*, only to be converted again, which creates the unnecessary temporary strings. For example, PrefValueStore::OnPrefValueChanged passes |key|, which is a string, as a const char* to PrefValueStore::NotifyPrefChanged, which then passes it to PrefNotifierImpl::OnPreferenceChanged as a string again. Our tests show that this change reduces on average by ~9% the number of total allocations in the browser process. Notes: - The underlying pref map in the PrefRegistry is based on string keys and thus any call to it, e.g. find(), needs to convert to string anyways at the bottom of the implementation so might as well force the string to be passed into the API originally. - A strict char* API, i.e. all the way into the pref map with a custom comparator/etc., would make memory harder to manage since we can't strictly force the caller to pass only pointers to constants in .rodata and would thus require explicit memory management. - Ideally, we would need a hybrid string class that manages the memory on demand. That way, it can point to hard coded strings with the memory management disabled or allocate a buffer that it will manage, to get the best of both worlds. BUG=438215 Committed: https://crrev.com/7da6e9da88541615b6e709baa4ed6b05c40fcdcf Cr-Commit-Position: refs/heads/master@{#306514}

Patch Set 1 : #

Total comments: 4

Patch Set 2 : Merge up to r306198. #

Patch Set 3 : Reverted changed to values.cc. #

Total comments: 10

Patch Set 4 : Removed all calls to c_str() in prefs. #

Unified diffs Side-by-side diffs Delta from patch set Stats (+240 lines, -246 lines) Patch
M base/prefs/mock_pref_change_callback.h View 1 2 3 1 chunk +1 line, -2 lines 0 comments Download
M base/prefs/pref_change_registrar.h View 1 chunk +3 lines, -3 lines 0 comments Download
M base/prefs/pref_change_registrar.cc View 1 2 3 4 chunks +5 lines, -6 lines 0 comments Download
M base/prefs/pref_change_registrar_unittest.cc View 1 chunk +2 lines, -4 lines 0 comments Download
M base/prefs/pref_member.h View 2 chunks +8 lines, -5 lines 0 comments Download
M base/prefs/pref_member.cc View 1 2 3 9 chunks +11 lines, -15 lines 0 comments Download
M base/prefs/pref_member_unittest.cc View 1 chunk +1 line, -1 line 0 comments Download
M base/prefs/pref_notifier_impl.h View 1 chunk +2 lines, -2 lines 0 comments Download
M base/prefs/pref_notifier_impl.cc View 1 2 3 3 chunks +3 lines, -3 lines 0 comments Download
M base/prefs/pref_notifier_impl_unittest.cc View 1 chunk +1 line, -1 line 0 comments Download
M base/prefs/pref_registry.h View 1 chunk +2 lines, -2 lines 0 comments Download
M base/prefs/pref_registry.cc View 2 chunks +2 lines, -2 lines 0 comments Download
M base/prefs/pref_registry_simple.h View 1 chunk +12 lines, -11 lines 0 comments Download
M base/prefs/pref_registry_simple.cc View 1 chunk +10 lines, -10 lines 0 comments Download
M base/prefs/pref_service.h View 1 5 chunks +30 lines, -31 lines 0 comments Download
M base/prefs/pref_service.cc View 1 2 3 21 chunks +49 lines, -46 lines 0 comments Download
M base/prefs/pref_value_store.h View 4 chunks +15 lines, -15 lines 0 comments Download
M base/prefs/pref_value_store.cc View 1 2 3 9 chunks +22 lines, -20 lines 0 comments Download
M base/prefs/pref_value_store_unittest.cc View 1 chunk +1 line, -1 line 0 comments Download
M base/prefs/scoped_user_pref_update.h View 2 chunks +2 lines, -2 lines 0 comments Download
M base/prefs/scoped_user_pref_update.cc View 1 2 3 2 chunks +3 lines, -5 lines 0 comments Download
M base/prefs/testing_pref_service.h View 3 chunks +55 lines, -59 lines 0 comments Download

Messages

Total messages: 32 (8 generated)
Georges Khalil
thakis@chromium.org: Please review changes in base/values.cc. gab@chromium.org: Please review changes in base/prefs.
6 years ago (2014-12-01 16:12:20 UTC) #4
Nico
Code looks fine, but the CL description needs work – why is it useful to ...
6 years ago (2014-12-01 16:57:03 UTC) #5
Nico
On 2014/12/01 16:57:03, Nico wrote: > Code looks fine, but the CL description needs work ...
6 years ago (2014-12-01 17:02:08 UTC) #6
gab
Thanks for the analysis and CL, general concern and suggestion below. https://codereview.chromium.org/753603002/diff/40001/base/prefs/pref_value_store.cc File base/prefs/pref_value_store.cc (left): ...
6 years ago (2014-12-01 19:12:11 UTC) #7
Nico
On Mon, Dec 1, 2014 at 11:12 AM, <gab@chromium.org> wrote: > Thanks for the analysis ...
6 years ago (2014-12-01 19:17:57 UTC) #8
gab
On 2014/12/01 19:17:57, Nico wrote: > On Mon, Dec 1, 2014 at 11:12 AM, <https://mail.google.com/mail/?view=cm&fs=1&tf=1&to=gab@chromium.org> ...
6 years ago (2014-12-01 19:29:15 UTC) #9
Georges Khalil
PTAL. https://codereview.chromium.org/753603002/diff/40001/base/prefs/pref_value_store.cc File base/prefs/pref_value_store.cc (left): https://codereview.chromium.org/753603002/diff/40001/base/prefs/pref_value_store.cc#oldcode252 base/prefs/pref_value_store.cc:252: NotifyPrefChanged(key.c_str(), type); On 2014/12/01 19:12:11, gab wrote: > ...
6 years ago (2014-12-01 20:34:12 UTC) #12
Nico
lgtm nit: Please wrap your CL description to ~78 columns, and try to follow the ...
6 years ago (2014-12-01 20:38:11 UTC) #13
commit-bot: I haz the power
CQ is trying da patch. Follow status at https://chromium-cq-status.appspot.com/patch-status/753603002/120001
6 years ago (2014-12-01 20:53:38 UTC) #15
gab
See comments below. Out of scope for this CL, but thinking of the broader problem: ...
6 years ago (2014-12-01 21:13:01 UTC) #16
gab
Oh also, as a general Chrome flow tip, do not put multiple reviewers if you ...
6 years ago (2014-12-01 21:14:38 UTC) #18
Nico
On Mon, Dec 1, 2014 at 1:14 PM, <gab@chromium.org> wrote: > Oh also, as a ...
6 years ago (2014-12-01 21:18:18 UTC) #19
Georges Khalil
On 2014/12/01 21:18:18, Nico wrote: > On Mon, Dec 1, 2014 at 1:14 PM, <mailto:gab@chromium.org> ...
6 years ago (2014-12-01 21:27:57 UTC) #20
Georges Khalil
https://codereview.chromium.org/753603002/diff/120001/base/prefs/testing_pref_service.h File base/prefs/testing_pref_service.h (right): https://codereview.chromium.org/753603002/diff/120001/base/prefs/testing_pref_service.h#newcode167 base/prefs/testing_pref_service.h:167: RemoveRecommendedPref(const std::string& path) { On 2014/12/01 21:13:01, gab wrote: ...
6 years ago (2014-12-01 21:28:21 UTC) #21
gab
Yes additional changes in a follow-up CL sgtm, some comments were about new c_str() calls ...
6 years ago (2014-12-01 21:56:17 UTC) #22
Finnur
Neat. :) Couple of drive-by nits on the CL description: > Change preference abis s/abis/APIs/ ...
6 years ago (2014-12-02 11:13:55 UTC) #23
gab
CC Bernhard FYI. @Georges: can you please re-add the motivation behind this change (~12% strings ...
6 years ago (2014-12-02 13:41:56 UTC) #24
Bernhard Bauer
+1 to what Gab said. Having a type that implies string literals would be awesome. ...
6 years ago (2014-12-02 13:52:54 UTC) #25
Georges Khalil
PTAL. I went through all the prefs code and eliminated all calls to c_str(). String ...
6 years ago (2014-12-02 17:57:00 UTC) #26
gab
lgtm, please also add these items to the CL description: 1) One of the example ...
6 years ago (2014-12-02 18:56:54 UTC) #27
Georges Khalil
On 2014/12/02 18:56:54, gab wrote: > lgtm, please also add these items to the CL ...
6 years ago (2014-12-02 22:44:15 UTC) #28
commit-bot: I haz the power
CQ is trying da patch. Follow status at https://chromium-cq-status.appspot.com/patch-status/753603002/140001
6 years ago (2014-12-02 22:50:03 UTC) #30
commit-bot: I haz the power
Committed patchset #4 (id:140001)
6 years ago (2014-12-03 01:10:36 UTC) #31
commit-bot: I haz the power
6 years ago (2014-12-03 01:11:18 UTC) #32
Message was sent while issue was closed.
Patchset 4 (id:??) landed as
https://crrev.com/7da6e9da88541615b6e709baa4ed6b05c40fcdcf
Cr-Commit-Position: refs/heads/master@{#306514}

Powered by Google App Engine
This is Rietveld 408576698