use std::sync::Arc;
use std::sync::Mutex;
use super::Condition;
use super::ConditionResolver;
use super::ConditionResolverContext;
use super::PrintNodeCell;
use super::SavePoint;
use super::StringContainer;
use super::WriteItem;
use super::collections::GraphNode;
use super::collections::LookAheadSavePointsNode;
use super::collections::NodeStackNode;
use super::thread_state;
use super::thread_state::Counts;
pub struct Arena {
bump: bumpalo::Bump,
outer_counts: Counts,
pool: Option<ArenaPool>,
}
impl Default for Arena {
fn default() -> Self {
Self::new()
}
}
impl Drop for Arena {
fn drop(&mut self) {
if let Some(pool) = self.pool.take() {
let mut bump = std::mem::take(&mut self.bump);
bump.reset();
pool.lock_bumps().push(bump);
}
thread_state::set_counts(std::mem::take(&mut self.outer_counts));
}
}
impl Arena {
pub fn new() -> Self {
Self::new_internal(bumpalo::Bump::new(), None)
}
fn new_internal(bump: bumpalo::Bump, pool: Option<ArenaPool>) -> Self {
Self {
bump,
outer_counts: thread_state::take_counts(),
pool,
}
}
pub fn bump(&self) -> &bumpalo::Bump {
&self.bump
}
pub fn alloc_resolver<'a>(&'a self, resolver: impl Fn(&mut ConditionResolverContext) -> Option<bool> + Copy + 'a) -> ConditionResolver<'a> {
self.bump.alloc(resolver)
}
pub(super) fn alloc_condition<'a>(&'a self, condition: Condition<'a>) -> &'a Condition<'a> {
self.bump.alloc(condition)
}
pub(super) fn alloc_str<'a>(&'a self, text: &str) -> &'a StringContainer<'a> {
let text = self.bump.alloc_str(text);
self.alloc_string_container(text)
}
pub(super) fn alloc_string_container<'a>(&'a self, text: &'a str) -> &'a StringContainer<'a> {
self.bump.alloc(StringContainer::new(text))
}
pub(super) fn alloc_print_node_cell<'a>(&'a self, cell: PrintNodeCell<'a>) -> &'a PrintNodeCell<'a> {
self.bump.alloc(cell)
}
pub(super) fn alloc_write_item_graph_node<'a>(&'a self, node: GraphNode<'a, WriteItem<'a>>) -> &'a GraphNode<'a, WriteItem<'a>> {
self.bump.alloc(node)
}
pub(super) fn alloc_node_stack_node<'a>(&'a self, node: NodeStackNode<'a>) -> &'a NodeStackNode<'a> {
self.bump.alloc(node)
}
pub(super) fn alloc_save_point<'a>(&'a self, save_point: SavePoint<'a>) -> &'a SavePoint<'a> {
self.bump.alloc(save_point)
}
pub(super) fn alloc_look_ahead_save_points_node<'a>(&'a self, node: LookAheadSavePointsNode<'a>) -> &'a LookAheadSavePointsNode<'a> {
self.bump.alloc(node)
}
}
#[derive(Clone, Default)]
pub struct ArenaPool {
bumps: Arc<Mutex<Vec<bumpalo::Bump>>>,
}
impl ArenaPool {
pub fn new() -> Self {
Self::default()
}
pub fn create_arena(&self) -> Arena {
let bump = self.lock_bumps().pop().unwrap_or_default();
Arena::new_internal(bump, Some(self.clone()))
}
pub fn clear(&self) {
self.lock_bumps().clear();
}
fn lock_bumps(&self) -> std::sync::MutexGuard<'_, Vec<bumpalo::Bump>> {
self.bumps.lock().unwrap_or_else(|err| err.into_inner())
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn should_reuse_the_memory_of_arenas_from_the_pool() {
let pool = ArenaPool::new();
{
let arena = pool.create_arena();
arena.bump().alloc_slice_fill_copy(10_000, 0u8);
}
assert_eq!(pool.lock_bumps().len(), 1);
{
let arena = pool.create_arena();
assert_eq!(pool.lock_bumps().len(), 0);
assert!(arena.bump().allocated_bytes() >= 10_000);
}
assert_eq!(pool.lock_bumps().len(), 1);
}
#[test]
fn should_keep_the_memory_of_arenas_alive_at_the_same_time() {
let pool = ArenaPool::new();
{
let _outer = pool.create_arena();
let _inner = pool.create_arena();
}
assert_eq!(pool.lock_bumps().len(), 2);
}
#[test]
fn should_free_the_memory_when_cleared() {
let pool = ArenaPool::new();
drop(pool.create_arena());
assert_eq!(pool.lock_bumps().len(), 1);
pool.clear();
assert_eq!(pool.lock_bumps().len(), 0);
}
#[test]
fn should_not_keep_the_memory_of_arenas_without_a_pool() {
let pool = ArenaPool::new();
drop(Arena::new());
assert_eq!(pool.lock_bumps().len(), 0);
}
#[test]
fn should_be_able_to_share_the_pool_between_threads() {
fn assert_send_sync<T: Send + Sync>() {}
assert_send_sync::<ArenaPool>();
}
}