Bug 149397

Summary: Use modern for-loops in WebCore/loader.
Product: WebKit Reporter: Hunseop Jeong <hs85.jeong>
Component: WebCore Misc.Assignee: Hunseop Jeong <hs85.jeong>
Status: RESOLVED FIXED    
Severity: Normal CC: buildbot, cdumez, commit-queue, darin, japhet, rniwa
Priority: P2    
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
Patch
none
Patch
none
Archive of layout-test-results from ews103 for mac-mavericks
none
Archive of layout-test-results from ews107 for mac-mavericks-wk2
none
Patch
none
Patch
none
PatchWithoutCopying
none
Patch none

Description Hunseop Jeong 2015-09-21 01:00:11 PDT
Refactor the for-loops using the ranged-for loops and auto keyword.
Comment 1 Hunseop Jeong 2015-09-21 01:17:46 PDT
Created attachment 261638 [details]
Patch
Comment 2 WebKit Commit Bot 2015-09-21 01:20:50 PDT
Attachment 261638 [details] did not pass style-queue:


ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:193:  Multi line control clauses should use braces.  [whitespace/braces] [4]
ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:777:  Multi line control clauses should use braces.  [whitespace/braces] [4]
ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:815:  Multi line control clauses should use braces.  [whitespace/braces] [4]
ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:816:  Multi line control clauses should use braces.  [whitespace/braces] [4]
ERROR: Source/WebCore/loader/archive/cf/LegacyWebArchive.cpp:250:  Multi line control clauses should use braces.  [whitespace/braces] [4]
Total errors found: 5 in 19 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 3 Hunseop Jeong 2015-09-21 01:25:02 PDT
Created attachment 261639 [details]
Patch
Comment 4 WebKit Commit Bot 2015-09-21 01:28:00 PDT
Attachment 261639 [details] did not pass style-queue:


ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:193:  Multi line control clauses should use braces.  [whitespace/braces] [4]
ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:777:  Multi line control clauses should use braces.  [whitespace/braces] [4]
ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:815:  Multi line control clauses should use braces.  [whitespace/braces] [4]
ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:816:  Multi line control clauses should use braces.  [whitespace/braces] [4]
ERROR: Source/WebCore/loader/archive/cf/LegacyWebArchive.cpp:250:  Multi line control clauses should use braces.  [whitespace/braces] [4]
Total errors found: 5 in 19 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 5 Build Bot 2015-09-21 01:59:49 PDT
Comment on attachment 261639 [details]
Patch

Attachment 261639 [details] did not pass mac-ews (mac):
Output: http://webkit-queues.webkit.org/results/192555

New failing tests:
fast/events/before-unload-remove-and-add-subframe.html
fast/events/before-unload-adopt-subframe-to-outside.html
Comment 6 Build Bot 2015-09-21 01:59:52 PDT
Created attachment 261642 [details]
Archive of layout-test-results from ews103 for mac-mavericks

The attached test failures were seen while running run-webkit-tests on the mac-ews.
Bot: ews103  Port: mac-mavericks  Platform: Mac OS X 10.9.5
Comment 7 Build Bot 2015-09-21 02:23:28 PDT
Comment on attachment 261639 [details]
Patch

Attachment 261639 [details] did not pass mac-wk2-ews (mac-wk2):
Output: http://webkit-queues.webkit.org/results/192591

New failing tests:
fast/events/before-unload-remove-and-add-subframe.html
fast/events/before-unload-adopt-subframe-to-outside.html
Comment 8 Build Bot 2015-09-21 02:23:31 PDT
Created attachment 261643 [details]
Archive of layout-test-results from ews107 for mac-mavericks-wk2

