Skip to content

Commit bc77534

Browse files
committed
feat: Use GITOXIDE_OBJECT_CACHE_MEMORY to control how much object cache is used (#215)
Note that this is mostly for debugging or quickly seeing if object caches help with certain operations. Ideally the implementation knows themselves and sets up caches accordingly, probably after trying it with these environment variables.
1 parent 91d7ef2 commit bc77534

File tree

1 file changed

+40
-38
lines changed

1 file changed

+40
-38
lines changed

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

Lines changed: 40 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -30,52 +30,54 @@ 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+
///
34+
/// Use the `GITOXIDE_OBJECT_CACHE_MEMORY=16mb` to set the given amount of memory to store full objects, on a per-thread basis.
3335
pub fn apply_environment(self) -> Self {
3436
// We have no cache types available without this flag currently. Maybe this should change at some point.
3537
#[cfg(not(feature = "max-performance"))]
3638
return self;
3739
#[cfg(feature = "max-performance")]
3840
{
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")
44-
.ok()
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!(
58-
"Parsed bytes value is not representable as usize. Defaulting to standard pack cache: {}",
59-
err
60-
)
61-
})
62-
.ok()
63-
})
64-
})
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-
}
74-
}
75-
};
41+
let pack_cache_disabled = std::env::var_os("GITOXIDE_DISABLE_PACK_CACHE").is_some();
7642
let mut this = self;
77-
this.objects.set_pack_cache(new_pack_cache);
43+
if !pack_cache_disabled {
44+
let bytes = parse_bytes_from_var("GITOXIDE_PACK_CACHE_MEMORY");
45+
let new_pack_cache = move || -> Box<git_odb::handle::PackCache> {
46+
match bytes {
47+
Some(bytes) => Box::new(git_pack::cache::lru::MemoryCappedHashmap::new(bytes)),
48+
None => Box::new(git_pack::cache::lru::StaticLinkedList::<64>::default()),
49+
}
50+
};
51+
this.objects.set_pack_cache(new_pack_cache);
52+
}
53+
if let Some(bytes) = parse_bytes_from_var("GITOXIDE_OBJECT_CACHE_MEMORY") {
54+
this.objects
55+
.set_object_cache(move || Box::new(git_pack::cache::object::MemoryCappedHashmap::new(bytes)));
56+
}
7857
this
7958
}
8059
}
8160
}
61+
62+
#[cfg(feature = "max-performance")]
63+
fn parse_bytes_from_var(name: &str) -> Option<usize> {
64+
std::env::var(name)
65+
.ok()
66+
.and_then(|v| {
67+
byte_unit::Byte::from_str(&v)
68+
.map_err(|err| log::warn!("Failed to parse {:?} into byte unit for pack cache: {}", v, err))
69+
.ok()
70+
})
71+
.and_then(|unit| {
72+
use std::convert::TryInto;
73+
unit.get_bytes()
74+
.try_into()
75+
.map_err(|err| {
76+
log::warn!(
77+
"Parsed bytes value is not representable as usize. Defaulting to standard pack cache: {}",
78+
err
79+
)
80+
})
81+
.ok()
82+
})
83+
}

0 commit comments

Comments
 (0)