Repository: MystenLabs/sui
File: external-crates/move/crates/move-vm-runtime/src/cache/arena.rs (Rust)

Code before:
// Copyright (c) The Move Contributors
// SPDX-License-Identifier: Apache-2.0

#![allow(unsafe_code)]

use move_binary_format::errors::{PartialVMError, PartialVMResult};
use move_core_types::vm_status::StatusCode;

use bumpalo::Bump;

use crate::shared::vm_pointer::VMPointer;

// -------------------------------------------------------------------------------------------------
// Types - Arenas for Cache Allocations
// -------------------------------------------------------------------------------------------------

pub struct Arena(Bump);

/// An arena-allocated vector. Notably, `Drop` does not drop the elements it holds, as that is the
/// perview of the arena it was allocated in.
#[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ArenaVec<T>(pub(crate) std::mem::ManuallyDrop<Vec<T>>);

/// An arena-allocated vector. Notably, `Drop` does not drop the value it holds, as that is the
/// perview of the arena it was allocated in.
#[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ArenaBox<T>(std::mem::ManuallyDrop<Box<T>>);

/// Size of a package arena.
/// This is 10 megabytes, which should be more than enough room for any pacakge on chain.
/// FIXME: Test this limit and validate. See how large packages are in backtesting and double that
/// limit, setting it here.
const ARENA_SIZE: usize = 10_000_000;

// -------------------------------------------------------------------------------------------------
// Impls
// -------------------------------------------------------------------------------------------------

impl Arena {
    pub fn new_bounded() -> Self {
        let bump = Bump::new();
        bump.set_allocation_limit(Some(ARENA_SIZE));
        Arena(bump)
    }

    /// SAFETY:
    /// 1. It is the caller's responsibility to ensure that `self` is not shared across threads

Review comment (refactor):
I'd probably keep this field private to keep a bit more of an abstraction barrier here, and then add a `drain` (or `drain_all`) function to the impl for `ArenaVec` and use that. 

Code after:
// Copyright (c) The Move Contributors
// SPDX-License-Identifier: Apache-2.0

#![allow(unsafe_code)]

use move_binary_format::errors::{PartialVMError, PartialVMResult};
use move_core_types::vm_status::StatusCode;

use bumpalo::Bump;

use crate::shared::vm_pointer::VMPointer;

// -------------------------------------------------------------------------------------------------
// Types - Arenas for Cache Allocations
// -------------------------------------------------------------------------------------------------

pub struct Arena(Bump);

/// An arena-allocated vector. Notably, `Drop` does not drop the elements it holds, as that is the
/// perview of the arena it was allocated in.
#[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ArenaVec<T>(std::mem::ManuallyDrop<Vec<T>>);

/// An arena-allocated vector. Notably, `Drop` does not drop the value it holds, as that is the
/// perview of the arena it was allocated in.
#[derive(Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ArenaBox<T>(std::mem::ManuallyDrop<Box<T>>);

/// Size of a package arena.
/// This is 10 megabytes, which should be more than enough room for any pacakge on chain.
/// FIXME: Test this limit and validate. See how large packages are in backtesting and double that
/// limit, setting it here.
const ARENA_SIZE: usize = 10_000_000;

// -------------------------------------------------------------------------------------------------
// Impls
// -------------------------------------------------------------------------------------------------

impl Arena {
    pub fn new_bounded() -> Self {
        let bump = Bump::new();
        bump.set_allocation_limit(Some(ARENA_SIZE));
        Arena(bump)
    }

    /// SAFETY:
    /// 1. It is the caller's responsibility to ensure that `self` is not shared across threads