{"id":36811,"date":"2022-08-19T15:06:15","date_gmt":"2022-08-19T19:06:15","guid":{"rendered":"https:\/\/mjtsai.com\/blog\/?p=36811"},"modified":"2023-06-19T10:10:25","modified_gmt":"2023-06-19T14:10:25","slug":"garbage-collection-in-javascriptcore","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2022\/08\/19\/garbage-collection-in-javascriptcore\/","title":{"rendered":"Garbage Collection in JavaScriptCore"},"content":{"rendered":"<p><a href=\"https:\/\/webkit.org\/blog\/12967\/understanding-gc-in-jsc-from-scratch\/\">Haoran Xu<\/a>:<\/p>\n<blockquote cite=\"https:\/\/webkit.org\/blog\/12967\/understanding-gc-in-jsc-from-scratch\/\"><p>The garbage collector in JSC is <a href=\"https:\/\/en.wikipedia.org\/wiki\/Tracing_garbage_collection#Moving_vs._non-moving\">non-compacting<\/a>, <a href=\"https:\/\/en.wikipedia.org\/wiki\/Tracing_garbage_collection#Generational_GC_(ephemeral_GC)\">generational<\/a> and mostly&#x2013;<a href=\"https:\/\/en.wikipedia.org\/wiki\/Tracing_garbage_collection#Stop-the-world_vs._incremental_vs._concurrent\">concurrent<\/a>. On top of being concurrent, JSC&rsquo;s GC heavily employs lock-free programming for better performance.<\/p>\n<p>[&#8230;]<\/p>\n<p>The inlined metadata <code>cellState<\/code> is easy to access for the mutator thread (the thread executing JavaScript code), since it is just a field in the object. However, it has bad memory locality for the GC and allocators, which need to quickly traverse through all the metadata of all objects in some block owned by <code>CompleteSubspace<\/code> (which is the common case). Outlined metadata have the opposite performance characteristics: they are more expensive to access for the mutator thread, but since they are aggregated into bitvectors and stored in the block footer of each block, GC and allocators can traverse them really fast.<\/p><p>So JSC keeps both inlined and outlined metadata to get the better of both worlds: the mutator thread&rsquo;s fast path will only concern the inlined <code>cellState<\/code>, while the GC and allocator logic can also take advantage of the memory locality of the outlined bits <code>isNew<\/code> and <code>isMarked<\/code>.<\/p><\/blockquote>\n\n<p>All this engineering notwithstanding, I still find myself using Chrome for some sites like <a href=\"https:\/\/boardgamearena.com\">Board Game Arena<\/a>, where performance, even with an M1 Mac, is abysmal compared with Chrome on a much slower Mac.<\/p>\n\n<p>Previously:<\/p>\n<ul>\n<li><a href=\"https:\/\/mjtsai.com\/blog\/2020\/04\/17\/chrome-vs-safari-energy-use-and-compatibility\/\">Chrome vs. Safari: Energy Use and Compatibility<\/a><\/li>\n<\/ul>","protected":false},"excerpt":{"rendered":"<p>Haoran Xu: The garbage collector in JSC is non-compacting, generational and mostly&#x2013;concurrent. On top of being concurrent, JSC&rsquo;s GC heavily employs lock-free programming for better performance. [&#8230;] The inlined metadata cellState is easy to access for the mutator thread (the thread executing JavaScript code), since it is just a field in the object. However, it [&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":"2022-08-19T19:06:18Z","apple_news_api_id":"72193bed-185e-4d53-9cf4-a2880fa83885","apple_news_api_modified_at":"2023-06-19T14:10:27Z","apple_news_api_revision":"AAAAAAAAAAAAAAAAAAAAAA==","apple_news_api_share_url":"https:\/\/apple.news\/Achk77RheTVOc9KKID6g4hQ","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":[910,2414,800,288,346,571,138,71,328],"class_list":["post-36811","post","type-post","status-publish","format-standard","hentry","category-programming-category","tag-board-game","tag-board-game-arena","tag-concurrency","tag-garbargecollection","tag-javascript","tag-memory-management","tag-optimization","tag-programming","tag-webkit"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36811","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=36811"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36811\/revisions"}],"predecessor-version":[{"id":36812,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/36811\/revisions\/36812"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=36811"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=36811"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=36811"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}