Skip to content

Commit d953dda

Browse files
authored
Move PreprocessorCacheModeConfig to src/config.rs (#2604)
* Extract cache::normalize_key to cache::utils::normalize_key * Move PreprocessorCacheModeConfig to src/config.rs
1 parent f99a589 commit d953dda

10 files changed

Lines changed: 87 additions & 79 deletions

File tree

src/cache/cache.rs

Lines changed: 6 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ use crate::cache::oss::OSSCache;
3030
use crate::cache::redis::RedisCache;
3131
#[cfg(feature = "s3")]
3232
use crate::cache::s3::S3Cache;
33-
#[cfg(feature = "webdav")]
34-
use crate::cache::webdav::WebdavCache;
35-
use crate::compiler::PreprocessorCacheEntry;
36-
use crate::config::Config;
3733
#[cfg(any(
3834
feature = "azure",
3935
feature = "gcs",
@@ -45,10 +41,14 @@ use crate::config::Config;
4541
feature = "oss",
4642
feature = "cos"
4743
))]
48-
use crate::config::{self, CacheType};
44+
use crate::cache::utils::normalize_key;
45+
#[cfg(feature = "webdav")]
46+
use crate::cache::webdav::WebdavCache;
47+
use crate::compiler::PreprocessorCacheEntry;
48+
use crate::config::Config;
49+
use crate::config::{self, CacheType, PreprocessorCacheModeConfig};
4950
use async_trait::async_trait;
5051

51-
use serde::{Deserialize, Serialize};
5252
use std::io;
5353
use std::sync::Arc;
5454
use std::time::Duration;
@@ -134,57 +134,6 @@ pub trait Storage: Send + Sync {
134134
}
135135
}
136136

137-
/// Configuration switches for preprocessor cache mode.
138-
#[derive(Debug, Copy, Clone, PartialEq, Eq, Serialize, Deserialize)]
139-
#[serde(deny_unknown_fields)]
140-
#[serde(default)]
141-
pub struct PreprocessorCacheModeConfig {
142-
/// Whether to use preprocessor cache mode entirely
143-
pub use_preprocessor_cache_mode: bool,
144-
/// If false (default), only compare header files by hashing their contents.
145-
/// If true, will use size + ctime + mtime to check whether a file has changed.
146-
/// See other flags below for more control over this behavior.
147-
pub file_stat_matches: bool,
148-
/// If true (default), uses the ctime (file status change on UNIX,
149-
/// creation time on Windows) to check that a file has/hasn't changed.
150-
/// Can be useful to disable when backdating modification times
151-
/// in a controlled manner.
152-
pub use_ctime_for_stat: bool,
153-
/// If true, ignore `__DATE__`, `__TIME__` and `__TIMESTAMP__` being present
154-
/// in the source code. Will speed up preprocessor cache mode,
155-
/// but can result in false positives.
156-
pub ignore_time_macros: bool,
157-
/// If true, preprocessor cache mode will not cache system headers, only
158-
/// add them to the hash.
159-
pub skip_system_headers: bool,
160-
/// If true (default), will add the current working directory in the hash to
161-
/// distinguish two compilations from different directories.
162-
pub hash_working_directory: bool,
163-
}
164-
165-
impl Default for PreprocessorCacheModeConfig {
166-
fn default() -> Self {
167-
Self {
168-
use_preprocessor_cache_mode: false,
169-
file_stat_matches: false,
170-
use_ctime_for_stat: true,
171-
ignore_time_macros: false,
172-
skip_system_headers: false,
173-
hash_working_directory: true,
174-
}
175-
}
176-
}
177-
178-
impl PreprocessorCacheModeConfig {
179-
/// Return a default [`Self`], but with the cache active.
180-
pub fn activated() -> Self {
181-
Self {
182-
use_preprocessor_cache_mode: true,
183-
..Default::default()
184-
}
185-
}
186-
}
187-
188137
/// Wrapper for opendal::Operator that adds basedirs support
189138
#[cfg(any(
190139
feature = "azure",
@@ -332,11 +281,6 @@ impl Storage for RemoteStorage {
332281
}
333282
}
334283

