@@ -7,7 +7,7 @@ use std::io::{self, Seek, SeekFrom, Write};
7
7
use std:: mem:: MaybeUninit ;
8
8
use std:: ops:: Deref ;
9
9
use std:: os:: unix:: prelude:: FileExt ;
10
- use std:: path:: PathBuf ;
10
+ use std:: path:: { Path , PathBuf } ;
11
11
use std:: sync:: atomic:: { self , AtomicUsize } ;
12
12
use std:: sync:: { Arc , Condvar , Mutex , RwLock , Weak } ;
13
13
@@ -45,8 +45,8 @@ pub struct Domain {
45
45
}
46
46
47
47
impl Domain {
48
- fn open ( dir : & PathBuf , name : & str , index : usize ) -> io:: Result < Self > {
49
- let mut path = dir. clone ( ) ;
48
+ fn open ( dir : & Path , name : & str , index : usize ) -> io:: Result < Self > {
49
+ let mut path = dir. to_path_buf ( ) ;
50
50
let name = encode ( name) ;
51
51
path. push ( format ! ( "{name}.vecs" ) ) ;
52
52
let mut write_file = File :: options ( )
@@ -150,9 +150,11 @@ struct PageSpec {
150
150
index : usize ,
151
151
}
152
152
153
+ type GuardedLoadState = Arc < ( Condvar , Mutex < LoadState > ) > ;
154
+
153
155
struct PageArena {
154
156
free : Mutex < Vec < Box < VectorPage > > > ,
155
- loading : Mutex < HashMap < PageSpec , Arc < ( Condvar , Mutex < LoadState > ) > > > ,
157
+ loading : Mutex < HashMap < PageSpec , GuardedLoadState > > ,
156
158
loaded : RwLock < HashMap < PageSpec , PinnedVectorPage > > ,
157
159
cache : RwLock < LruCache < PageSpec , LoadedVectorPage > > ,
158
160
}
@@ -167,10 +169,7 @@ enum LoadState {
167
169
168
170
impl LoadState {
169
171
fn is_loading ( & self ) -> bool {
170
- match self {
171
- Self :: Loading => true ,
172
- _ => false ,
173
- }
172
+ matches ! ( self , Self :: Loading )
174
173
}
175
174
}
176
175
@@ -230,7 +229,7 @@ impl PageArena {
230
229
231
230
fn start_loading_or_wait ( self : & Arc < Self > , spec : PageSpec ) -> LoadState {
232
231
let mut loading = self . loading . lock ( ) . unwrap ( ) ;
233
- if let Some ( x) = loading. get ( & spec) . map ( |x| x . clone ( ) ) {
232
+ if let Some ( x) = loading. get ( & spec) . cloned ( ) {
234
233
// someone is already loading. Let's wait.
235
234
std:: mem:: drop ( loading) ;
236
235
let ( cv, m) = & * x;
@@ -332,9 +331,7 @@ impl PageArena {
332
331
let mut cache = self . cache . write ( ) . unwrap ( ) ;
333
332
334
333
let page = cache. pop ( & spec) ;
335
- if page. is_none ( ) {
336
- return None ;
337
- }
334
+ page. as_ref ( ) ?;
338
335
let page = page. unwrap ( ) ;
339
336
let handle = Arc :: new ( PageHandle {
340
337
spec,
@@ -453,7 +450,7 @@ impl PageHandle {
453
450
// returns the pagehandle as an arc, and the page doesn't get
454
451
// moved out of the loaded pages unless this arc's refcount is
455
452
// 0.
456
- unsafe { & * ( self . p as * const Embedding ) . offset ( index as isize ) }
453
+ unsafe { & * ( self . p as * const Embedding ) . add ( index) }
457
454
}
458
455
459
456
pub fn get_loaded_vec ( self : & Arc < Self > , index : usize ) -> LoadedVec {
@@ -464,7 +461,7 @@ impl PageHandle {
464
461
) ;
465
462
}
466
463
467
- let vec = unsafe { ( self . p as * const Embedding ) . offset ( index as isize ) } ;
464
+ let vec = unsafe { ( self . p as * const Embedding ) . add ( index) } ;
468
465
LoadedVec {
469
466
page : self . clone ( ) ,
470
467
vec,
0 commit comments