Bug 123707 - [Cocoa] Wrappers' -copyWithZone: should copy if the collection is mutable
Summary: [Cocoa] Wrappers' -copyWithZone: should copy if the collection is mutable
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKit2 (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: mitz
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-11-03 10:35 PST by mitz
Modified: 2013-11-03 18:59 PST (History)
2 users (show)

See Also:


Attachments
If the collection is mutable, make a copy (2.65 KB, patch)
2013-11-03 10:38 PST, mitz
no flags Details | Formatted Diff | Diff
If the collection is mutable, make a copy (2.65 KB, patch)
2013-11-03 12:30 PST, mitz
ggaren: review+
Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description mitz 2013-11-03 10:35:49 PST
[Cocoa] Wrappers' -copyWithZone: should copy if the collection is mutable
Comment 1 mitz 2013-11-03 10:38:16 PST
Created attachment 215870 [details]
If the collection is mutable, make a copy
Comment 2 Geoffrey Garen 2013-11-03 12:25:33 PST
Comment on attachment 215870 [details]
If the collection is mutable, make a copy

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

> Source/WebKit2/Shared/ImmutableArray.h:68
> +    const Vector<RefPtr<APIObject>> entries() { return m_entries; }

Should this be Vector& instead of Vector? As written, each call to entries() will make a copy.

> Source/WebKit2/Shared/Cocoa/WKNSArray.mm:63
> +    auto entries = reinterpret_cast<ImmutableArray*>(&_array)->entries();

If you had not used auto in this context, you would have use an explicit reference type, in which case the compiler would have caught the extra copy mistake (assuming I am right that it was a mistake).
Comment 3 mitz 2013-11-03 12:28:32 PST
Comment on attachment 215870 [details]
If the collection is mutable, make a copy

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

>> Source/WebKit2/Shared/ImmutableArray.h:68
>> +    const Vector<RefPtr<APIObject>> entries() { return m_entries; }
> 
> Should this be Vector& instead of Vector? As written, each call to entries() will make a copy.

Yes.

>> Source/WebKit2/Shared/Cocoa/WKNSArray.mm:63
>> +    auto entries = reinterpret_cast<ImmutableArray*>(&_array)->entries();
> 
> If you had not used auto in this context, you would have use an explicit reference type, in which case the compiler would have caught the extra copy mistake (assuming I am right that it was a mistake).

This needs to be a copy, not a reference.
Comment 4 mitz 2013-11-03 12:30:49 PST
Created attachment 215872 [details]
If the collection is mutable, make a copy
Comment 5 Geoffrey Garen 2013-11-03 12:37:03 PST
Comment on attachment 215872 [details]
If the collection is mutable, make a copy

r=me
Comment 6 Geoffrey Garen 2013-11-03 12:38:55 PST
> >> Source/WebKit2/Shared/Cocoa/WKNSArray.mm:63
> >> +    auto entries = reinterpret_cast<ImmutableArray*>(&_array)->entries();
> > 
> > If you had not used auto in this context, you would have use an explicit reference type, in which case the compiler would have caught the extra copy mistake (assuming I am right that it was a mistake).
> 
> This needs to be a copy, not a reference.

What I meant was that, if you had used

const ImmutableArray::VectorType& entries = ...

you would have gotten an error, due to taking a reference to a temporary, which might have alerted you to the accidental copy caused by the return type of entries().

I think about stuff like this because I'm still not sure when we should use auto, and when we should not.
Comment 7 mitz 2013-11-03 12:41:13 PST
(In reply to comment #6)
> > >> Source/WebKit2/Shared/Cocoa/WKNSArray.mm:63
> > >> +    auto entries = reinterpret_cast<ImmutableArray*>(&_array)->entries();
> > > 
> > > If you had not used auto in this context, you would have use an explicit reference type, in which case the compiler would have caught the extra copy mistake (assuming I am right that it was a mistake).
> > 
> > This needs to be a copy, not a reference.
> 
> What I meant was that, if you had used
> 
> const ImmutableArray::VectorType& entries = ...
> 
> you would have gotten an error, due to taking a reference to a temporary, which might have alerted you to the accidental copy caused by the return type of entries().

But, regardless of my mistake in the definition of entries(), I wouldn’t have used the above type for the local, since I did want a copy, not a reference.
Comment 8 mitz 2013-11-03 12:41:27 PST
Committed <http://trac.webkit.org/r158538>.
Comment 9 Geoffrey Garen 2013-11-03 12:48:42 PST
> But, regardless of my mistake in the definition of entries(), I wouldn’t have used the above type for the local, since I did want a copy, not a reference.

Uh oh. Since the patch you committed passes a reference from _array (and _dictionary) to adopt(), and adopt() calls swap() -- does this mean that -copyWithZone will remove all entries from self?

I am demonstrating my limitations as a reviewer :(.
Comment 10 mitz 2013-11-03 12:56:25 PST
(In reply to comment #9)
> > But, regardless of my mistake in the definition of entries(), I wouldn’t have used the above type for the local, since I did want a copy, not a reference.
> 
> Uh oh. Since the patch you committed passes a reference from _array (and _dictionary) to adopt(), and adopt() calls swap() -- does this mean that -copyWithZone will remove all entries from self?

The locals’ types aren’t const references. They are a Vector and a MapType.
Comment 11 mitz 2013-11-03 18:57:26 PST
(In reply to comment #8)
> Committed <http://trac.webkit.org/r158538>.

I shouldn’t have landed this patch without a WebKit2 owner’s permission. I reverted the change in <http://trac.webkit.org/r158552>.
Comment 12 mitz 2013-11-03 18:59:34 PST
…and committed <http://trac.webkit.org/r158553>.