The attached test failures were seen while running run-webkit-tests on the mac-wk2-ews.
Bot: ews107  Port: mac-mavericks-wk2  Platform: Mac OS X 10.9.5
Comment 9 Hunseop Jeong 2015-09-21 18:16:40 PDT
Created attachment 261711 [details]
Patch
Comment 10 WebKit Commit Bot 2015-09-21 18:33:22 PDT
Attachment 261711 [details] did not pass style-queue:


ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:193:  Multi line control clauses should use braces.  [whitespace/braces] [4]
ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:777:  Multi line control clauses should use braces.  [whitespace/braces] [4]
ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:815:  Multi line control clauses should use braces.  [whitespace/braces] [4]
ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:816:  Multi line control clauses should use braces.  [whitespace/braces] [4]
ERROR: Source/WebCore/loader/archive/cf/LegacyWebArchive.cpp:250:  Multi line control clauses should use braces.  [whitespace/braces] [4]
Total errors found: 5 in 19 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 11 Hunseop Jeong 2015-09-21 19:19:04 PDT
(In reply to comment #10)
> Attachment 261711 [details] did not pass style-queue:
> 
> 
> ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:193:  Multi line control
> clauses should use braces.  [whitespace/braces] [4]
> ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:777:  Multi line control
> clauses should use braces.  [whitespace/braces] [4]
> ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:815:  Multi line control
> clauses should use braces.  [whitespace/braces] [4]
> ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:816:  Multi line control
> clauses should use braces.  [whitespace/braces] [4]
> ERROR: Source/WebCore/loader/archive/cf/LegacyWebArchive.cpp:250:  Multi
> line control clauses should use braces.  [whitespace/braces] [4]
> Total errors found: 5 in 19 files
> 
> 
> If any of these errors are false positives, please file a bug against
> check-webkit-style.


It looks false alarm. These modified codes don't need the braces.
Comment 12 Chris Dumez 2015-09-21 19:27:10 PDT
(In reply to comment #11)
> (In reply to comment #10)
> > Attachment 261711 [details] did not pass style-queue:
> > 
> > 
> > ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:193:  Multi line control
> > clauses should use braces.  [whitespace/braces] [4]
> > ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:777:  Multi line control
> > clauses should use braces.  [whitespace/braces] [4]
> > ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:815:  Multi line control
> > clauses should use braces.  [whitespace/braces] [4]
> > ERROR: Source/WebCore/loader/icon/IconDatabase.cpp:816:  Multi line control
> > clauses should use braces.  [whitespace/braces] [4]
> > ERROR: Source/WebCore/loader/archive/cf/LegacyWebArchive.cpp:250:  Multi
> > line control clauses should use braces.  [whitespace/braces] [4]
> > Total errors found: 5 in 19 files
> > 
> > 
> > If any of these errors are false positives, please file a bug against
> > check-webkit-style.
> 
> 
> It looks false alarm. These modified codes don't need the braces.

I don't think they are. This usually means you have extra spaces on the next line. You should remove those extra spaces.
Comment 13 Chris Dumez 2015-09-21 19:28:20 PDT
Comment on attachment 261711 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=261711&action=review

> Source/WebCore/loader/icon/IconDatabase.cpp:195
>              

For example, you probably have white spaces on this line.
Comment 14 Darin Adler 2015-09-21 19:30:24 PDT
Comment on attachment 261711 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=261711&action=review

> Source/WebCore/loader/DocumentLoader.cpp:1146
> +        RefPtr<ResourceLoader> loader = entry.key;

No need for the reference count churn here. Could just be:

    auto& loader = entry.key;

> Source/WebCore/loader/FrameLoader.cpp:1860
> +        for (auto& response : m_documentLoader->responses()) {

I think that in both the old and new code this is risky. What if sendRemainingDelegateMessages calls something that ends up calling back through to us and deletes the object currently in m_documentLoader or adds responses to the vector? We probably need to make a copy of the response vector and iterate that. Not sure how to test this.

> Source/WebCore/loader/appcache/ApplicationCache.cpp:156
> +    for (auto& whiteURL : m_onlineWhitelist) {

I think whitelistURL would be better than whiteURL.

> Source/WebCore/loader/appcache/ApplicationCacheGroup.cpp:734
> +    for (auto& loader : m_pendingMasterResourceLoaders)
> +        associateDocumentLoaderWithCache(loader, m_cacheBeingUpdated.get());
> +

Why the new extra blank line here? Please don’t include that.

> Source/WebCore/loader/appcache/ApplicationCacheHost.cpp:308
> +    for (auto& deferred : m_deferredEvents)

I think I’d call this “event” rather than “deferred”.

> Source/WebCore/loader/appcache/ApplicationCacheStorage.cpp:753
> +        for (auto& url : onlineWhitelist) {

I think whitelistURL is probably the best name for this, better than just url.

> Source/WebCore/loader/archive/cf/LegacyWebArchive.cpp:138
> +        RetainPtr<CFDictionaryRef> subresource = createPropertyListRepresentation(resource.get(), Subresource);
>          if (subresource)

I’d suggest putting the RetainPtr inside the if.

> Source/WebCore/loader/archive/cf/LegacyWebArchive.cpp:149
> +        RetainPtr<CFDictionaryRef> subframeArchive = createPropertyListRepresentation(subframe.get());
>          if (subframeArchive)

I’d suggest putting the RetainPtr inside the if.

> Source/WebCore/loader/cache/CachedResourceLoader.cpp:1093
> +    for (auto& res : m_preloads) {

Would be nice to rename this resource instead of "res".

> Source/WebCore/loader/icon/IconDatabase.cpp:817
> +    for (auto& loader : m_loadersPendingDecision)
> +        if (loader->refCount() > 1)
> +            loader->iconLoadDecisionAvailable();

Should have braces for WebKit coding style.
Comment 15 Hunseop Jeong 2015-09-21 23:51:46 PDT
(In reply to comment #13)
> Comment on attachment 261711 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=261711&action=review
> 
> > Source/WebCore/loader/icon/IconDatabase.cpp:195
> >              
> 
> For example, you probably have white spaces on this line.

Oops,, I didn't see the white spaces.. I will remove the white spaces.
Comment 16 Hunseop Jeong 2015-09-21 23:57:35 PDT
Comment on attachment 261711 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=261711&action=review

>> Source/WebCore/loader/FrameLoader.cpp:1860
>> +        for (auto& response : m_documentLoader->responses()) {
> 
> I think that in both the old and new code this is risky. What if sendRemainingDelegateMessages calls something that ends up calling back through to us and deletes the object currently in m_documentLoader or adds responses to the vector? We probably need to make a copy of the response vector and iterate that. Not sure how to test this.

You mean, We need to make a copy of the response vector to escape the risk.
Like as : 

        Vector<ResourceResponse> responses;
        responses.reserveCapacity(m_documentLoader->responses().size());
        for (auto& response : m_documentLoader->responses())
            responses.append(response);

        for (auto& response : responses) {
            ...

>> Source/WebCore/loader/appcache/ApplicationCache.cpp:156
>> +    for (auto& whiteURL : m_onlineWhitelist) {
> 
> I think whitelistURL would be better than whiteURL.

Okay, Naming is very hard work.
Comment 17 Darin Adler 2015-09-22 08:52:53 PDT
Comment on attachment 261711 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=261711&action=review

>>> Source/WebCore/loader/FrameLoader.cpp:1860
>>> +        for (auto& response : m_documentLoader->responses()) {
>> 
>> I think that in both the old and new code this is risky. What if sendRemainingDelegateMessages calls something that ends up calling back through to us and deletes the object currently in m_documentLoader or adds responses to the vector? We probably need to make a copy of the response vector and iterate that. Not sure how to test this.
> 
> You mean, We need to make a copy of the response vector to escape the risk.
> Like as : 
> 
>         Vector<ResourceResponse> responses;
>         responses.reserveCapacity(m_documentLoader->responses().size());
>         for (auto& response : m_documentLoader->responses())
>             responses.append(response);
> 
>         for (auto& response : responses) {
>             ...

Yes, copying the response vector is one thing that might help. It also might have a performance cost or create other problems, so I think we need to look into testing this, not just writing code that looks right.

If we do want to copy it, it’s actually much easier to copy than the code above. It’s this one-liner:

    auto responsesCopy = m_documentLoader->responses();

No need for a loop with reserveCapacity and append. (And if we did write the loop, it should be with reserveInitialCapacity and uncheckedAppend instead.)
Comment 18 Hunseop Jeong 2015-09-23 02:17:46 PDT
Created attachment 261809 [details]
Patch
Comment 19 Hunseop Jeong 2015-09-23 05:53:05 PDT
Created attachment 261821 [details]
PatchWithoutCopying
Comment 20 Hunseop Jeong 2015-09-23 05:59:07 PDT
(In reply to comment #17)
> Comment on attachment 261711 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=261711&action=review
> 
> >>> Source/WebCore/loader/FrameLoader.cpp:1860
> >>> +        for (auto& response : m_documentLoader->responses()) {
> >> 
> >> I think that in both the old and new code this is risky. What if sendRemainingDelegateMessages calls something that ends up calling back through to us and deletes the object currently in m_documentLoader or adds responses to the vector? We probably need to make a copy of the response vector and iterate that. Not sure how to test this.
> > 
> > You mean, We need to make a copy of the response vector to escape the risk.
> > Like as : 
> > 
> >         Vector<ResourceResponse> responses;
> >         responses.reserveCapacity(m_documentLoader->responses().size());
> >         for (auto& response : m_documentLoader->responses())
> >             responses.append(response);
> > 
> >         for (auto& response : responses) {
> >             ...
> 
> Yes, copying the response vector is one thing that might help. It also might
> have a performance cost or create other problems, so I think we need to look
> into testing this, not just writing code that looks right.
> 
> If we do want to copy it, it’s actually much easier to copy than the code
> above. It’s this one-liner:
> 
>     auto responsesCopy = m_documentLoader->responses();
> 
> No need for a loop with reserveCapacity and append. (And if we did write the
> loop, it should be with reserveInitialCapacity and uncheckedAppend instead.)
Other comments are done.

How to look into testing this?
Is it covered by existing layoutTest or PerformanceTest?

If we need the deep testing, could you upload this patch without the copying code?
Comment 21 Darin Adler 2015-09-23 08:16:34 PDT
(In reply to comment #20)
> How to look into testing this?

That’s an excellent question. I don’t know exactly how to test it, other than the suggestions I made above about sendRemainingDelegateMessages. We somehow have to make a test case where those delegate messages trigger a change in the frame loader, probably by loading a new page in the frame, but with a load that is as synchronous as possible (perhaps loading about:blank).

> Is it covered by existing layoutTest or PerformanceTest?

Definitely not, because if there is a bug here, it already existed in the old code.

> If we need the deep testing, could you upload this patch without the copying
> code?

Sure, we can do refactoring without fixing this, but someone should really be following up to explore whether this is a bug.
Comment 22 Hunseop Jeong 2015-09-23 18:58:30 PDT
(In reply to comment #21)
> (In reply to comment #20)
> > How to look into testing this?
> 
> That’s an excellent question. I don’t know exactly how to test it, other
> than the suggestions I made above about sendRemainingDelegateMessages. We
> somehow have to make a test case where those delegate messages trigger a
> change in the frame loader, probably by loading a new page in the frame, but
> with a load that is as synchronous as possible (perhaps loading about:blank).
> 
> > Is it covered by existing layoutTest or PerformanceTest?
> 
> Definitely not, because if there is a bug here, it already existed in the
> old code.
> 
> > If we need the deep testing, could you upload this patch without the copying
> > code?
> 
> Sure, we can do refactoring without fixing this, but someone should really
> be following up to explore whether this is a bug.
Thanks. I also try to explore whether this is a bug.
Comment 23 Hunseop Jeong 2015-09-29 21:20:25 PDT
Created attachment 262139 [details]
Patch
Comment 24 WebKit Commit Bot 2015-09-29 22:54:16 PDT
Comment on attachment 262139 [details]
Patch

Clearing flags on attachment: 262139

Committed r190338: <http://trac.webkit.org/changeset/190338>
Comment 25 WebKit Commit Bot 2015-09-29 22:54:23 PDT
All reviewed patches have been landed.  Closing bug.