Skip to content

Commit 0713bb4

Browse files
committed
Use typed-arena
1 parent a47b267 commit 0713bb4

File tree

7 files changed

+42
-52
lines changed

7 files changed

+42
-52
lines changed

Cargo.lock

Lines changed: 9 additions & 19 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ categories = ["mathematics", "science"]
1616
[dependencies]
1717
ahash = "0.8.3"
1818
auto_ops = "0.3.0"
19-
elsa = { version = "1.9.0", features = ["indexmap"] }
2019
itertools = "0.10.5"
2120
nom = "7.1.3"
2221
num-rational = { version = "0.4.1", default-features = false, features = ["std"]}
2322
# Later versions introduce binary blob: https://github.com/serde-rs/serde/issues/2538
2423
serde = { version = "<=1.0.171", optional = true}
2524
serde_repr = { version = "0.1.12", optional = true}
25+
typed-arena = "2.0.2"
2626

2727
[dev-dependencies]
2828
quickcheck = { version = "1.0", default-features = false }

src/short/partizan/canonical_form.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1378,7 +1378,7 @@ fn parse_games() {
13781378
test_game_parse!("{1/2|2}", "1");
13791379
}
13801380

1381-
#[cfg(test)]
1381+
#[cfg(all(test, not(miri)))]
13821382
mod tests {
13831383
use super::*;
13841384
use quickcheck::{Arbitrary, Gen, QuickCheck};

src/short/partizan/games/domineering.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,7 @@ fn finds_canonical_form_of_num_nim_sum() {
719719
}
720720

721721
#[test]
722+
#[cfg(not(miri))]
722723
fn finds_temperature_of_four_by_four_grid() {
723724
use crate::numeric::rational::Rational;
724725

src/short/partizan/partizan_game.rs

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ pub trait PartizanGame: Sized + Clone + Hash + Send + Sync + Eq {
7070
transposition_table: &'a TranspositionTable<'a, Self>,
7171
) -> CanonicalForm {
7272
if let Some(id) = transposition_table.grids_get(self) {
73-
return id;
73+
return id.clone();
7474
}
7575

7676
if let Some(cf) = self.canonical_form_special_cases() {
@@ -79,26 +79,25 @@ pub trait PartizanGame: Sized + Clone + Hash + Send + Sync + Eq {
7979

8080
let mut result = CanonicalForm::new_integer(0);
8181
for position in self.decompositions() {
82-
let sub_result = transposition_table.grids_get(&position).unwrap_or_else(|| {
83-
let moves = Moves {
84-
left: position
85-
.left_moves()
86-
.iter()
87-
.map(|o| o.canonical_form(transposition_table))
88-
.collect(),
89-
right: position
90-
.right_moves()
91-
.iter()
92-
.map(|o| o.canonical_form(transposition_table))
93-
.collect(),
94-
};
95-
96-
let canonical_form = CanonicalForm::new_from_moves(moves);
97-
transposition_table.grids_insert(position, canonical_form.clone());
98-
canonical_form
99-
});
100-
101-
result = sub_result + result;
82+
match transposition_table.grids_get(&position) {
83+
Some(cached_sub_result) => result += cached_sub_result,
84+
None => {
85+
let moves = Moves {
86+
left: position
87+
.left_moves()
88+
.iter()
89+
.map(|o| o.canonical_form(transposition_table))
90+
.collect(),
91+
right: position
92+
.right_moves()
93+
.iter()
94+
.map(|o| o.canonical_form(transposition_table))
95+
.collect(),
96+
};
97+
let sub_result = CanonicalForm::new_from_moves(moves);
98+
result += sub_result;
99+
}
100+
}
102101
}
103102

104103
transposition_table.grids_insert(self.clone(), result.clone());

src/short/partizan/transposition_table.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,25 @@
11
//! Thread safe transposition table for game values
22
3-
use elsa::FrozenIndexSet;
4-
5-
// TODO: Move to short positional game module
63
use crate::{rw_hash_map::RwHashMap, short::partizan::canonical_form::CanonicalForm};
74
use std::{hash::Hash, sync::Mutex};
5+
use typed_arena::Arena;
86

97
/// Transaction table (cache) of game positions and canonical forms.
108
pub struct TranspositionTable<'a, G> {
11-
known_games: Mutex<FrozenIndexSet<Box<CanonicalForm>>>,
9+
known_games: Mutex<Arena<CanonicalForm>>,
1210
grids: RwHashMap<G, &'a CanonicalForm, ahash::RandomState>,
1311
}
1412

1513
#[cfg_attr(feature = "cargo-clippy", allow(clippy::new_without_default))]
1614
impl<'a, G> TranspositionTable<'a, G>
1715
where
18-
G: Eq + Hash + Clone + Sync + Send,
16+
G: Eq + Hash + Sync + Send,
1917
{
2018
/// Create new empty transposition table.
2119
#[inline]
2220
pub fn new() -> Self {
2321
Self {
24-
known_games: Mutex::new(FrozenIndexSet::new()),
22+
known_games: Mutex::new(Arena::new()),
2523
grids: RwHashMap::with_hasher(ahash::RandomState::new()),
2624
}
2725
}
@@ -30,16 +28,17 @@ where
3028

3129
/// Lookup a position
3230
#[inline]
33-
pub fn grids_get(&self, grid: &G) -> Option<CanonicalForm> {
34-
self.grids.get(grid).cloned()
31+
pub fn grids_get(&'a self, grid: &G) -> Option<&'a CanonicalForm> {
32+
self.grids.get(grid)
3533
}
3634

3735
/// Save position and its game value
3836
#[cfg_attr(feature = "cargo-clippy", allow(clippy::missing_panics_doc))]
3937
#[inline]
4038
pub fn grids_insert(&'a self, grid: G, game: CanonicalForm) {
41-
let inserted: *const CanonicalForm =
42-
self.known_games.lock().unwrap().insert(Box::new(game));
39+
let arena = self.known_games.lock().unwrap();
40+
let inserted = arena.alloc(game) as *const CanonicalForm;
41+
drop(arena);
4342
self.grids.insert(grid, unsafe { &*inserted });
4443
}
4544

tests/domineering_bench.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use std::hint::black_box;
88
static ALLOC: dhat::Alloc = dhat::Alloc;
99

1010
#[test]
11+
#[cfg(not(miri))]
1112
fn bench_domineering() {
1213
let profiler = dhat::Profiler::builder().build();
1314

0 commit comments

Comments
 (0)