Title: GC/RT: Deferred Reference Counting? · Issue #1534 · AssemblyScript/assemblyscript · GitHub
Open Graph Title: GC/RT: Deferred Reference Counting? · Issue #1534 · AssemblyScript/assemblyscript
X Title: GC/RT: Deferred Reference Counting? · Issue #1534 · AssemblyScript/assemblyscript
Description: With the changes introduced in #1503, in particular having one more header word to work with, the following deferred mechanism akin to autorelease pools may be possible to improve our ARC implementation: Repurpose gcInfo2 (originally int...
Open Graph Description: With the changes introduced in #1503, in particular having one more header word to work with, the following deferred mechanism akin to autorelease pools may be possible to improve our ARC implement...
X Description: With the changes introduced in #1503, in particular having one more header word to work with, the following deferred mechanism akin to autorelease pools may be possible to improve our ARC implement...
Opengraph URL: https://github.com/AssemblyScript/assemblyscript/issues/1534
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"GC/RT: Deferred Reference Counting?","articleBody":"With the changes introduced in https://github.com/AssemblyScript/assemblyscript/pull/1503, in particular having one more header word to work with, the following deferred mechanism akin to autorelease pools may be possible to improve our ARC implementation:\r\n\r\n* Repurpose `gcInfo2` (originally introduced for tracing) to maintain a per-function autorelease pool as a linked list\r\n* Let the list's tail be a sentinel value, e.g. `-1`, so it can be distinguished from `null`\r\n* When compiling a function, instead of refcounting locals (to emulate a stack), link objects being kept alive by the function to the function's autorelease pool / list. Typical entries are managed function arguments, foreign function return values and managed values loaded from memory.\r\n* If there's a reachable `pool_track` in a code path, insert a `pool_create` incl. the necessary additional local at the start of a function, and a `pool_release` when exiting the function.\r\n\r\n```ts\r\n// Prototypical implementation\r\n\r\nimport { OBJECT, TOTAL_OVERHEAD } from \"rt/common\";\r\n\r\n/** Creates a new autorelease pool. */\r\n@inline function pool_create(): usize {\r\n return -1;\r\n}\r\n\r\n/** Tracks an object in an autorelease pool. */\r\nfunction pool_track(pool: usize, ptr: usize): usize {\r\n if (ptr) {\r\n var obj = changetype\u003cOBJECT>(ptr - TOTAL_OVERHEAD);\r\n // Only track objects that are not yet part of a pool. If the object is part\r\n // of a pool already, it is either part of this pool, or of a parent pool,\r\n // which is guaranteed to be released later than this pool.\r\n if (!obj.gcInfo2) {\r\n obj.gcInfo2 = pool;\r\n pool = changetype\u003cusize>(obj);\r\n }\r\n }\r\n return pool;\r\n}\r\n\r\n/** Releases an autorelease pool. */\r\nfunction pool_release(pool: usize): void {\r\n while (pool != -1) {\r\n let obj = changetype\u003cOBJECT>(pool);\r\n pool = obj.gcInfo2;\r\n obj.gcInfo2 = 0;\r\n decrement(changetype\u003cusize>(obj));\r\n }\r\n}\r\n```\r\n\r\n```ts\r\n// Example (comments are compiler inserts)\r\n\r\nclass Obj {}\r\n\r\nfunction foo(a: Obj): Obj { // `a` is likely tracked in an outer pool\r\n /**/var pool = pool_create(); // create pool for the function\r\n /**/pool = pool_track(pool, changetype\u003cusize>(a)); // track `a`, likely already in a pool\r\n var b = bar(a);\r\n /**/pool = pool_track(pool, changetype\u003cusize>(b)); // track `b`, likely not yet in a pool\r\n /**/__retain(b); // retain the return value\r\n /**/pool_release(pool); // release `a` (unlikely), `b` (likely)\r\n return b;\r\n}\r\n\r\nfunction bar(b: Obj): Obj { // `b` is likely tracked in an outer pool\r\n /**/var pool = pool_create(); // create pool for the function\r\n /**/pool = pool_track(pool, changetype\u003cusize>(b)); // track `b`, likely already in a pool\r\n /**/__retain(b); // retain the return value\r\n /**/pool_release(pool); // release `b` (unlikely)\r\n return b;\r\n}\r\n```\r\n\r\nGiving us:\r\n\r\n* No need to inject retain/release on locals, greatly simplifying compiler logic in that performing autoreleases is now a runtime instead of a compiler detail. Can then strip functionality like `Constraints.WILL_RETAIN`, `LocalFlags.RETAINED`, `Compiler#performAutoreleases`, `finishAutoreleases`, `skippedAutoreleases`, `tryUndoAutorelease`, `delayAutorelease` etc. from the compiler itself.\r\n* Reduces unnecessary write barriers on `release`, in that redundant `retain`/`release` pairs are implicitly deduplicated by means of adding objects to one pool max.\r\n* No additional allocations necessary to implement (i.e. no shadow stack)\r\n\r\nI'm likely missing something, but seems promising, so pinning it here. @MaxGraey Thoughts?","author":{"url":"https://github.com/dcodeIO","@type":"Person","name":"dcodeIO"},"datePublished":"2020-11-03T18:36:29Z","url":"https://github.com/AssemblyScript/assemblyscript/issues/1534"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:51cdf23f-501c-4187-228f-9875072f1038 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 80DA:114DF4:30DA93:4156FB:6B009E78 |
| html-safe-nonce | d612482dbeeb1af80df327287da29dd12f2d4b18befeab7fcb5dcd3f31f3d809 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MERBOjExNERGNDozMERBOTM6NDE1NkZCOjZCMDA5RTc4IiwidmlzaXRvcl9pZCI6IjU2MDA1Nzc4NTc1OTcyNTExOTIiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | e58b7d970d83a1919e8888ce4e236c21ff9e80d40bcb2c1002bd00a6d57d04e4 |
| hovercard-subject-tag | issue:735549801 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/AssemblyScript/assemblyscript/1534/issue_layout |
| twitter:image | https://opengraph.githubassets.com/971d7561816e7a8121aeb3a820da94c66fa1ea00ec43bb8ac60aad980bf51333/AssemblyScript/assemblyscript/issues/1534 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/971d7561816e7a8121aeb3a820da94c66fa1ea00ec43bb8ac60aad980bf51333/AssemblyScript/assemblyscript/issues/1534 |
| og:image:alt | With the changes introduced in #1503, in particular having one more header word to work with, the following deferred mechanism akin to autorelease pools may be possible to improve our ARC implement... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | dcodeIO |
| hostname | github.com |
| expected-hostname | github.com |
| None | 52c95ce7e48016ae14399656b4ee812462b6f1fa28c5008c8394da64794c00b8 |
| turbo-cache-control | no-preview |
| go-import | github.com/AssemblyScript/assemblyscript git https://github.com/AssemblyScript/assemblyscript.git |
| octolytics-dimension-user_id | 28916798 |
| octolytics-dimension-user_login | AssemblyScript |
| octolytics-dimension-repository_id | 105138356 |
| octolytics-dimension-repository_nwo | AssemblyScript/assemblyscript |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 105138356 |
| octolytics-dimension-repository_network_root_nwo | AssemblyScript/assemblyscript |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 1c9ddec8215f9a94e295c8310c3579b9c54ba31a |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width