Skip to content

Commit 2329579

Browse files
committed
Switching to Mmap::advise
1 parent 71e6738 commit 2329579

File tree

3 files changed

+19
-31
lines changed

3 files changed

+19
-31
lines changed

Cargo.toml

-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ base64 = "0.21.0"
1919
byteorder = "1.4.3"
2020
crc32fast = "1.3.2"
2121
once_cell = "1.10.0"
22-
madvise = { version="0.1", optional= true }
2322
regex = { version = "1.5.5", default-features = false, features = ["std", "unicode"] }
2423
aho-corasick = "1.0"
2524
tantivy-fst = "0.4.0"
@@ -101,7 +100,6 @@ overflow-checks = true
101100
default = ["mmap", "stopwords", "lz4-compression"]
102101
mmap = ["fs4", "tempfile", "memmap2"]
103102
stopwords = []
104-
madvise = ["dep:madvise"]
105103

106104
brotli-compression = ["brotli"]
107105
lz4-compression = ["lz4_flex"]

src/directory/mmap_directory.rs

+18-27
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@ use std::{fmt, result};
88

99
use common::StableDeref;
1010
use fs4::FileExt;
11-
12-
#[cfg(feature = "madvise")]
13-
use crate::AccessPattern;
14-
#[cfg(not(feature = "madvise"))]
15-
type AccessPattern = ();
16-
1711
use memmap2::Mmap;
1812
use serde::{Deserialize, Serialize};
1913
use tempfile::TempDir;
@@ -27,6 +21,7 @@ use crate::directory::{
2721
AntiCallToken, Directory, DirectoryLock, FileHandle, Lock, OwnedBytes, TerminatingWrite,
2822
WatchCallback, WatchHandle, WritePtr,
2923
};
24+
use crate::Advice;
3025

