-
Notifications
You must be signed in to change notification settings - Fork 6
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
Pool based allocation #16
Comments
For example maybe we could have let mut factory: HConsign<Expr> = HConsign::from_arena(arena);
factory.mk(Expr::Add(1, 2)); // Calls `arena.alloc_arc(Expr::Add(1,2))` |
This sounds great, but unfortunately I don't have a lot of time to spend on this... Would you be interested in writing a PR for this? |
Yes. I can write a PR for it. Do you want to add I think this is what I would do:
so something like let mut factory: HConsed<Expr, Pointer=ArenaArc<Expr>, Allocator=SharedArena<Expr>> = ... and the default behaviour is to have |
There is currently a problem. The implementation of pub struct HConsign<T: Hash + Eq + Clone, S = RandomState, Alloc: Allocator<T>=alloc::DefaultAllocator> {
/// The actual hash consing table.
table: HashMap<T, WHConsed<T, Alloc>, S>,
/// Counter for uids.
count: u64,
}
The default allocator is implemented like this: pub trait Arc<T, W>: std::ops::Deref<Target=T> + core::borrow::Borrow<T>
{
fn downgrade(&self) -> W;
}
pub trait Weak<T, S>
{
fn upgrade(&self) -> Option<S>;
}
impl<T> Arc<T, std::sync::Weak<T>> for std::sync::Arc<T>
{
fn downgrade(&self) -> std::sync::Weak<T>
{
std::sync::Arc::downgrade(&self)
}
}
impl<T> Weak<T, std::sync::Arc<T>> for std::sync::Weak<T>
{
fn upgrade(&self) -> Option<std::sync::Arc<T>>
{
std::sync::Weak::upgrade(&self)
}
}
pub trait Allocator<T>
{
/// Strong `std::sync::Arc`-like pointer
type Strong: Arc<T, Self::Weak>;
/// `std::sync::Weak`-like pointer
type Weak: Weak<T, Self::Strong>;
fn alloc(data: T) -> Self::Strong;
}
pub(super) struct DefaultAllocator {}
impl<T> Allocator<T> for DefaultAllocator
{
type Strong = std::sync::Arc<T>;
type Weak = std::sync::Weak<T>;
fn alloc(data: T) -> Self::Strong
{
Self::Strong::new(data)
}
} |
I'm tempted to have two parameters for (So assuming you use refs that don't have a weak version, both these types would be the same.) |
On that note, it seems to me like instead of having 4 or 5 or more parameters, we could just have one trait that specifies everything (types and functions) and just take that as a parameter. (Or maybe two traits, whatever makes the most sense, I just mean some of these parameters should be aggregated.) Then we provide a type implementing this trait that corresponds to the current behavior and make that the default. Adding shared arena is then just a feature-gated type also implementing this trait and some helpers to easily create the associated consign. |
What do you think? |
We can do that. What do you think we should do about the Weak type? We can emulate the weak type by using two memory arenas. One arena would store the underlying data type Is the current solution good enough? The |
I've submitted a PR for shared_arena so the |
Update: I think |
Is there anyway to make the
HConsed
pointers allocate using some memory pool? For example theArenaArc
type in shared_arena looks pretty compatible withstd::sync::Arc
, and it would help a lot for applications where a large number of objects needs to be allocated and deallocated.The text was updated successfully, but these errors were encountered: