@@ -52,6 +52,25 @@ pub struct BuilderNeighborCache {
52
52
}
53
53
54
54
impl BuilderNeighborCache {
55
+ fn reconcile_with_disk_neighbors < S : Storage > (
56
+ & self ,
57
+ neighbors_of : ItemPointer ,
58
+ cached_neighbors : Vec < NeighborWithDistance > ,
59
+ storage : & S ,
60
+ stats : & mut PruneNeighborStats ,
61
+ ) -> Vec < NeighborWithDistance > {
62
+ let disk_neighbors = storage. get_neighbors_with_distances_from_disk ( neighbors_of, stats) ;
63
+ let mut all_neighbors = cached_neighbors;
64
+ for disk_neighbor in disk_neighbors {
65
+ if !all_neighbors. iter ( ) . any ( |n : & NeighborWithDistance | {
66
+ n. get_index_pointer_to_neighbor ( ) == disk_neighbor. get_index_pointer_to_neighbor ( )
67
+ } ) {
68
+ all_neighbors. push ( disk_neighbor) ;
69
+ }
70
+ }
71
+ all_neighbors
72
+ }
73
+
55
74
pub fn new ( memory_budget : f64 , meta_page : & MetaPage , worker_count : usize ) -> Self {
56
75
let total_memory = maintenance_work_mem_bytes ( ) as f64 ;
57
76
let memory_budget = ( total_memory * memory_budget) . ceil ( ) as usize ;
@@ -130,18 +149,8 @@ impl BuilderNeighborCache {
130
149
let evictee =
131
150
neighbor_map. push ( neighbors_of, NeighborCacheEntry :: new ( labels, new_neighbors) ) ;
132
151
if let Some ( ( key, value) ) = evictee {
133
- // Read existing neighbors from disk and merge with cached neighbors
134
- let disk_neighbors =
135
- storage. get_neighbors_with_distances_from_disk ( neighbors_of, stats) ;
136
- let mut all_neighbors = value. neighbors ;
137
- for disk_neighbor in disk_neighbors {
138
- if !all_neighbors. iter ( ) . any ( |n : & NeighborWithDistance | {
139
- n. get_index_pointer_to_neighbor ( )
140
- == disk_neighbor. get_index_pointer_to_neighbor ( )
141
- } ) {
142
- all_neighbors. push ( disk_neighbor) ;
143
- }
144
- }
152
+ let all_neighbors =
153
+ self . reconcile_with_disk_neighbors ( key, value. neighbors , storage, stats) ;
145
154
let new_neighbors = Graph :: prune_neighbors (
146
155
self . max_alpha ,
147
156
self . num_neighbors ,
@@ -166,18 +175,8 @@ impl BuilderNeighborCache {
166
175
while cache. len ( ) > 0 {
167
176
let ( neighbors_of, entry) = cache. pop_lru ( ) . unwrap ( ) ;
168
177
drop ( cache) ;
169
- // Read existing neighbors from disk and merge with cached neighbors
170
- let disk_neighbors =
171
- storage. get_neighbors_with_distances_from_disk ( neighbors_of, stats) ;
172
- let mut all_neighbors = entry. neighbors ;
173
- for disk_neighbor in disk_neighbors {
174
- if !all_neighbors. iter ( ) . any ( |n : & NeighborWithDistance | {
175
- n. get_index_pointer_to_neighbor ( )
176
- == disk_neighbor. get_index_pointer_to_neighbor ( )
177
- } ) {
178
- all_neighbors. push ( disk_neighbor) ;
179
- }
180
- }
178
+ let all_neighbors =
179
+ self . reconcile_with_disk_neighbors ( neighbors_of, entry. neighbors , storage, stats) ;
181
180
182
181
let pruned_neighbors = if all_neighbors. len ( ) > self . num_neighbors {
183
182
Graph :: prune_neighbors (
0 commit comments