3126
pub type ArcBytes = Arc<dyn Deref<Target = [u8]> + Send + Sync + 'static>;
3227
pub type WeakArcBytes = Weak<dyn Deref<Target = [u8]> + Send + Sync + 'static>;
@@ -38,10 +33,9 @@ pub(crate) fn make_io_err(msg: String) -> io::Error {
3833

3934
/// Returns `None` iff the file exists, can be read, but is empty (and hence
4035
/// cannot be mmapped)
41-
#[allow(unused_variables)]
4236
fn open_mmap(
4337
full_path: &Path,
44-
access_pattern_opt: Option<AccessPattern>,
38+
madvice_opt: Option<Advice>,
4539
) -> result::Result<Option<Mmap>, OpenReadError> {
4640
let file = File::open(full_path).map_err(|io_err| {
4741
if io_err.kind() == io::ErrorKind::NotFound {
@@ -65,11 +59,9 @@ fn open_mmap(
6559
.map(Some)
6660
.map_err(|io_err| OpenReadError::wrap_io_error(io_err, full_path.to_path_buf()))
6761
}?;
68-
#[cfg(feature = "madvise")]
69-
match (&mmap_opt, access_pattern_opt) {
70-
(Some(mmap), Some(access_pattern)) => {
71-
use madvise::AdviseMemory;
72-
let _ = mmap.advise_memory_access(access_pattern);
62+
match (&mmap_opt, madvice_opt) {
63+
(Some(mmap), Some(madvice)) => {
64+
let _ = mmap.advise(madvice);
7365
}
7466
_ => {}
7567
}
@@ -94,15 +86,15 @@ pub struct CacheInfo {
9486
struct MmapCache {
9587
counters: CacheCounters,
9688
cache: HashMap<PathBuf, WeakArcBytes>,
97-
access_pattern_opt: Option<AccessPattern>,
89+
madvice_opt: Option<Advice>,
9890
}
9991

10092
impl MmapCache {
101-
fn new(access_pattern_opt: Option<AccessPattern>) -> MmapCache {
93+
fn new(madvice_opt: Option<Advice>) -> MmapCache {
10294
MmapCache {
10395
counters: CacheCounters::default(),
10496
cache: HashMap::default(),
105-
access_pattern_opt,
97+
madvice_opt,
10698
}
10799
}
108100

@@ -136,7 +128,7 @@ impl MmapCache {
136128
}
137129
self.cache.remove(full_path);
138130
self.counters.miss += 1;
139-
let mmap_opt = open_mmap(full_path, self.access_pattern_opt)?;
131+
let mmap_opt = open_mmap(full_path, self.madvice_opt)?;
140132
Ok(mmap_opt.map(|mmap| {
141133
let mmap_arc: ArcBytes = Arc::new(mmap);
142134
let mmap_weak = Arc::downgrade(&mmap_arc);
@@ -174,10 +166,10 @@ impl MmapDirectoryInner {
174166
fn new(
175167
root_path: PathBuf,
176168
temp_directory: Option<TempDir>,
177-
access_pattern_opt: Option<AccessPattern>,
169+
madvice_opt: Option<Advice>,
178170
) -> MmapDirectoryInner {
179171
MmapDirectoryInner {
180-
mmap_cache: RwLock::new(MmapCache::new(access_pattern_opt)),
172+
mmap_cache: RwLock::new(MmapCache::new(madvice_opt)),
181173
_temp_directory: temp_directory,
182174
watcher: FileWatcher::new(&root_path.join(*META_FILEPATH)),
183175
root_path,
@@ -199,9 +191,9 @@ impl MmapDirectory {
199191
fn new(
200192
root_path: PathBuf,
201193
temp_directory: Option<TempDir>,
202-
access_pattern_opt: Option<AccessPattern>,
194+
madvice_opt: Option<Advice>,
203195
) -> MmapDirectory {
204-
let inner = MmapDirectoryInner::new(root_path, temp_directory, access_pattern_opt);
196+
let inner = MmapDirectoryInner::new(root_path, temp_directory, madvice_opt);
205197
MmapDirectory {
206198
inner: Arc::new(inner),
207199
}
@@ -230,17 +222,16 @@ impl MmapDirectory {
230222
}
231223

232224
/// Opens a MmapDirectory in a directory, with a given access pattern.
233-
#[cfg(feature = "madvise")]
234-
pub fn open_with_access_pattern<P: AsRef<Path>>(
225+
pub fn open_with_madvice<P: AsRef<Path>>(
235226
directory_path: P,
236-
access_pattern: AccessPattern,
227+
madvice: Advice,
237228
) -> Result<MmapDirectory, OpenDirectoryError> {
238-
Self::open_with_access_pattern_impl(directory_path.as_ref(), Some(access_pattern))
229+
Self::open_with_access_pattern_impl(directory_path.as_ref(), Some(madvice))
239230
}
240231

241232
fn open_with_access_pattern_impl(
242233
directory_path: &Path,
243-
access_pattern_opt: Option<AccessPattern>,
234+
madvice_opt: Option<Advice>,
244235
) -> Result<MmapDirectory, OpenDirectoryError> {
245236
let directory_path: &Path = directory_path.as_ref();
246237
if !directory_path.exists() {
@@ -269,7 +260,7 @@ impl MmapDirectory {
269260
directory_path,
270261
)));
271262
}
272-
Ok(MmapDirectory::new(canonical_path, None, access_pattern_opt))
263+
Ok(MmapDirectory::new(canonical_path, None, madvice_opt))
273264
}
274265

275266
/// Joins a relative_path to the directory `root_path`

src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,7 @@ pub use crate::schema::{DateOptions, DatePrecision, Document, Term};
189189
/// Index format version.
190190
const INDEX_FORMAT_VERSION: u32 = 5;
191191

192-
#[cfg(feature = "madvise")]
193-
pub use madvise::AccessPattern;
192+
pub use memmap2::Advice;
194193

195194
/// Structure version for the index.
196195
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]

0 commit comments

Comments
 (0)