forked from NixOS/nix
-
Notifications
You must be signed in to change notification settings - Fork 13
Allocate RootValues more efficiently #546
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e365b60
allocRootValue: Recycle root slots through a pool
edolstra 5bd8dc0
Add UniqueRootValue: a shared_ptr-free GC root handle
edolstra bb1f958
Root value pool: Use an intrusive freelist
edolstra 7cf421a
UniqueRootValue: Guard against self-assignment
edolstra File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| #pragma once | ||
| ///@file | ||
|
|
||
| #include <memory> | ||
| #include <utility> | ||
|
|
||
| namespace nix { | ||
|
|
||
| struct Value; | ||
|
|
||
| /** | ||
| * Allocate a slot from the root value pool, i.e. a GC-visible | ||
| * `Value *` cell that keeps the value it points to alive across | ||
| * garbage collections. Use `UniqueRootValue`/`RootValue` rather than | ||
| * calling this directly. | ||
| */ | ||
| Value ** allocRootValueSlot(Value * v); | ||
|
|
||
| /** | ||
| * Clear the given slot and return it to the root value pool. | ||
| */ | ||
| void freeRootValueSlot(Value ** slot); | ||
|
|
||
| /** | ||
| * A move-only handle rooting a Value, i.e. keeping it and everything | ||
| * reachable from it alive across garbage collections. Prefer this | ||
| * over `RootValue` unless the handle must be copyable (e.g. when it's | ||
| * captured in a `std::function`-backed lambda). | ||
| */ | ||
| class UniqueRootValue | ||
| { | ||
| Value ** slot = nullptr; | ||
|
|
||
| public: | ||
| UniqueRootValue() = default; | ||
|
|
||
| explicit UniqueRootValue(Value * v) | ||
| : slot(allocRootValueSlot(v)) | ||
| { | ||
| } | ||
|
|
||
| UniqueRootValue(const UniqueRootValue &) = delete; | ||
| UniqueRootValue & operator=(const UniqueRootValue &) = delete; | ||
|
|
||
| UniqueRootValue(UniqueRootValue && other) noexcept | ||
| : slot(std::exchange(other.slot, nullptr)) | ||
| { | ||
| } | ||
|
|
||
| UniqueRootValue & operator=(UniqueRootValue && other) noexcept | ||
| { | ||
| if (this == &other) | ||
| return *this; | ||
| if (slot) | ||
| freeRootValueSlot(slot); | ||
| slot = std::exchange(other.slot, nullptr); | ||
| return *this; | ||
|
edolstra marked this conversation as resolved.
|
||
| } | ||
|
|
||
| ~UniqueRootValue() | ||
| { | ||
| if (slot) | ||
| freeRootValueSlot(slot); | ||
| } | ||
|
|
||
| /** | ||
| * Release the slot, i.e. stop rooting the value. | ||
| */ | ||
| void reset() | ||
| { | ||
| if (slot) { | ||
| freeRootValueSlot(slot); | ||
| slot = nullptr; | ||
| } | ||
| } | ||
|
|
||
| Value *& operator*() const | ||
| { | ||
| return *slot; | ||
| } | ||
|
|
||
| explicit operator bool() const | ||
| { | ||
| return slot != nullptr; | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * A copyable, shared handle rooting a Value. Only use this instead of | ||
| * `UniqueRootValue` if the handle must be copyable. | ||
| */ | ||
| typedef std::shared_ptr<Value *> RootValue; | ||
|
|
||
| RootValue allocRootValue(Value * v); | ||
|
|
||
| } // namespace nix | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.