335-
/// Normalize key `abcdef` into `a/b/c/abcdef`
336-
pub(in crate::cache) fn normalize_key(key: &str) -> String {
337-
format!("{}/{}/{}/{}", &key[0..1], &key[1..2], &key[2..3], &key)
338-
}
339-
340284
/// Build a single cache storage from CacheType
341285
/// Helper function used by storage_from_config for both single and multi-level caches
342286
#[cfg(any(
@@ -587,14 +531,6 @@ mod test {
587531
use crate::config::CacheModeConfig;
588532
use fs_err as fs;
589533

590-
#[test]
591-
fn test_normalize_key() {
592-
assert_eq!(
593-
normalize_key("0123456789abcdef0123456789abcdef"),
594-
"0/1/2/0123456789abcdef0123456789abcdef"
595-
);
596-
}
597-
598534
#[test]
599535
fn test_read_write_mode_local() {
600536
let runtime = tokio::runtime::Builder::new_current_thread()

src/cache/disk.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ use std::time::{Duration, Instant};
2525
use crate::errors::*;
2626

2727
use super::lazy_disk_cache::LazyDiskCache;
28-
use super::{PreprocessorCacheModeConfig, normalize_key};
28+
use super::utils::normalize_key;
29+
use crate::config::PreprocessorCacheModeConfig;
2930

3031
/// A cache that stores entries at local disk paths.
3132
pub struct DiskCache {

src/cache/readonly.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ use async_trait::async_trait;
1717

1818
use crate::cache::{Cache, CacheMode, CacheWrite, Storage};
1919
use crate::compiler::PreprocessorCacheEntry;
20+
use crate::config::PreprocessorCacheModeConfig;
2021
use crate::errors::*;
2122

22-
use super::PreprocessorCacheModeConfig;
23-
2423
pub struct ReadOnlyStorage(pub Arc<dyn Storage>);
2524

2625
#[async_trait]

src/cache/utils.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ use std::path::Path;
1616

1717
use crate::errors::*;
1818

19+
/// Normalize key `abcdef` into `a/b/c/abcdef`
20+
pub(in crate::cache) fn normalize_key(key: &str) -> String {
21+
format!("{}/{}/{}/{}", &key[0..1], &key[1..2], &key[2..3], &key)
22+
}
23+
1924
#[cfg(unix)]
2025
pub(in crate::cache) fn get_file_mode(file: &fs::File) -> Result<Option<u32>> {
2126
use std::os::unix::fs::MetadataExt;
@@ -42,3 +47,16 @@ pub(in crate::cache) fn set_file_mode(path: &Path, mode: u32) -> Result<()> {
4247
pub(in crate::cache) fn set_file_mode(_path: &Path, _mode: u32) -> Result<()> {
4348
Ok(())
4449
}
50+
51+
#[cfg(test)]
52+
mod test {
53+
use super::*;
54+
55+
#[test]
56+
fn test_normalize_key() {
57+
assert_eq!(
58+
normalize_key("0123456789abcdef0123456789abcdef"),
59+
"0/1/2/0123456789abcdef0123456789abcdef"
60+
);
61+
}
62+
}

src/compiler/c.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,15 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::cache::{FileObjectSource, PreprocessorCacheModeConfig, Storage};
15+
use crate::cache::{FileObjectSource, Storage};
1616
use crate::compiler::preprocessor_cache::preprocessor_cache_entry_hash_key;
1717
use crate::compiler::{
1818
Cacheable, ColorMode, Compilation, CompileCommand, Compiler, CompilerArguments, CompilerHasher,
1919
CompilerKind, HashResult, Language,
2020
};
2121
#[cfg(feature = "dist-client")]
2222
use crate::compiler::{DistPackagers, NoopOutputsRewriter};
23+
use crate::config::PreprocessorCacheModeConfig;
2324
use crate::dist;
2425
#[cfg(feature = "dist-client")]
2526
use crate::dist::pkg;

src/compiler/compiler.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1882,7 +1882,8 @@ where
18821882
mod test {
18831883
use super::*;
18841884
use crate::cache::disk::DiskCache;
1885-
use crate::cache::{CacheMode, CacheRead, PreprocessorCacheModeConfig};
1885+
use crate::cache::{CacheMode, CacheRead};
1886+
use crate::config::PreprocessorCacheModeConfig;
18861887
use crate::mock_command::*;
18871888
use crate::test::mock_storage::MockStorage;
18881889
use crate::test::utils::*;

src/compiler/preprocessor_cache.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use chrono::Datelike;
3333
use serde::{Deserialize, Serialize};
3434

3535
use crate::{
36-
cache::PreprocessorCacheModeConfig,
36+
config::PreprocessorCacheModeConfig,
3737
util::{Digest, HashToDigest, MetadataCtimeExt, Timestamp, encode_path, strip_basedirs},
3838
};
3939

src/config.rs

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ use std::sync::{LazyLock, Mutex};
3535
use std::{collections::HashMap, fmt};
3636
use typed_path::Utf8TypedPathBuf;
3737

38-
pub use crate::cache::PreprocessorCacheModeConfig;
3938
use crate::errors::*;
4039

4140
static CACHED_CONFIG_PATH: LazyLock<PathBuf> = LazyLock::new(CachedConfig::file_config_path);
@@ -189,6 +188,57 @@ pub struct AzureCacheConfig {
189188
pub key_prefix: String,
190189
}
191190

191+
/// Configuration switches for preprocessor cache mode.
192+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
193+
#[serde(deny_unknown_fields)]
194+
#[serde(default)]
195+
pub struct PreprocessorCacheModeConfig {
196+
/// Whether to use preprocessor cache mode entirely
197+
pub use_preprocessor_cache_mode: bool,
198+
/// If false (default), only compare header files by hashing their contents.
199+
/// If true, will use size + ctime + mtime to check whether a file has changed.
200+
/// See other flags below for more control over this behavior.
201+
pub file_stat_matches: bool,
202+
/// If true (default), uses the ctime (file status change on UNIX,
203+
/// creation time on Windows) to check that a file has/hasn't changed.
204+
/// Can be useful to disable when backdating modification times
205+
/// in a controlled manner.
206+
pub use_ctime_for_stat: bool,
207+
/// If true, ignore `__DATE__`, `__TIME__` and `__TIMESTAMP__` being present
208+
/// in the source code. Will speed up preprocessor cache mode,
209+
/// but can result in false positives.
210+
pub ignore_time_macros: bool,
211+
/// If true, preprocessor cache mode will not cache system headers, only
212+
/// add them to the hash.
213+
pub skip_system_headers: bool,
214+
/// If true (default), will add the current working directory in the hash to
215+
/// distinguish two compilations from different directories.
216+
pub hash_working_directory: bool,
217+
}
218+
219+
impl Default for PreprocessorCacheModeConfig {
220+
fn default() -> Self {
221+
Self {
222+
use_preprocessor_cache_mode: false,
223+
file_stat_matches: false,
224+
use_ctime_for_stat: true,
225+
ignore_time_macros: false,
226+
skip_system_headers: false,
227+
hash_working_directory: true,
228+
}
229+
}
230+
}
231+
232+
impl PreprocessorCacheModeConfig {
233+
/// Return a default [`Self`], but with the cache active.
234+
pub fn activated() -> Self {
235+
Self {
236+
use_preprocessor_cache_mode: true,
237+
..Default::default()
238+
}
239+
}
240+
}
241+
192242
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize)]
193243
#[serde(deny_unknown_fields)]
194244
#[serde(default)]

src/test/mock_storage.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::cache::{Cache, CacheWrite, PreprocessorCacheModeConfig, Storage};
15+
use crate::cache::{Cache, CacheWrite, Storage};
16+
use crate::config::PreprocessorCacheModeConfig;
1617
use crate::errors::*;
1718
use async_trait::async_trait;
1819
use futures::channel::mpsc;

src/test/tests.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,11 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
use crate::cache::CacheMode;
1516
use crate::cache::disk::DiskCache;
16-
use crate::cache::{CacheMode, PreprocessorCacheModeConfig};
1717
use crate::client::connect_to_server;
1818
use crate::commands::{do_compile, request_shutdown, request_stats};
19+
use crate::config::PreprocessorCacheModeConfig;
1920
use crate::jobserver::Client;
2021
use crate::mock_command::*;
2122
use crate::server::{DistClientContainer, SccacheServer, ServerMessage};

0 commit comments

Comments
 (0)