Skip to content

Instantly share code, notes, and snippets.

@groue
Last active October 5, 2022 07:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save groue/fdd43e3665fb334fe8e57fe138373233 to your computer and use it in GitHub Desktop.
Save groue/fdd43e3665fb334fe8e57fe138373233 to your computer and use it in GitHub Desktop.
import SwiftUI
struct MyCollection: RandomAccessCollection {
let startIndex = 0
let endIndex = 1000
subscript(index: Int) -> Int {
print("Access \(index)")
return index
}
}
struct ContentView: View {
var body: some View {
TabView {
NavigationStack {
// Wrapped in a scroll view, LazyVStack only accesses the elements it needs
ScrollView {
LazyVStack {
ForEach(MyCollection(), id: \.self) { index in
let _ = { print("LazyVStack renders \(index)") }()
Text("\(index)").padding()
}
}
}.navigationBarTitle("LazyVStack")
}.tabItem { Text("LazyVStack") }
NavigationStack {
// List always accesses all elements
List(MyCollection(), id: \.self) { index in
let _ = { print("List renders \(index)") }()
Text("\(index)")
}.navigationBarTitle("List")
}.tabItem { Text("List") }
}
}
}
# Indexes accessed during a "scroll to top" gesture performed
# from a LazyVStack scrolled to the end of a list of
# 1_000_000 elements:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
13378
13379
13380
13381
13382
13383
13384
13385
13386
13387
13388
13389
13390
13391
13392
13393
15514
15515
15516
15517
15518
15519
15520
15521
15522
15523
15524
15525
15526
15527
15528
15529
17999
18000
18001
18002
18003
18004
18005
18006
18007
18008
18009
18010
18011
18012
18013
18014
20890
20891
20892
20893
20894
20895
20896
20897
20898
20899
20900
20901
20902
20903
20904
20905
24254
24255
24256
24257
24258
24259
24260
24261
24262
24263
24264
24265
24266
24267
24268
24269
28172
28173
28174
28175
28176
28177
28178
28179
28180
28181
28182
28183
28184
28185
28186
28187
32737
32738
32739
32740
32741
32742
32743
32744
32745
32746
32747
32748
32749
32750
32751
32752
38058
38059
38060
38061
38062
38063
38064
38065
38066
38067
38068
38069
38070
38071
38072
38073
44266
44267
44268
44269
44270
44271
44272
44273
44274
44275
44276
44277
44278
44279
44280
44281
51510
51511
51512
51513
51514
51515
51516
51517
51518
51519
51520
51521
51522
51523
51524
51525
59971
59972
59973
59974
59975
59976
59977
59978
59979
59980
59981
59982
59983
59984
59985
59986
69859
69860
69861
69862
69863
69864
69865
69866
69867
69868
69869
69870
69871
69872
69873
69874
81423
81424
81425
81426
81427
81428
81429
81430
81431
81432
81433
81434
81435
81436
81437
81438
94958
94959
94960
94961
94962
94963
94964
94965
94966
94967
94968
94969
94970
94971
94972
94973
110812
110813
110814
110815
110816
110817
110818
110819
110820
110821
110822
110823
110824
110825
110826
110827
129399
129400
129401
129402
129403
129404
129405
129406
129407
129408
129409
129410
129411
129412
129413
129414
151210
151211
151212
151213
151214
151215
151216
151217
151218
151219
151220
151221
151222
151223
151224
151225
176829
176830
176831
176832
176833
176834
176835
176836
176837
176838
176839
176840
176841
176842
176843
176844
206954
206955
206956
206957
206958
206959
206960
206961
206962
206963
206964
206965
206966
206967
206968
206969
242471
242472
242473
242474
242475
242476
242477
242478
242479
242480
242481
242482
242483
242484
242485
242486
284319
284320
284321
284322
284323
284324
284325
284326
284327
284328
284329
284330
284331
284332
284333
284334
333742
333743
333744
333745
333746
333747
333748
333749
333750
333751
333752
333753
333754
333755
333756
333757
392213
392214
392215
392216
392217
392218
392219
392220
392221
392222
392223
392224
392225
392226
392227
392228
461533
461534
461535
461536
461537
461538
461539
461540
461541
461542
461543
461544
461545
461546
461547
461548
543911
543912
543913
543914
543915
543916
543917
543918
543919
543920
543921
543922
543923
543924
543925
543926
642088
642089
642090
642091
642092
642093
642094
642095
642096
642097
642098
642099
642100
642101
642102
642103
759639
759640
759641
759642
759643
759644
759645
759646
759647
759648
759649
759650
759651
759652
759653
759654
901068
901069
901070
901071
901072
901073
901074
901075
901076
901077
901078
901079
901080
901081
901082
901083
999986
999987
999988
999989
999990
999991
999992
999993
999994
999995
999996
999997
999998
999999
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment