Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(293)

Issue 1319783003: Add static ensure() methods to unmodifiable views.

Created:
5 years, 3 months ago by nweiz
Modified:
5 years, 2 months ago
CC:
reviews_dartlang.org
Base URL:
git@github.com:dart-lang/sdk.git@master
Target Ref:
refs/heads/master
Visibility:
Public.

Description

Add static ensure() methods to unmodifiable views. A common pattern in code with (largely-)immutable objects is to have a "change" or "replace" method that constructs a new version of that object with a few fields changed. Since the object is immutable, it tends to use Unmodifiable views for any collections it exposes. Creating another layer of wrapping each time the object changes is a performance and memory issue; this change makes it much easier to avoid re-wrapping when it's not necessary. R=lrn@google.com

Patch Set 1 #

Total comments: 21

Patch Set 2 : Code review changes #

Unified diffs Side-by-side diffs Delta from patch set Stats (+36 lines, -2 lines) Patch
M CHANGELOG.md View 1 chunk +5 lines, -0 lines 0 comments Download
M sdk/lib/collection/collections.dart View 1 1 chunk +13 lines, -0 lines 0 comments Download
M sdk/lib/collection/maps.dart View 1 1 chunk +15 lines, -2 lines 0 comments Download
M tests/corelib/map_test.dart View 2 chunks +3 lines, -0 lines 0 comments Download

Messages

