{“payload”:{“allShortcutsEnabled”:false,”fileTree”:{“doc/internals-notes”:{“items”:[{“name”:”Android-build.txt”,”path”:”doc/internals-notes/Android-build.txt”,”contentType”:”file”},{“name”:”GENCGC-PORTING-NOTES”,”path”:”doc/internals-notes/GENCGC-PORTING-NOTES”,”contentType”:”file”},{“name”:”arena-allocation.txt”,”path”:”doc/internals-notes/arena-allocation.txt”,”contentType”:”file”},{“name”:”array-ubsan”,”path”:”doc/internals-notes/array-ubsan”,”contentType”:”file”},{“name”:”closure”,”path”:”doc/internals-notes/closure”,”contentType”:”file”},{“name”:”cmsgc.txt”,”path”:”doc/internals-notes/cmsgc.txt”,”contentType”:”file”},{“name”:”compact-instance”,”path”:”doc/internals-notes/compact-instance”,”contentType”:”file”},{“name”:”eden-pages”,”path”:”doc/internals-notes/eden-pages”,”contentType”:”file”},{“name”:”fdefn-gc-safety”,”path”:”doc/internals-notes/fdefn-gc-safety”,”contentType”:”file”},{“name”:”make-thread-bench”,”path”:”doc/internals-notes/make-thread-bench”,”contentType”:”file”},{“name”:”mark-region”,”path”:”doc/internals-notes/mark-region”,”contentType”:”file”},{“name”:”memory-arenas.txt”,”path”:”doc/internals-notes/memory-arenas.txt”,”contentType”:”file”},{“name”:”metaspace”,”path”:”doc/internals-notes/metaspace”,”contentType”:”file”},{“name”:”non-moving-gc”,”path”:”doc/internals-notes/non-moving-gc”,”contentType”:”file”},{“name”:”release-checklist.txt”,”path”:”doc/internals-notes/release-checklist.txt”,”contentType”:”file”},{“name”:”structure-typep-change”,”path”:”doc/internals-notes/structure-typep-change”,”contentType”:”file”},{“name”:”updating-unicode”,”path”:”doc/internals-notes/updating-unicode”,”contentType”:”file”},{“name”:”zero-with-mmap-bug.txt”,”path”:”doc/internals-notes/zero-with-mmap-bug.txt”,”contentType”:”file”}],”totalCount”:18},”doc”:{“items”:[{“name”:”cmu-user”,”path”:”doc/cmu-user”,”contentType”:”directory”},{“name”:”internals-notes”,”path”:”doc/internals-notes”,”contentType”:”directory”},{“name”:”internals”,”path”:”doc/internals”,”contentType”:”directory”},{“name”:”manual”,”path”:”doc/manual”,”contentType”:”directory”},{“name”:”FOR-CMUCL-DEVELOPERS”,”path”:”doc/FOR-CMUCL-DEVELOPERS”,”contentType”:”file”},{“name”:”GIT-FOR-SBCL-HACKERS.txt”,”path”:”doc/GIT-FOR-SBCL-HACKERS.txt”,”contentType”:”file”},{“name”:”GIT-WORKFLOW.md”,”path”:”doc/GIT-WORKFLOW.md”,”contentType”:”file”},{“name”:”PACKAGING-SBCL.txt”,”path”:”doc/PACKAGING-SBCL.txt”,”contentType”:”file”},{“name”:”README”,”path”:”doc/README”,”contentType”:”file”},{“name”:”clean.sh”,”path”:”doc/clean.sh”,”contentType”:”file”},{“name”:”entities.inc”,”path”:”doc/entities.inc”,”contentType”:”file”},{“name”:”make-doc.sh”,”path”:”doc/make-doc.sh”,”contentType”:”file”},{“name”:”sbcl.1″,”path”:”doc/sbcl.1″,”contentType”:”file”}],”totalCount”:13},””:{“items”:[{“name”:”.github”,”path”:”.github”,”contentType”:”directory”},{“name”:”benchmarks”,”path”:”benchmarks”,”contentType”:”directory”},{“name”:”contrib”,”path”:”contrib”,”contentType”:”directory”},{“name”:”crossbuild-runner”,”path”:”crossbuild-runner”,”contentType”:”directory”},{“name”:”doc”,”path”:”doc”,”contentType”:”directory”},{“name”:”src”,”path”:”src”,”contentType”:”directory”},{“name”:”tests”,”path”:”tests”,”contentType”:”directory”},{“name”:”tlsf-bsd”,”path”:”tlsf-bsd”,”contentType”:”directory”},{“name”:”tools-for-build”,”path”:”tools-for-build”,”contentType”:”directory”},{“name”:”.cirrus.yml”,”path”:”.cirrus.yml”,”contentType”:”file”},{“name”:”.dir-locals.el”,”path”:”.dir-locals.el”,”contentType”:”file”},{“name”:”.gitattributes”,”path”:”.gitattributes”,”contentType”:”file”},{“name”:”.gitignore”,”path”:”.gitignore”,”contentType”:”file”},{“name”:”.mailmap”,”path”:”.mailmap”,”contentType”:”file”},{“name”:”BUGS”,”path”:”BUGS”,”contentType”:”file”},{“name”:”COPYING”,”path”:”COPYING”,”contentType”:”file”},{“name”:”CREDITS”,”path”:”CREDITS”,”contentType”:”file”},{“name”:”HACKING”,”path”:”HACKING”,”contentType”:”file”},{“name”:”INSTALL”,”path”:”INSTALL”,”contentType”:”file”},{“name”:”NEWS”,”path”:”NEWS”,”contentType”:”file”},{“name”:”OPTIMIZATIONS”,”path”:”OPTIMIZATIONS”,”contentType”:”file”},{“name”:”PRINCIPLES”,”path”:”PRINCIPLES”,”contentType”:”file”},{“name”:”README”,”path”:”README”,”contentType”:”file”},{“name”:”TLA”,”path”:”TLA”,”contentType”:”file”},{“name”:”TODO”,”path”:”TODO”,”contentType”:”file”},{“name”:”binary-distribution.sh”,”path”:”binary-distribution.sh”,”contentType”:”file”},{“name”:”build-comparison.sh”,”path”:”build-comparison.sh”,”contentType”:”file”},{“name”:”clean.sh”,”path”:”clean.sh”,”contentType”:”file”},{“name”:”cload-xc.lisp”,”path”:”cload-xc.lisp”,”contentType”:”file”},{“name”:”cross-make.sh”,”path”:”cross-make.sh”,”contentType”:”file”},{“name”:”distclean.sh”,”path”:”distclean.sh”,”contentType”:”file”},{“name”:”find-gnumake.sh”,”path”:”find-gnumake.sh”,”contentType”:”file”},{“name”:”float-math.lisp-expr”,”path”:”float-math.lisp-expr”,”contentType”:”file”},{“name”:”freeze.sh”,”path”:”freeze.sh”,”contentType”:”file”},{“name”:”generate-version.sh”,”path”:”generate-version.sh”,”contentType”:”file”},{“name”:”html-distribution.sh”,”path”:”html-distribution.sh”,”contentType”:”file”},{“name”:”install.sh”,”path”:”install.sh”,”contentType”:”file”},{“name”:”load-xc.lisp”,”path”:”load-xc.lisp”,”contentType”:”file”},{“name”:”loader.lisp”,”path”:”loader.lisp”,”contentType”:”file”},{“name”:”make-all-targets.sh”,”path”:”make-all-targets.sh”,”contentType”:”file”},{“name”:”make-android.sh”,”path”:”make-android.sh”,”contentType”:”file”},{“name”:”make-c-runtime.sh”,”path”:”make-c-runtime.sh”,”contentType”:”file”},{“name”:”make-config.sh”,”path”:”make-config.sh”,”contentType”:”file”},{“name”:”make-genesis-2.lisp”,”path”:”make-genesis-2.lisp”,”contentType”:”file”},{“name”:”make-genesis-2.sh”,”path”:”make-genesis-2.sh”,”contentType”:”file”},{“name”:”make-host-1.lisp”,”path”:”make-host-1.lisp”,”contentType”:”file”},{“name”:”make-host-1.sh”,”path”:”make-host-1.sh”,”contentType”:”file”},{“name”:”make-host-2.lisp”,”path”:”make-host-2.lisp”,”contentType”:”file”},{“name”:”make-host-2.sh”,”path”:”make-host-2.sh”,”contentType”:”file”},{“name”:”make-shared-library.sh”,”path”:”make-shared-library.sh”,”contentType”:”file”},{“name”:”make-target-1.sh”,”path”:”make-target-1.sh”,”contentType”:”file”},{“name”:”make-target-2-load.lisp”,”path”:”make-target-2-load.lisp”,”contentType”:”file”},{“name”:”make-target-2.sh”,”path”:”make-target-2.sh”,”contentType”:”file”},{“name”:”make-target-contrib-android.sh”,”path”:”make-target-contrib-android.sh”,”contentType”:”file”},{“name”:”make-target-contrib.sh”,”path”:”make-target-contrib.sh”,”contentType”:”file”},{“name”:”make-windows-installer.sh”,”path”:”make-windows-installer.sh”,”contentType”:”file”},{“name”:”make.sh”,”path”:”make.sh”,”contentType”:”file”},{“name”:”maketarg2loop.lisp”,”path”:”maketarg2loop.lisp”,”contentType”:”file”},{“name”:”pubring.pgp”,”path”:”pubring.pgp”,”contentType”:”file”},{“name”:”release.sh”,”path”:”release.sh”,”contentType”:”file”},{“name”:”run-sbcl.sh”,”path”:”run-sbcl.sh”,”contentType”:”file”},{“name”:”sbcl-pwd.sh”,”path”:”sbcl-pwd.sh”,”contentType”:”file”},{“name”:”slam.sh”,”path”:”slam.sh”,”contentType”:”file”},{“name”:”source-distribution.sh”,”path”:”source-distribution.sh”,”contentType”:”file”},{“name”:”tagify.sh”,”path”:”tagify.sh”,”contentType”:”file”},{“name”:”verify-header-parsing.sh”,”path”:”verify-header-parsing.sh”,”contentType”:”file”},{“name”:”wc.sh”,”path”:”wc.sh”,”contentType”:”file”}],”totalCount”:67}},”fileTreeProcessingTime”:12.601077,”foldersToFetch”:[],”reducedMotionEnabled”:null,”repo”:{“id”:1890957,”defaultBranch”:”master”,”name”:”sbcl”,”ownerLogin”:”sbcl”,”currentUserCanPush”:false,”isFork”:false,”isEmpty”:false,”createdAt”:”2011-06-13T20:33:25.000Z”,”ownerAvatar”:”https://avatars.githubusercontent.com/u/318632?v=4″,”public”:true,”private”:false,”isOrgOwned”:true},”symbolsExpanded”:false,”treeExpanded”:true,”refInfo”:{“name”:”master”,”listCacheKey”:”v0:1696884190.0″,”canEdit”:false,”refType”:”branch”,”currentOid”:”e466236e00cd13d8aae6a56aa96636d096e248c8″},”path”:”doc/internals-notes/arena-allocation.txt”,”currentUser”:null,”blob”:{“rawLines”:[“Overview”,”========”,”Arenas offer a mark/release paradigm for rapid deallocation of thread-local”,”lisp objects with the goal of reducing global heap usage.”,””,”The implementation is built atop the existing pointer-bump allocator”,”of ‘gencgc’ with a provision for redirecting the allocator’s free-pointer”,”somewhere other than dynamic space. When the C fallback is invoked,”,”it notices that allocation should not occur to the main heap.”,””,”It is possible for multiple threads to share one arena, or for threads”,”to each get their own arena, or potentially even to have more than one”,”arena controlled by a thread. The constraint is that in order to release”,”all memory used by an arena without incurring a stop-the-world event”,”there must be no heap-to-arena pointer reachable in a graph trace,”,”supposing that the about-to-be-released memory is not a root.”,”An arena has to be released in total, though in theory it could be”,”possible to provide a partial release feature as well.”,”It thus becomes possible to discard large portions of the”,”reachability graph under user control.”,””,”Design consideration”,”====================”,”Two possible approaches toward modifying the pointer-bump were reasonable:”,”1) upon each allocation, decide whether it is to occur to the dynamic space”,” or elsewhere, and then use a pair of pointers (free-pointer and limit)”,” that are particular to either the dynamic space or the elsewhere.”,” As always, when the free-pointer and limit coincide, the slow path”,” is invoked which calls to the C runtime support for help.”,””,”2) upon each allocation, assume that a _single_ pair of pointers (as before,”,” the free-pointer and limit) are always pointing to the correct “place””,” (dynamic space or elsewhere). This introduces no branching except”,” in the case where the slow path is invoked.”,””,”Option (1) entails significantly more runtime overhead, as every allocation”,”would involve flag checking and lookups of the address containing the”,”pointers that should be used.”,””,”Option (2) entails no overhead beyond the free==limit check, which is always”,”performed no matter what. By suitably initializing the pointer and limit”,”to coincide on each “switch” of arena<->heap, the only overhead to be”,”introduced is in the fallback code (the C runtime)”,”This settles the matter: approach (2) wins.”,””,”Implementation”,”==============”,”Each thread structure is augmented with two new thread-local”,”allocation buffers (“TLABs”). One is for conses and the other”,”is for everything else. These mirror the existing two TLABs.”,”The distinction between cons/non-cons is that cons pages can be prefilled”,”in each byte with 0xFF which is not a valid cons; whereas other objects are”,”prefilled with 0s, which is similarly not valid. Thus we can recognize”,”portions of memory that have not been initialized with valid objects.”,”The cons of (0 . 0) is valid, and so 0 is inadequate to detect uninitialized”,”memory.”,””,”In total, each thread has 4 TLABs:”,” system conses”,” system “Mixed objects””,” user conses”,” user “Mixed objects””,””,”Correct use of the distinct TLABs allows the user code to avoid”,”creating heap-to-arena pointers.”,”In the absence of arenas, the “user” TLABs are the ones ordinarily”,”used for all allocations, including “system” allocation. i.e. There is”,”no distinction between “user” and “system” code.”,”In the presence of arenas, the user TLABs are directed either”,”to the dynamic space, or to the arena depending the dynamic control”,”which selects whether arena allocation is to occur.”,”In contract, “system” TLABs can only allocate to dynamic space.”,””,”Memory is claimed from the arena in small chunks, much as it is”,”obtained from dynamic space in a certain granularity, currently”,”32 KiB, which is SB-VM:GENCGC-PAGE-BYTES. The arena allocation”,”granularity is the same, for no particular reason.”,”There is actually no restriction on the chunk size, so objects”,”in excess of 32KiB can be allocated to the arena.”,”When several threads share a single arena, they claim successive”,”chunks using a compare-and-swap on the arena-relative free-pointer.”,””,”GC interaction”,”==============”,”The memory in an arena is intended to be invisible to GC”,”for the most part. Pointers between dynamic space and the arena”,”in either direction are not traced by the collector. Therefore,”,”applications making use of arenas should generally inhibit”,”collection around use of the arena. Arenas do not attempt”,”to emulate “thread local heaps”.”,””,”While debugging arena-based algorithms it is helpful to treat arenas”,”as GC roots, so that if garbage-collection occurs organically due to”,”dynamic-space usage, all heap objects pointed to by any arena remain live.”,”Using the tools available such as SB-EXT:SEARCH-ROOTS and the new”,”FIND-HEAP->ARENA, it is almost always possible to eradicate”,”the “forbidden” heap->arena pointers. This is of course only for”,”debugging, because any real-world scenario would expect not to need”,”the extra delay that comes from hunting for pointers, as it is”,”entirely contrary to the intent of using the arena in the first place.”,””,”Thread interaction”,”==================”,”A created thread inherits the arena of its creator.”,””,”At present, threads do not maintain enough state to know where”,”they were allocating in both the arena and the dynamic space.”,”Consequently, each “switch” from arena to dynamic space and back”,”incurs a small amount of waste, as the last chunk of memory claimed”,”for that thread in a particular TLAB is discarded.”,””,”Control mechanisms”,”==================”,”SB-VM:WITH-ARENA”,” specifies that all allocations within its dynamic scope”,” (i.e. regardless of where in the program allocation occurs)”,” are to be directed to the arena, with the exception that”,” code which was compiled to use the system TLAB will only”,” allocate to the heap.”,”SB-VM:WITHOUT-ARENA”,” specifies that all allocations within its dynamic scope”,” are to be directed to the dynamic space.”,”SB-VM:IN-SAME-ARENA (X)”,” specifies that allocation should occur where object X”,” was allocated.”,”(DECLARE (SB-C::TLAB :SYSTEM))”,” specifies that within its lexical scope, all allocations”,” should go to the heap. This works as intended _only _if_”,” all allocations within the scope are handled as”,” “inline” allocations. Code that is called from within”,” the scope of this declaration does not see the declaration”,” (as is to be expected per the language semantics)”,” and therefore uses the dynamic mechanism.”,””,”Best practice”,”=============”,”Based on the preceding description of the control mechanisms”,”and the limitation upon switching in terms of memory waste,”,”it should be evident that code which uses the lexical declaration”,”is slightly to be preferred.”,”It is often possible to avoid use of the dynamic mechanism”,”by replacing an allocation point with the following pattern:”,””,” (if (should-allocate-to-heap)”,” (locally (declare (sb-c::tlab :system)) (do-allocation))”,” (do-allocation))”,””,”So despite the “doubling” of the allocator form, this is potentially”,”more efficient. Most likely the user would wrap this idiom in a macro.”,””,”In practice, all mechanisms of control are necessary. Within a”,”lexically scoped usage, there might be a hidden call to a builtin”,”function such as REVERSE that would cons a new list using”,”the dynamic choice of heap or arena.”,””,”Pending items”,”=============”,”* Some of the waste that comes from switching between arena and heap”,” can be avoided by adding more state to the thread structure.”,””,”* Background thread pools in particular are a problem.”,” In one such implementation, a thread which requests work to be performed”,” by a worker in a pool sends as part of the work request an identifier”,” of the arena in use by the requester. The worker will switch to that”,” same arena. The implication is that worker threads will constantly be”,” switching their arena, which as per above, is inefficient.”],”stylingDirectives”:[[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[],[]],”csv”:null,”csvError”:null,”dependabotInfo”:{“showConfigurationBanner”:false,”configFilePath”:null,”networkDependabotPath”:”/sbcl/sbcl/network/updates”,”dismissConfigurationNoticePath”:”/settings/dismiss-notice/dependabot_configuration_notice”,”configurationNoticeDismissed”:null,”repoAlertsPath”:”/sbcl/sbcl/security/dependabot”,”repoSecurityAndAnalysisPath”:”/sbcl/sbcl/settings/security_analysis”,”repoOwnerIsOrg”:true,”currentUserCanAdminRepo”:false},”displayName”:”arena-allocation.txt”,”displayUrl”:”https://github.com/sbcl/sbcl/blob/master/doc/internals-notes/arena-allocation.txt?raw=true”,”headerInfo”:{“blobSize”:”7.59 KB”,”deleteInfo”:{“deleteTooltip”:”You must be signed in to make or propose changes”},”editInfo”:{“editTooltip”:”You must be signed in to make or propose changes”},”ghDesktopPath”:”https://desktop.github.com”,”gitLfsPath”:null,”onBranch”:true,”shortPath”:”e66fba4″,”siteNavLoginPath”:”/login?return_to=https%3A%2F%2Fgithub.com%2Fsbcl%2Fsbcl%2Fblob%2Fmaster%2Fdoc%2Finternals-notes%2Farena-allocation.txt”,”isCSV”:false,”isRichtext”:false,”toc”:null,”lineInfo”:{“truncatedLoc”:”166″,”truncatedSloc”:”145″},”mode”:”file”},”image”:false,”isCodeownersFile”:null,”isPlain”:false,”isValidLegacyIssueTemplate”:false,”issueTemplateHelpUrl”:”https://docs.github.com/articles/about-issue-and-pull-request-templates”,”issueTemplate”:null,”discussionTemplate”:null,”language”:”Text”,”languageID”:372,”large”:false,”loggedIn”:false,”newDiscussionPath”:”/sbcl/sbcl/discussions/new”,”newIssuePath”:”/sbcl/sbcl/issues/new”,”planSupportInfo”:{“repoIsFork”:null,”repoOwnedByCurrentUser”:null,”requestFullPath”:”/sbcl/sbcl/blob/master/doc/internals-notes/arena-allocation.txt”,”showFreeOrgGatedFeatureMessage”:null,”showPlanSupportBanner”:null,”upgradeDataAttributes”:null,”upgradePath”:null},”publishBannersInfo”:{“dismissActionNoticePath”:”/settings/dismiss-notice/publish_action_from_dockerfile”,”dismissStackNoticePath”:”/settings/dismiss-notice/publish_stack_from_file”,”releasePath”:”/sbcl/sbcl/releases/new?marketplace=true”,”showPublishActionBanner”:false,”showPublishStackBanner”:false},”rawBlobUrl”:”https://github.com/sbcl/sbcl/raw/master/doc/internals-notes/arena-allocation.txt”,”renderImageOrRaw”:false,”richText”:null,”renderedFileInfo”:null,”shortPath”:null,”tabSize”:8,”topBannersInfo”:{“overridingGlobalFundingFile”:false,”globalPreferredFundingPath”:null,”repoOwner”:”sbcl”,”repoName”:”sbcl”,”showInvalidCitationWarning”:false,”citationHelpUrl”:”https://docs.github.com/en/github/creating-cloning-and-archiving-repositories/creating-a-repository-on-github/about-citation-files”,”showDependabotConfigurationBanner”:false,”actionsOnboardingTip”:null},”truncated”:false,”viewable”:true,”workflowRedirectUrl”:null,”symbols”:{“timedOut”:false,”notAnalyzed”:true,”symbols”:[]}},”copilotInfo”:null,”copilotAccessAllowed”:false,”csrf_tokens”:{“/sbcl/sbcl/branches”:{“post”:”S4WcYGJUVw0-Q_bSQsvvPQmi-rTJMTBIGgH6O7vzqhWQDzFREzC2_H5lJ1jrqSjhMlNGHv9lwn1rsOfRW6jIEw”},”/repos/preferences”:{“post”:”hLICaK08VtuqrT3z35zaE_P1MpM42T9v51SHOpyQfoJoAkZNEAKN_96dlAZ0_4HVrPHMz0hATjRb0hNBe7YE0A”}}},”title”:”sbcl/doc/internals-notes/arena-allocation.txt at master ยท sbcl/sbcl”}

Read More