Skip to content

Commit

Permalink
allow sharing filesystem between tablebase instances
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf committed Jun 6, 2024
1 parent 39137de commit 0b773bd
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 7 deletions.
3 changes: 2 additions & 1 deletion fuzz/fuzz_targets/pawnful.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
use std::{
io,
path::{Path, PathBuf},
sync::Arc,
};

use libfuzzer_sys::fuzz_target;
Expand Down Expand Up @@ -56,6 +57,6 @@ fuzz_target!(|data: &[u8]| {
.into_position(CastlingMode::Standard)
.expect("valid position");

let tables = Tablebase::with_filesystem(Box::new(FakeFilesystem { data: data.into() }));
let tables = Tablebase::with_filesystem(Arc::new(FakeFilesystem { data: data.into() }));
let _ = tables.probe_wdl(&pos);
});
4 changes: 2 additions & 2 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{
};

/// An abstract filesystem.
pub trait Filesystem: Send + Sync {
pub trait Filesystem: Sync + Send {
/// Determines the size in bytes of the given file.
///
/// Follows symbolic links.
Expand Down Expand Up @@ -51,7 +51,7 @@ pub enum ReadHint {
}

/// An abstract randomly readable file.
pub trait RandomAccessFile: Send + Sync {
pub trait RandomAccessFile: Sync + Send {
/// Reads some bytes starting from a given offset.
///
/// See [`std::os::unix::fs::FileExt::read_at()`] for precise semantics.
Expand Down
9 changes: 5 additions & 4 deletions src/tablebase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::{
ffi::OsStr,
fmt, io,
path::{Path, PathBuf},
sync::Arc,
};

use arrayvec::ArrayVec;
Expand Down Expand Up @@ -35,7 +36,7 @@ enum ProbeState {

/// A collection of tables.
pub struct Tablebase<S: Position + Clone + Syzygy> {
filesystem: Box<dyn Filesystem>,
filesystem: Arc<dyn Filesystem>,
wdl: FxHashMap<Material, (PathBuf, OnceCell<WdlTable<S>>)>,
dtz: FxHashMap<Material, (PathBuf, OnceCell<DtzTable<S>>)>,
max_pieces: usize,
Expand Down Expand Up @@ -63,7 +64,7 @@ impl<S: Position + Clone + Syzygy> Tablebase<S> {
/// implementation will be used to read table files.
#[cfg(any(unix, windows))]
pub fn new() -> Tablebase<S> {
Tablebase::with_filesystem(Box::new(filesystem::os::OsFilesystem))
Tablebase::with_filesystem(Arc::new(filesystem::os::OsFilesystem))
}

/// Creates an empty collection of tables. Memory mapping will be used
Expand All @@ -85,12 +86,12 @@ impl<S: Position + Clone + Syzygy> Tablebase<S> {
pub unsafe fn with_mmap_filesystem() -> Tablebase<S> {
// Safety: Forwarding contract of memmap2::MmapOptions::map()
// to caller.
Tablebase::with_filesystem(unsafe { Box::new(filesystem::mmap::MmapFilesystem::new()) })
Tablebase::with_filesystem(unsafe { Arc::new(filesystem::mmap::MmapFilesystem::new()) })
}

/// Creates an empty collection of tables. A custom filesystem
/// implementation will be used to read table files.
pub fn with_filesystem(filesystem: Box<dyn Filesystem>) -> Tablebase<S> {
pub fn with_filesystem(filesystem: Arc<dyn Filesystem>) -> Tablebase<S> {
Tablebase {
filesystem,
wdl: FxHashMap::with_capacity_and_hasher(145, Default::default()),
Expand Down

0 comments on commit 0b773bd

Please sign in to comment.