Say
struct Teste: Codable {
let a: Int
lazy var x: Int = {
print("I simply modified this Teste merchandise")
return a + 1000
}()
}
and also you
var hugeData: [Teste] // 750 million of those
So it is one thing like your knowledge from the cloud, held in a singleton or such.
Say then you definitely occur to
let see: [Teste] = hugeData.filter{ .. }
// 4 of those. we'll say one in all them occurs to be hugeData[109333072]
possibly to show in a view or such. Subsequent! Whereas see
exists, you occur to for some purpose
let width = hugeData[109333072].x
At that second, it has to copy-due-to-write all of hugeData
, simply because the silly little see
exists at the moment.
- Or it may very well be that Swift’s copy-on-write mechanisms are good sufficient, in that scenario, to really do the copy-due-to-write on the tiny one? Does anybody know if that is so?
It solely simply occurred to me that an enormous hazard (if you happen to cope with huge quantities of knowledge), that if one thing modifications within the massive authentic array, when you simply occur to have some pissant little copy-on-write array in existence, it is an enormous efficiency woe.
(And/or, as talked about within the bullet level, possibly Swift offers with this by really doing the copy-due-to-write on the toddler??)
Briefly, is it right that duplicate on write is utilized to (all) the “authentic” array if a change is made to that array whereas there occurs to be a replica extant?
This is a brief self-explanatory demo …
struct Teste: Codable {
let a: Int
lazy var x: Int = {
print("I simply calculated it", a + 1000)
return a + 1000
}()
}
print("yo")
tt = [Teste(a:7), Teste(a:8), Teste(a:9)]
let tt2 = tt.filter{ $0.a > 8 }
print(tt)
print(tt2)
print("sure, nonetheless nil in tt2")
print("go..", tt[2].x)
print("go once more..", tt[2].x)
print(tt)
print(tt2)
print("sure, copy on write was? utilized to the unique enormous array")
print("ie YES, nonetheless nil in tt2")
let tt3 = tt.filter{ $0.a > 8 }
print(tt)
print(tt3)
print("sure, the set worth appears to hold by way of to tt3")
outcome
yo
[.Teste(a: 7, $__lazy_storage_$_x: nil), .Teste(a: 8, $__lazy_storage_$_x: nil), .Teste(a: 9, $__lazy_storage_$_x: nil)]
[.Teste(a: 9, $__lazy_storage_$_x: nil)]
sure, nonetheless nil in tt2
I simply calculated it 1009
go.. 1009
go once more.. 1009
[.Teste(a: 7, $__lazy_storage_$_x: nil), .Teste(a: 8, $__lazy_storage_$_x: nil), .Teste(a: 9, $__lazy_storage_$_x: Optional(1009))]
[.Teste(a: 9, $__lazy_storage_$_x: nil)]
sure, copy on write was? utilized to the unique enormous array
ie YES, nonetheless nil in tt2
[.Teste(a: 7, $__lazy_storage_$_x: nil), .Teste(a: 8, $__lazy_storage_$_x: nil), .Teste(a: 9, $__lazy_storage_$_x: Optional(1009))]
[.Teste(a: 9, $__lazy_storage_$_x: Optional(1009))]
sure, the set worth appears to hold by way of to tt3
So in such a case wouldn’t it copy the 750 million merchandise array?