Skip to content

Commit b4681af

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

File tree

3 files changed

+19
-30
lines changed

3 files changed

+19
-30
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-26
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>;
@@ -41,7 +36,7 @@ pub(crate) fn make_io_err(msg: String) -> io::Error {
4136
#[allow(unused_variables)]
4237
fn open_mmap(
4338
full_path: &Path,
44-
access_pattern_opt: Option<AccessPattern>,
39+
madvise_opt: Option<Advice>,
4540
) -> result::Result<Option<Mmap>, OpenReadError> {
4641
let file = File::open(full_path).map_err(|io_err| {
4742
if io_err.kind() == io::ErrorKind::NotFound {
@@ -65,11 +60,9 @@ fn open_mmap(
6560
.map(Some)
6661
.map_err(|io_err| OpenReadError::wrap_io_error(io_err, full_path.to_path_buf()))
6762
}?;
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);
63+
match (&mmap_opt, madvise_opt) {
64+
(Some(mmap), Some(madvise)) => {
65+
let _ = mmap.advise(madvise);
7366
}
7467
_ => {}
7568
}
@@ -94,15 +87,15 @@ pub struct CacheInfo {
9487
struct MmapCache {
9588
counters: CacheCounters,
9689
cache: HashMap<PathBuf, WeakArcBytes>,
97-
access_pattern_opt: Option<AccessPattern>,
90+
madvise_opt: Option<Advice>,
9891
}
9992

10093
impl MmapCache {
101-
fn new(access_pattern_opt: Option<AccessPattern>) -> MmapCache {
94+
fn new(madvise_opt: Option<Advice>) -> MmapCache {
10295
MmapCache {
10396
counters: CacheCounters::default(),
10497
cache: HashMap::default(),
105-
access_pattern_opt,
98+
madvise_opt,
10699
}
107100
}
108101

@@ -136,7 +129,7 @@ impl MmapCache {
136129
}
137130
self.cache.remove(full_path);
138131
self.counters.miss += 1;
139-
let mmap_opt = open_mmap(full_path, self.access_pattern_opt)?;
132+
let mmap_opt = open_mmap(full_path, self.madvise_opt)?;
140133
Ok(mmap_opt.map(|mmap| {
141134
let mmap_arc: ArcBytes = Arc::new(mmap);
142135
let mmap_weak = Arc::downgrade(&mmap_arc);
@@ -174,10 +167,10 @@ impl MmapDirectoryInner {
174167
fn new(
175168
root_path: PathBuf,
176169
temp_directory: Option<TempDir>,
177-
access_pattern_opt: Option<AccessPattern>,
170+
madvise_opt: Option<Advice>,
178171
) -> MmapDirectoryInner {
179172
MmapDirectoryInner {
180-
mmap_cache: RwLock::new(MmapCache::new(access_pattern_opt)),
173+
mmap_cache: RwLock::new(MmapCache::new(madvise_opt)),
181174
_temp_directory: temp_directory,
182175
watcher: FileWatcher::new(&root_path.join(*META_FILEPATH)),
183176
root_path,
@@ -199,9 +192,9 @@ impl MmapDirectory {
199192
fn new(
200193
root_path: PathBuf,
201194
temp_directory: Option<TempDir>,
202-
access_pattern_opt: Option<AccessPattern>,
195+
madvise_opt: Option<Advice>,
203196
) -> MmapDirectory {
204-
let inner = MmapDirectoryInner::new(root_path, temp_directory, access_pattern_opt);
197+
let inner = MmapDirectoryInner::new(root_path, temp_directory, madvise_opt);
205198
MmapDirectory {
206199
inner: Arc::new(inner),
207200
}
@@ -230,17 +223,16 @@ impl MmapDirectory {
230223
}
231224

232225
/// 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>>(
226+
pub fn open_with_madvise<P: AsRef<Path>>(
235227
directory_path: P,
236-
access_pattern: AccessPattern,
228+
madvise: Advice,
237229
) -> Result<MmapDirectory, OpenDirectoryError> {
238-
Self::open_with_access_pattern_impl(directory_path.as_ref(), Some(access_pattern))
230+
Self::open_with_access_pattern_impl(directory_path.as_ref(), Some(madvise))
239231
}
240232

241233
fn open_with_access_pattern_impl(
242234
directory_path: &Path,
243-
access_pattern_opt: Option<AccessPattern>,
235+
madvise_opt: Option<Advice>,
244236
) -> Result<MmapDirectory, OpenDirectoryError> {
245237
let directory_path: &Path = directory_path.as_ref();
246238
if !directory_path.exists() {
@@ -269,7 +261,7 @@ impl MmapDirectory {
269261
directory_path,
270262
)));
271263
}
272-
Ok(MmapDirectory::new(canonical_path, None, access_pattern_opt))
264+
Ok(MmapDirectory::new(canonical_path, None, madvise_opt))
273265
}
274266

275267
/// 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)