Total messages: 23 (4 generated)
nweiz
5 years, 3 months ago (2015-08-26 20:28:51 UTC) #1
kevmoo
DBC: test for List, too?
5 years, 3 months ago (2015-08-26 21:46:06 UTC) #3
Lasse Reichstein Nielsen
LGTM if we rename the function. https://codereview.chromium.org/1319783003/diff/1/sdk/lib/collection/collections.dart File sdk/lib/collection/collections.dart (right): https://codereview.chromium.org/1319783003/diff/1/sdk/lib/collection/collections.dart#newcode20 sdk/lib/collection/collections.dart:20: * [UnmodifiableListView], this ...
5 years, 3 months ago (2015-08-27 07:59:04 UTC) #4
nweiz
https://codereview.chromium.org/1319783003/diff/1/sdk/lib/collection/collections.dart File sdk/lib/collection/collections.dart (right): https://codereview.chromium.org/1319783003/diff/1/sdk/lib/collection/collections.dart#newcode20 sdk/lib/collection/collections.dart:20: * [UnmodifiableListView], this avoids re-wrapping it. On 2015/08/27 07:59:04, ...
5 years, 3 months ago (2015-08-27 19:25:38 UTC) #5
nweiz
Code review changes
5 years, 3 months ago (2015-08-27 19:34:56 UTC) #6
sra1
https://codereview.chromium.org/1319783003/diff/1/sdk/lib/collection/collections.dart File sdk/lib/collection/collections.dart (right): https://codereview.chromium.org/1319783003/diff/1/sdk/lib/collection/collections.dart#newcode22 sdk/lib/collection/collections.dart:22: static List ensure(Iterable source) => How does this type-check ...
5 years, 3 months ago (2015-08-27 20:24:53 UTC) #8
nweiz
https://codereview.chromium.org/1319783003/diff/1/sdk/lib/collection/collections.dart File sdk/lib/collection/collections.dart (right): https://codereview.chromium.org/1319783003/diff/1/sdk/lib/collection/collections.dart#newcode22 sdk/lib/collection/collections.dart:22: static List ensure(Iterable source) => On 2015/08/27 20:24:52, sra1 ...
5 years, 3 months ago (2015-08-28 00:41:29 UTC) #9
Lasse Reichstein Nielsen
https://codereview.chromium.org/1319783003/diff/1/sdk/lib/collection/collections.dart File sdk/lib/collection/collections.dart (right): https://codereview.chromium.org/1319783003/diff/1/sdk/lib/collection/collections.dart#newcode22 sdk/lib/collection/collections.dart:22: static List ensure(Iterable source) => UmodifiableListView.unmodifiable isn't very clear ...
5 years, 3 months ago (2015-08-31 09:08:24 UTC) #10
Bob Nystrom
https://codereview.chromium.org/1319783003/diff/1/sdk/lib/collection/collections.dart File sdk/lib/collection/collections.dart (right): https://codereview.chromium.org/1319783003/diff/1/sdk/lib/collection/collections.dart#newcode22 sdk/lib/collection/collections.dart:22: static List ensure(Iterable source) => On 2015/08/27 07:59:04, Lasse ...
5 years, 3 months ago (2015-08-31 20:42:45 UTC) #12
nweiz
On 2015/08/31 20:42:45, Bob Nystrom wrote: > https://codereview.chromium.org/1319783003/diff/1/sdk/lib/collection/collections.dart > File sdk/lib/collection/collections.dart (right): > > https://codereview.chromium.org/1319783003/diff/1/sdk/lib/collection/collections.dart#newcode22 ...
5 years, 3 months ago (2015-08-31 20:49:48 UTC) #13
sra1
+leaf to address if this is a problem for DDC.
5 years, 3 months ago (2015-08-31 23:22:44 UTC) #15
Leaf
On 2015/08/31 23:22:44, sra1 wrote: > +leaf to address if this is a problem for ...
5 years, 3 months ago (2015-08-31 23:59:30 UTC) #16
sra1
On 2015/08/31 23:59:30, Leaf wrote: > On 2015/08/31 23:22:44, sra1 wrote: > > +leaf to ...
5 years, 2 months ago (2015-10-06 18:20:36 UTC) #17
sra1
Why can't we change the constructors to do this? class UnmodifiableListView<E> ... { UnmodifiableListView._(this._source); factory ...
5 years, 2 months ago (2015-10-06 19:19:22 UTC) #18
nweiz
On 2015/10/06 19:19:22, sra1 wrote: > Why can't we change the constructors to do this? ...
5 years, 2 months ago (2015-10-06 20:47:12 UTC) #19
sra1
On 2015/10/06 20:47:12, nweiz wrote: > On 2015/10/06 19:19:22, sra1 wrote: > > Why can't ...
5 years, 2 months ago (2015-10-06 22:42:04 UTC) #20
Lasse Reichstein Nielsen
https://codereview.chromium.org/1319783003/diff/1/sdk/lib/collection/collections.dart File sdk/lib/collection/collections.dart (right): https://codereview.chromium.org/1319783003/diff/1/sdk/lib/collection/collections.dart#newcode28 sdk/lib/collection/collections.dart:28: UnmodifiableListView(Iterable<E> source) : _source = source; Because the UnmodifiableListView ...
5 years, 2 months ago (2015-10-07 07:40:40 UTC) #21
kevmoo
DBQ Could we just update the 'unmodifiable' ctors on List and Map to return 'other' ...
5 years, 2 months ago (2015-10-20 15:27:50 UTC) #22
nweiz
5 years, 2 months ago (2015-10-20 20:20:15 UTC) #23
On 2015/10/20 15:27:50, kevmoo wrote:
> DBQ
> 
> Could we just update the 'unmodifiable' ctors on List and Map to return
'other'
> if it is unmodifiable?
> 
> We can keep the generic. We don't introduce a new API.

If you want to be nitty about it (which we usually are for core library
changes), this would be a breaking change. The following code changes behavior:

    var l1 = [];
    var l2 = new UnmodifiableListView(l1);
    var l3 = new List.unmodifiable(l2);
    l1.add(1);
    print(l3);

This currently prints "[]" because the unmodifiable list copies its input, but
under the proposed semantics it would print "[1]". It's not a huge stretch to
imagine that there's code that relies on the copying behavior.

This is why I put the function on UnmodifiableListView/UnmodifiableMapView in
the first place—that way it's clear that it returns a *view*, not a copy.

Powered by Google App Engine
This is Rietveld 408576698