New C++ "features" (sort of) in Chromium

244 views
Skip to first unread message

Peter Kasting

unread,
Mar 10, 2017, 3:23:21 AM3/10/17
to Chromium-dev
I occasionally write about new features in the C++11 support page, but in the last four months the only change is a clarification about using auto* in place of auto sometimes.  (Plug: if you want changes to the allowed features list, email c...@chromium.org as described in the guide.)

Instead, here are new things in Chromium which may be in upcoming C++ standards, but you can use today.
  • base::flat_set<T>, an alternative to std::set<T> that uses a vector instead of a tree for storage.  base/containers/flat_set.h gives more detail, but in short, if you have a small number of elements in your set or you are doing more lookups than modifications, consider using this container.  We found this could improve performance and significantly reduce memory overhead (on Android) in omnibox code.  flat_map<T> is in progress, and flat containers are being considered for future standardization.
  • base::Erase()/EraseIf(), which are proposed for future standardization as std::erase/erase_if().  These can be used to quickly erase from a container by value or predicate, which today is cumbersome:

    vec.erase(std::remove_if(vec.begin(), vec.end(), Pred), vec.end());

    becomes

    base::EraseIf(vec, Pred);
(Feel free to fix existing code!)

I hope this second point also reminds you to use STL algorithms and lambdas in general.  Lambdas and algorithms together provide a powerful way to condense/eliminate loops.  If you're unfamiliar with STL algorithms or slow to read code using them, study and use them more :).  For more on this (and "eliminate loops" in general), see the first 30 minutes of Xoogler Sean Parent's "C++ Seasoning" talk, which uses some old ChromiumOS code as a (negative) example.

PK

Michael Giuffrida

unread,
Mar 11, 2017, 11:31:20 PM3/11/17
to pkas...@google.com, Chromium-dev
Thanks for the reminders!

On a side note, is there a list of references to other Chromium C++11 guidelines/pitfalls, such as Rvalue references in Chromium? That page has a lot of useful info but doesn't seem to be linked to from anywhere (I only found it from a Google search).

--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev

Daniel Cheng

unread,
Mar 12, 2017, 12:04:18 AM3/12/17
to mich...@chromium.org, pkas...@google.com, Chromium-dev
There's a few sub pages off https://www.chromium.org/developers/coding-style that cover various C++ guidelines (not all related to C++11, but still useful reading):
- C++ dos and don'ts, which covers things like inlining and variable initialization
- Chromium C++ style guide, which covers guidelines for logging, forward declarations, and types
- Important Data Structures and Abstractions, which probably needs an update for things (documentation related to MessageLoopProxy, Callback, Singleton, LazyInstance, etc. are a bit out of date)
- Chromium Style Checker Errors, which covers checks from Chrome's custom clang plugin

Daniel

---
You received this message because you are subscribed to the Google Groups "Chromium-dev" group.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-dev/CACi5S_1f5ZhcRd1%2BZn%3Dunj0zfXEvXYeKPf6rVp5mc%3D76BE_RWQ%40mail.gmail.com.

Nico Weber

unread,
Mar 12, 2017, 3:13:47 PM3/12/17
to Daniel Cheng, Chromium-dev, Michael Giuffrida, Peter Kasting
Michael, chromium-cpp.appspot.com has a list of allowed c++11 features; it links to that dos&don'ts doc iirc.

Peter, thanks for sending this message. To balance things out a bit, I'd encourage folks to not overboard with lambdas and functional style. I find that a for loop can often be clearer than a lambda and a wrapper, so do use lambdas when they help, but don't go overboard.

Brett Wilson

unread,
Mar 14, 2017, 7:57:21 PM3/14/17
to Peter Kasting, Chromium-dev
On Fri, Mar 10, 2017 at 12:22 AM, 'Peter Kasting' via Chromium-dev <chromi...@chromium.org> wrote:
I occasionally write about new features in the C++11 support page, but in the last four months the only change is a clarification about using auto* in place of auto sometimes.  (Plug: if you want changes to the allowed features list, email c...@chromium.org as described in the guide.)

Instead, here are new things in Chromium which may be in upcoming C++ standards, but you can use today.
  • base::flat_set<T>, an alternative to std::set<T> that uses a vector instead of a tree for storage.  base/containers/flat_set.h gives more detail, but in short, if you have a small number of elements in your set or you are doing more lookups than modifications, consider using this container.  We found this could improve performance and significantly reduce memory overhead (on Android) in omnibox code.  flat_map<T> is in progress, and flat containers are being considered for future standardization.
Just a warning: don't go willy-nilly converting std::set to flat_set. Most sets in Chrome are small so this is a solid win, but since insertion time is O(n²) it has the potential to be bad if the set gets large in edge cases. To use this you really need to understand the worst-case size or be able to mostly populate the set in one go with the range constructor.

Brett

Денис

unread,
Mar 16, 2017, 7:07:35 AM3/16/17
to Chromium-dev, pkas...@google.com
It's also ok (if your moves are not crazy expensive) if you build your flat_set once using range constructor, no matter the size.

среда, 15 марта 2017 г., 2:57:21 UTC+3 пользователь Brett Wilson написал:

dyar...@yandex-team.ru

unread,
Jul 22, 2017, 6:23:55 PM7/22/17
to Chromium-dev
C++14 interface extensions for base::flat_set/flat_map have landed! (cl). Now functions that require lookup (erase, count, find, equal_range, lower_bound, upper_bound) if the comparison is transparet accept any type comparable with key_type, not just key_type itself. This means that you can do lookup in the set of std::strings by const char* or base::StringPiece without constructing temporary variables. This also means that flat_set of unique_ptrs now can be used in a natural way, even though we don't have a ready to go comparator in base (the default one won't work since std::unique_ptr is not comparible with plain pointers).
You can find more information and some examples in updated docs.

пятница, 10 марта 2017 г., 11:23:21 UTC+3 пользователь Peter Kasting написал:
Reply all
Reply to author
Forward
0 new messages