Bug 128644

Summary: Remove some unintended copies in ranged for loops
Product: WebKit Reporter: Brent Fulgham <bfulgham>
Component: WebKit Misc.Assignee: Brent Fulgham <bfulgham>
Status: RESOLVED FIXED    
Severity: Normal CC: allan.jensen, andersca, bfulgham, commit-queue, esprehn+autocc, glenn, gyuyoung.kim, macpherson, menard
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: All   
OS: All   
Attachments:
Description Flags
Patch andersca: review+

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 )
        statement

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);
        statement
    }

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]
Patch
Comment 2 Brent Fulgham 2014-02-12 09:05:23 PST
Committed r163959: <http://trac.webkit.org/changeset/163959>