@@ -16,6 +16,13 @@ use subspace_networking::utils::multihash::ToMultihash;
16
16
use thiserror:: Error ;
17
17
use tracing:: { debug, info, warn} ;
18
18
19
+ /// Max plot space for which to use caching, for larger gaps between the plotted part and the end of
20
+ /// the file it will result in very long period of writing zeroes on Windows, see
21
+ /// https://stackoverflow.com/q/78058306/3806795
22
+ ///
23
+ /// Currently set to 2TiB.
24
+ const MAX_WINDOWS_PLOT_SPACE_FOR_CACHE : u64 = 2 * 1024 * 1024 * 1024 * 1024 ;
25
+
19
26
/// Disk plot cache open error
20
27
#[ derive( Debug , Error ) ]
21
28
pub enum DiskPlotCacheError {
@@ -68,33 +75,36 @@ impl DiskPlotCache {
68
75
// Clippy complains about `RecordKey`, but it is not changing here, so it is fine
69
76
#[ allow( clippy:: mutable_key_type) ]
70
77
let mut map = HashMap :: new ( ) ;
78
+ let mut next_offset = None ;
71
79
72
80
let file_size = sector_size * u64:: from ( target_sector_count) ;
73
81
let plotted_size = sector_size * sectors_metadata. len ( ) as u64 ;
74
82
75
- // Step over all free potential offsets for pieces that could have been cached
76
- let from_offset = ( plotted_size / Self :: element_size ( ) as u64 ) as u32 ;
77
- let to_offset = ( file_size / Self :: element_size ( ) as u64 ) as u32 ;
78
- let mut next_offset = None ;
79
- // TODO: Parallelize or read in larger batches
80
- for offset in ( from_offset..to_offset) . rev ( ) {
81
- match Self :: read_piece_internal ( file, offset, & mut element) {
82
- Ok ( maybe_piece_index) => match maybe_piece_index {
83
- Some ( piece_index) => {
84
- map. insert ( RecordKey :: from ( piece_index. to_multihash ( ) ) , offset) ;
85
- }
86
- None => {
83
+ // Avoid writing over large gaps on Windows that is very lengthy process
84
+ if !cfg ! ( windows) || ( file_size - plotted_size) <= MAX_WINDOWS_PLOT_SPACE_FOR_CACHE {
85
+ // Step over all free potential offsets for pieces that could have been cached
86
+ let from_offset = ( plotted_size / Self :: element_size ( ) as u64 ) as u32 ;
87
+ let to_offset = ( file_size / Self :: element_size ( ) as u64 ) as u32 ;
88
+ // TODO: Parallelize or read in larger batches
89
+ for offset in ( from_offset..to_offset) . rev ( ) {
90
+ match Self :: read_piece_internal ( file, offset, & mut element) {
91
+ Ok ( maybe_piece_index) => match maybe_piece_index {
92
+ Some ( piece_index) => {
93
+ map. insert ( RecordKey :: from ( piece_index. to_multihash ( ) ) , offset) ;
94
+ }
95
+ None => {
96
+ next_offset. replace ( offset) ;
97
+ break ;
98
+ }
99
+ } ,
100
+ Err ( DiskPlotCacheError :: ChecksumMismatch ) => {
87
101
next_offset. replace ( offset) ;
88
102
break ;
89
103
}
90
- } ,
91
- Err ( DiskPlotCacheError :: ChecksumMismatch ) => {
92
- next_offset. replace ( offset) ;
93
- break ;
94
- }
95
- Err ( error) => {
96
- warn ! ( %error, %offset, "Failed to read plot cache element" ) ;
97
- break ;
104
+ Err ( error) => {
105
+ warn ! ( %error, %offset, "Failed to read plot cache element" ) ;
106
+ break ;
107
+ }
98
108
}
99
109
}
100
110
}
0 commit comments