{"id":3265,"date":"2023-06-09T19:40:05","date_gmt":"2023-06-09T23:40:05","guid":{"rendered":"https:\/\/michaelrowe01.com\/?p=3265"},"modified":"2023-06-09T19:40:08","modified_gmt":"2023-06-09T23:40:08","slug":"demystify-swiftui-performance","status":"publish","type":"post","link":"https:\/\/michaelrowe01.com\/index.php\/blog\/demystify-swiftui-performance\/","title":{"rendered":"Demystify SwiftUI Performance"},"content":{"rendered":"\n<figure class=\"wp-block-image size-large\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" width=\"660\" height=\"371\" data-attachment-id=\"3266\" data-permalink=\"https:\/\/michaelrowe01.com\/index.php\/blog\/demystify-swiftui-performance\/attachment\/%ef%a3%bfwwdc23-34\/\" data-orig-file=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-34.png?fit=1920%2C1080&amp;ssl=1\" data-orig-size=\"1920,1080\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"\uf8ffWWDC23-34\" data-image-description=\"\" data-image-caption=\"\" data-large-file=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-34.png?fit=660%2C371&amp;ssl=1\" src=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-34.png?resize=660%2C371&#038;ssl=1\" alt=\"\" class=\"wp-image-3266\" srcset=\"https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-34.png?resize=1024%2C576&amp;ssl=1 1024w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-34.png?resize=300%2C169&amp;ssl=1 300w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-34.png?resize=768%2C432&amp;ssl=1 768w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-34.png?resize=1536%2C864&amp;ssl=1 1536w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-34.png?w=1920&amp;ssl=1 1920w, https:\/\/i0.wp.com\/michaelrowe01.com\/wp-content\/uploads\/2023\/06\/%EF%A3%BFWWDC23-34.png?w=1320&amp;ssl=1 1320w\" sizes=\"auto, (max-width: 660px) 100vw, 660px\" \/><\/figure>\n\n\n\n<p>Building a mental model for performance in SwiftUI<\/p>\n\n\n\n<p>Performance feedback Loop<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Symptoms &#8211; slowness, broken animation, or spinner<\/li>\n\n\n\n<li>Measure &#8211; to get data<\/li>\n\n\n\n<li>Identify cause<\/li>\n\n\n\n<li>Optimize to fix the root cause &#8211; then re-measure to verify<\/li>\n\n\n\n<li>Resolve the process<\/li>\n<\/ul>\n\n\n\n<p>Pre-requisite &nbsp;<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Identify<\/li>\n\n\n\n<li>View Lifetime and Value<\/li>\n<\/ul>\n\n\n\n<p>Watch Demystify SwiftUI from WWDC. 2021<\/p>\n\n\n\n<p>Dependencies<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Understanding how a View is dependent on various views and their dependencies &#8211; all views ultimately resolve to a leaf view<\/li>\n\n\n\n<li>The update process can be understand in the View Graph of dependencies, this includes Dynamic Properties (like @Environment property values), the process is recurves to go thru all the nodes which require updates.&nbsp; This will replicate down thru to all the leafs.<\/li>\n\n\n\n<li>To improve the process you can use _printChanges method in SwiftUI, you can set a break point and look at \u201cexpression Self._printChanges()\u201d in lldb.&nbsp; This is a debugging tool for best effort understanding of why a view changed.&nbsp; You can also add a let _ = Self._printChanges() and it will write to the view console.&nbsp; You MUST remove this code before submitting to the App Store<\/li>\n\n\n\n<li>By extracting the views to only handle the specific item you will can get better updates<\/li>\n\n\n\n<li>You can use @Observable which will also help reduce dependencies<\/li>\n<\/ul>\n\n\n\n<p>Faster View updates<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Analyze Hangs in Instruments from WWDC23 should be review ed<\/li>\n\n\n\n<li>There is a tech talk on Hitches<\/li>\n\n\n\n<li>They both originate from a slow update (like data filtering)<\/li>\n\n\n\n<li>You shouldn\u2019t do synchronous work in loading your data in a view (I should check this in my own apps)<\/li>\n<\/ul>\n\n\n\n<p>Identify in List and Tables<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>These are complex advanced controls that can cause performance issues.&nbsp; Understanding Identity is key to help improve this.<\/li>\n\n\n\n<li>Identity helps swiftUI handle View timeline\n<ul class=\"wp-block-list\">\n<li>Adding a ForEach in a list, as it will impact performance, it will need to know how many rows (views) to create, the Identity is used in creating this.<\/li>\n\n\n\n<li>For Filters you should not do it inline as it will still need to create all of the rows.&nbsp; You can move the filter into the ForEach but that is slow too.&nbsp; So you should move the filter into the model.&nbsp; That will be the fastest method.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>Avoid using AnyView in ForEach<\/li>\n\n\n\n<li>Flatten nested ForEach, except in Section Lists where SwiftUI will understand how to optimize this. ( can possibly use this for my category view in my own app)<\/li>\n\n\n\n<li>These same rules work in Tables too<\/li>\n\n\n\n<li>Number of Rows in Tables and Lists is Constant<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Building a mental model for performance in SwiftUI Performance feedback Loop Pre-requisite &nbsp; Watch Demystify SwiftUI from WWDC. 2021 Dependencies Faster View updates Identify in List and Tables<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"hide_page_title":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[2,3],"tags":[725,467,527,680],"class_list":["post-3265","post","type-post","status-publish","format-standard","hentry","category-blog","category-personal-softwareandit","tag-day-5","tag-performance","tag-swiftui","tag-wwdc23"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p2aMa8-QF","jetpack-related-posts":[],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3265","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/comments?post=3265"}],"version-history":[{"count":1,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3265\/revisions"}],"predecessor-version":[{"id":3267,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/posts\/3265\/revisions\/3267"}],"wp:attachment":[{"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/media?parent=3265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/categories?post=3265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/michaelrowe01.com\/index.php\/wp-json\/wp\/v2\/tags?post=3265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}