Skip to content

Commit

Permalink
Add tests of the cost of cloning EvaluatedBlock.
Browse files Browse the repository at this point in the history
  • Loading branch information
kpreid committed Oct 15, 2023
1 parent c28079b commit e56bfe3
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ members = [
resolver = "2"

[workspace.dependencies]
allocation-counter = "0.8.1"
anyhow = "1.0.70"
# Each use should be { optional = true }.
arbitrary = { version = "1.1.6", features = ["derive"] }
Expand Down
1 change: 1 addition & 0 deletions all-is-cubes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ yield-progress = { workspace = true }
euclid = { version = "0.22.9", default-features = false, features = ["libm", "mint"] }

[dev-dependencies]
allocation-counter = { workspace = true }
criterion = { workspace = true }
pretty_assertions = { workspace = true }
serde_json = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions all-is-cubes/src/block/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub struct BlockAttributes {
///
/// The default value is the empty string. The empty string should be considered a
/// reasonable choice for solid-color blocks with no special features.
// ---
// TODO: Make this a refcounted type so we can make cloning O(1).
pub display_name: Cow<'static, str>,

/// Whether players' [cursors](crate::character::Cursor) target it or pass through it.
Expand Down
70 changes: 70 additions & 0 deletions all-is-cubes/tests/alloc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
//! Tests of memory allocation behavior.
//!
//! In a separate test crate to avoid modifying the global allocator elsewhere.
use std::borrow::Cow;

use all_is_cubes::universe::Universe;
use allocation_counter::{measure, AllocationInfo};

use all_is_cubes::block::{self, BlockAttributes};

/// TODO: Make the name refcounted so this count goes to zero.
#[test]
fn clone_block_attributes() {
let display_name: Cow<'static, str> = String::from("hello").into();
let original = BlockAttributes {
// This field will allocate when cloned
display_name: display_name.clone(),

// TODO: This field could allocate when cloned but we're not worrying about that now
tick_action: None,

// These fields currently will never allocate when cloned
selectable: true,
rotation_rule: block::RotationPlacementRule::Never,
animation_hint: block::AnimationHint::UNCHANGING,
};
let mut clone = None;

assert_eq!(
measure(|| {
clone = Some(original.clone());
}),
AllocationInfo {
count_total: 1,
count_current: 1,
count_max: 1,
bytes_total: display_name.len() as u64,
bytes_current: display_name.len() as i64,
bytes_max: display_name.len() as u64
}
)
}

/// Test that cloning an `EvaluatedBlock`, with voxels, allocates nothing (except
/// BlockAttributes as tested above).
#[test]
fn clone_evaluated_block() {
let universe = &mut Universe::new();
let [block] = all_is_cubes::content::make_some_voxel_blocks(universe);

let original = block.evaluate().unwrap();
// TODO: should be zero
let expected_bytes = original.attributes.display_name.len() as u64;
let mut clone = None;

assert_eq!(
measure(|| {
clone = Some(original.clone());
}),
AllocationInfo {
count_total: 1,
count_current: 1,
count_max: 1,
bytes_total: expected_bytes,
bytes_current: expected_bytes as i64,
bytes_max: expected_bytes
}
)
}

0 comments on commit e56bfe3

Please sign in to comment.