<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Oct 12, 2016, at 9:32 AM, plx via swift-evolution &lt;<a href="mailto:swift-evolution@swift.org" class="">swift-evolution@swift.org</a>&gt; wrote:</div><br class="Apple-interchange-newline"><div class=""><meta http-equiv="Content-Type" content="text/html charset=utf-8" class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">The issue addressed is real; I’m not sure this is the best approach.&nbsp;</div><div class=""><br class=""></div><div class="">In particular, unless I’m missing something obvious, the ownership strategy here would have to be:</div><div class=""><br class=""></div><div class=""><div class="">- `DictionaryKeys` and `DictionaryValues` would each induce the expected +1 retain on the underlying storage</div><div class="">- `DictionaryValues`’s mutations avoid triggering COW on the underlying storage by skipping the usual ownership check</div></div><div class=""><br class=""></div><div class="">…as otherwise it’s unclear how you’d do those in-place mutations (and this seems to be how the implementation works...is that correct?).</div></div></div></blockquote><div><br class=""></div><div>That's not quite right—when you access these views through the dictionary, they do not increment the storage retain count. This is the way slicing and views currently work on other mutable types. For example, when you reverse a slice of an array in-place, the slice doesn't get its own duplicate storage:</div><div><br class=""></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div><div>var a = Array(1...10)</div></div><div><div>a[0..&lt;5].reverse()</div></div><div><div>a == [5, 4, 3, 2, 1, 6, 7, 8, 9, 10]</div></div></blockquote><div><div><br class=""></div><div>However, if you create a new variable out of the slice and reverse <i class="">that</i>, the slice does get its own storage:</div><div><br class=""></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div><div>var b = Array(1...10)</div></div><div><div>var bSlice = b[0..&lt;5]</div></div><div><div>bSlice.reverse()</div></div><div><div>bSlice == [5, 4, 3, 2, 1]</div></div><div><div>b == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</div></div></blockquote><div><div><br class=""></div><div>Strings and their views work the same way:</div><div><br class=""></div></div><blockquote style="margin: 0 0 0 40px; border: none; padding: 0px;" class=""><div><div><div>var s = "abcdefg"</div></div></div><div><div><div>s.characters.append("H") &nbsp; // storage updated in place</div></div></div><div><div><div>s == "abcdefgH"</div></div></div><div><div><div><br class=""></div></div></div><div><div><div>var sChars = s.characters &nbsp;// no copy yet</div></div></div><div><div><div>sChars.removeLast() &nbsp; &nbsp; // sChars gets its own copy before the mutation</div></div></div><div><div><div>s == "abcdefgH"</div></div></div><div><div><div>String(sChars) == "abcdefg"</div></div></div><div><div><div><br class=""></div></div></div><div><div><div><div>var t = s &nbsp; // no copy yet</div></div></div></div><div><div><div><div class="">t.characters.removeLast() &nbsp;// t gets a new copy here</div></div></div></div><div><div><div>s == "abcdefgH"</div></div></div><div><div><div>t == "abcdefg"</div></div></div></blockquote><div class=""><br class=""></div><div class="">I don't know the name of the compiler feature that enables this, but it's a critical part of the way views and slices work.</div><br class=""><div><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">With that design, it seems like you’d wind up allowing things like the below:</div><div class=""><br class=""></div><div class="">&nbsp; // example A</div><div class="">&nbsp; let foo = [ “abc”: [1,2,3], “efg”: [4,5,6] ]</div><div class="">&nbsp; let bar = foo // shared storage, no COW</div><div class="">&nbsp; foo.values[foo.index(of: “abc”)!].append(789) // update shared storage, no COW</div><div class=""><br class=""></div><div class="">&nbsp; // shared storage mutated,</div><div class="">&nbsp; // despite (a) both being `let` and (b) only foo.values getting touched</div><div class="">&nbsp; foo[“abc”] // [1, 2, 3, 789]</div><div class="">&nbsp; bar[“abc”] // [1, 2, 3, 789]</div></div></div></blockquote><div><br class=""></div><div>Example A isn't allowed—if foo and bar are both immutable, both of their `values` collections are also immutable, so there's no way to modify their shared storage.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class="">&nbsp; // example B</div><div class="">&nbsp; var foo = [ “abc”: [1,2,3], “efg”: [4,5,6] ]</div><div class="">&nbsp; var bar = foo // shared storage, no COW</div><div class="">&nbsp; foo.values[foo.index(of: “abc”)!].append(789)</div><div class=""><br class=""></div><div class="">&nbsp; // shared storage mutated only foo.values getting touched</div><div class="">&nbsp; foo[“abc”] // [1, 2, 3, 789]</div><div class="">&nbsp; bar[“abc”] // [1, 2, 3, 789]</div></div></div></div></blockquote><div><br class=""></div><div>Example B is incorrect—the mutation at `foo.values[...].append(789)` triggers a copy of the entire dictionary's underlying storage before allowing the mutation, since it knows that storage isn't uniquely referenced.</div><div><br class=""></div><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class=""><div class="">&nbsp; // example C</div><div class="">&nbsp; var foo = [ “abc”: [1,2,3], “efg”: [4,5,6] ]</div><div class="">&nbsp; var bar = foo&nbsp;</div><div class="">&nbsp; bar[“abc”] = [1, 2, 3, 4] // COW triggered here, no shared storage</div><div class="">&nbsp; foo.values[foo.index(of: “abc”)!].append(789)</div><div class=""><br class=""></div><div class="">&nbsp; // only `foo`’s storage mutated, b/c change to `bar` triggered COW</div><div class="">&nbsp; foo[“abc”] // [1, 2, 3, 789]</div><div class="">&nbsp; bar[“abc”] // [1, 2, 3, 4]</div></div></div></div></blockquote><div><br class=""></div><div>This is the current behavior and would remain the same after the proposed the changes.</div><br class=""><blockquote type="cite" class=""><div class=""><div style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><div class="">…where both A (by itself) and the B/C contrast seem very unwelcome.</div><div class=""><br class=""></div><div class="">Also, even if we assume we only ever make *responsible* use, having the stdlib include such directly-mutating views would seem likely to complicate any future concurrency plans.</div><div class=""><br class=""></div><div class="">To reiterate, I think the issue being addressed here is extremely important…I just don’t think I can get behind this type of solution (unless I’m grossly misunderstanding its mechanics).</div></div></div></blockquote><div><br class=""></div>Nate</div><div><br class=""></div></body></html>