Skip to content

Commit 91d7ef2

Browse files
committed
Don't read environment variables each time an pack cache is created (#215)
Instead, read them eagerly and pass the result on to the closure instead.
1 parent 1d5ab44 commit 91d7ef2

File tree

1 file changed

+40
-30
lines changed

1 file changed

+40
-30
lines changed

git-repository/src/easy/ext/cache.rs

Lines changed: 40 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -30,42 +30,52 @@ impl easy::Handle {
3030
/// the cache efficiency is low. Use `GITOXIDE_PACK_CACHE_MEMORY=512MB` to use up to 512MB of RAM for the pack delta base
3131
/// cache. If none of these are set, the default cache is fast enough to nearly never cause a (marginal) slow-down while providing
3232
/// some gains most of the time. Note that the value given is _per-thread_.
33-
pub fn apply_environment(mut self) -> Self {
34-
self.objects.set_pack_cache(|| {
35-
#[cfg(not(feature = "max-performance"))]
36-
{
37-
Box::new(git_pack::cache::Never)
38-
}
39-
#[cfg(feature = "max-performance")]
40-
{
41-
use std::convert::TryInto;
42-
if std::env::var_os("GITOXIDE_DISABLE_PACK_CACHE").is_some() {
43-
Box::new(git_pack::cache::Never)
44-
} else if let Some(bytes) = std::env::var("GITOXIDE_PACK_CACHE_MEMORY")
45-
.ok()
46-
.and_then(|v| {
47-
byte_unit::Byte::from_str(&v)
48-
.map_err(|err| log::warn!("Failed to parse {:?} into byte unit for pack cache: {}", v, err))
33+
pub fn apply_environment(self) -> Self {
34+
// We have no cache types available without this flag currently. Maybe this should change at some point.
35+
#[cfg(not(feature = "max-performance"))]
36+
return self;
37+
#[cfg(feature = "max-performance")]
38+
{
39+
let new_pack_cache = {
40+
let pack_cache_disabled = std::env::var_os("GITOXIDE_DISABLE_PACK_CACHE").is_some();
41+
let bytes = (!pack_cache_disabled)
42+
.then(|| {
43+
std::env::var("GITOXIDE_PACK_CACHE_MEMORY")
4944
.ok()
50-
})
51-
.and_then(|unit| {
52-
unit.get_bytes()
53-
.try_into()
54-
.map_err(|err| {
55-
log::warn!(
45+
.and_then(|v| {
46+
byte_unit::Byte::from_str(&v)
47+
.map_err(|err| {
48+
log::warn!("Failed to parse {:?} into byte unit for pack cache: {}", v, err)
49+
})
50+
.ok()
51+
})
52+
.and_then(|unit| {
53+
use std::convert::TryInto;
54+
unit.get_bytes()
55+
.try_into()
56+
.map_err(|err| {
57+
log::warn!(
5658
"Parsed bytes value is not representable as usize. Defaulting to standard pack cache: {}",
5759
err
5860
)
61+
})
62+
.ok()
5963
})
60-
.ok()
6164
})
62-
{
63-
Box::new(git_pack::cache::lru::MemoryCappedHashmap::new(bytes))
64-
} else {
65-
Box::new(git_pack::cache::lru::StaticLinkedList::<64>::default())
65+
.flatten();
66+
move || -> Box<git_odb::handle::PackCache> {
67+
if pack_cache_disabled {
68+
Box::new(git_pack::cache::Never)
69+
} else if let Some(bytes) = bytes {
70+
Box::new(git_pack::cache::lru::MemoryCappedHashmap::new(bytes))
71+
} else {
72+
Box::new(git_pack::cache::lru::StaticLinkedList::<64>::default())
73+
}
6674
}
67-
}
68-
});
69-
self
75+
};
76+
let mut this = self;
77+
this.objects.set_pack_cache(new_pack_cache);
78+
this
79+
}
7080
}
7181
}

0 commit comments

Comments
 (0)