{"id":32738,"date":"2021-06-07T11:17:54","date_gmt":"2021-06-07T15:17:54","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=32738"},"modified":"2021-06-07T11:17:54","modified_gmt":"2021-06-07T15:17:54","slug":"kernel-assisting-the-objective-c-runtime","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2021\/06\/07\/kernel-assisting-the-objective-c-runtime\/","title":{"rendered":"Kernel Assisting the Objective-C Runtime"},"content":{"rendered":"<p>This is really cool, and I just realized that I forgot to link to it after last year&rsquo;s WWDC.<\/p>\n\n<p><a href=\"https:\/\/twitter.com\/pedantcoder\/status\/1277124486207176706\">Pierre Habouzit<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/pedantcoder\/status\/1277124486207176706\"><p>Faster, priority-inversion-less GC (speed)<\/p><p>@mikeash wrote about it a <a href=\"https:\/\/www.mikeash.com\/pyblog\/friday-qa-2015-05-29-concurrent-memory-deallocation-in-the-objective-c-runtime.html\">long time ago<\/a>, however it turns out this was relatively slow and could also fail (if a thread was observed in the critical section).<\/p><p>The general idea is still the same, but instead of observing the other threads PC, we use a scheme where at startup the runtime tells the kernel where the code reading the IMP caches &ldquo;unsafely&rdquo; is, and gives a &ldquo;recovery PC&rdquo;.<\/p><p>Now when a GC is triggered, instead of asking for all the other threads PCs, the kernel is told to &ldquo;force&rdquo; the PC of threads in the critical section to the recovery path, there&rsquo;s no waiting, and it can&rsquo;t fail.<\/p><p>it&rsquo;s also 10-100x as fast as before.<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/Catfish_Man\/status\/1277204498985828353\">David Smith<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/Catfish_Man\/status\/1277204498985828353\"><p>This is one of the wildest optimizations I&rsquo;ve seen. How do you make a reader-writer lock with zero instructions? You uh&#8230; ask the kernel to move all the reader threads out of the critical section &#x1F631;&#x1F631;&#x1F631;<\/p><p>Just like&#8230; longjmp someone else from outside &#x1F92F;<\/p><\/blockquote>\n\n<p><a href=\"https:\/\/twitter.com\/pedantcoder\/status\/1280377086411067392\">Pierre Habouzit<\/a>:<\/p>\n<blockquote cite=\"https:\/\/twitter.com\/pedantcoder\/status\/1280377086411067392\"><p>I really feel that working at \n@Apple\n gives one a unique convergence of &ldquo;all the things&rdquo; that is mostly unmatched in the rest of the industry and some of us are lucky enough to have the opportunity to take advantage of it ;)<\/p><\/blockquote>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/07\/06\/optimizing-the-objective-c-runtime-in-big-sur\/\">Optimizing the Objective-C Runtime in Big Sur<\/a><\/li>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2016\/02\/12\/concurrent-memory-deallocation-in-the-objective-c-runtime\/\">Concurrent Memory Deallocation in the Objective-C Runtime<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>This is really cool, and I just realized that I forgot to link to it after last year&rsquo;s WWDC. Pierre Habouzit: Faster, priority-inversion-less GC (speed)@mikeash wrote about it a long time ago, however it turns out this was relatively slow and could also fail (if a thread was observed in the critical section).The general idea [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"2021-06-07T15:17:58Z","apple_news_api_id":"cd82b7b5-40f1-44a7-88a2-4440321ffb14","apple_news_api_modified_at":"2021-06-07T15:17:59Z","apple_news_api_revision":"AAAAAAAAAAD\/\/\/\/\/\/\/\/\/\/w==","apple_news_api_share_url":"https:\/\/apple.news\/AzYK3tUDxRKeIokRAMh_7FA","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[4],"tags":[800,288,31,1837,845,30,1891,571,760,138,71],"class_list":["post-32738","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-concurrency","tag-garbargecollection","tag-ios","tag-ios-14","tag-kernel","tag-mac","tag-macos-11-0","tag-memory-management","tag-objective-c-runtime","tag-optimization","tag-programming"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32738","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=32738"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32738\/revisions"}],"predecessor-version":[{"id":32739,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/32738\/revisions\/32739"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=32738"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=32738"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=32738"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}