Bug 128644 - Remove some unintended copies in ranged for loops
Summary: Remove some unintended copies in ranged for loops
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit Misc. (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Brent Fulgham
Depends on:
Reported: 2014-02-11 19:11 PST by Brent Fulgham
Modified: 2014-02-12 09:05 PST (History)
9 users (show)

See Also:

Patch (15.09 KB, patch)
2014-02-11 19:54 PST, Brent Fulgham
andersca: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Brent Fulgham 2014-02-11 19:11:57 PST
Once I noticed the handful of errors in Bug 128578, I did some more searching and turned up a few more.

Per http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2049.pdf, the ranged iterator syntax:

    for( type-specifier-seq simple-declarator : expression )

is syntactically equivalent to

    typedef decltype(expression) C;
    auto&& rng(expression);
    for (auto begin(std::For<C>::begin(rng)), end(std::For<C>::end(rng)); begin != end; ++ begin) {
        type-specifier-seq simple-declarator(∗begin);

The issue here is that the type of auto (instead of 'auto&' or 'const auto&') causes us to make a copy of each element as we pass through the loop.

When the container is just a  set of pointers, it's not much of an issue. But when we have iterate over reference-counted types, strings, or other larger objects we incur unnecessary costs.
Comment 1 Brent Fulgham 2014-02-11 19:54:49 PST
Created attachment 223932 [details]
Comment 2 Brent Fulgham 2014-02-12 09:05:23 PST
Committed r163959: <http://trac.webkit.org/changeset/163959>