Skip to content

Commit 3523aa4

Browse files
committed
Make obvious that we steal data from the free list (#293)
Thanks @joshtriplett for the review and suggestions!
1 parent dae710f commit 3523aa4

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

git-repository/src/easy/handle.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ impl easy::Handle {
7474
self.bufs.borrow_mut().pop().unwrap_or_default()
7575
}
7676

77+
/// This method is commonly called from the destructor of objects that previously claimed an entry
78+
/// in the free-list with `free_buf()`.
79+
/// They are welcome to take out the data themselves, for instance when the object is detached, to avoid
80+
/// it to be reclaimed.
7781
#[inline]
7882
pub(crate) fn reuse_buffer(&self, data: &mut Vec<u8>) {
7983
if data.capacity() > 0 {

git-repository/src/easy/object/impls.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,13 @@ impl AsRef<[u8]> for DetachedObject {
2929
impl<'repo> TryFrom<Object<'repo>> for Commit<'repo> {
3030
type Error = Object<'repo>;
3131

32-
fn try_from(value: Object<'repo>) -> Result<Self, Self::Error> {
32+
fn try_from(mut value: Object<'repo>) -> Result<Self, Self::Error> {
3333
let handle = value.handle;
3434
match value.kind {
3535
object::Kind::Commit => Ok(Commit {
3636
id: value.id,
3737
handle,
38-
data: {
39-
drop(value);
40-
handle.free_buf()
41-
},
38+
data: steal(&mut value.data),
4239
}),
4340
_ => Err(value),
4441
}
@@ -48,18 +45,21 @@ impl<'repo> TryFrom<Object<'repo>> for Commit<'repo> {
4845
impl<'repo> TryFrom<Object<'repo>> for Tree<'repo> {
4946
type Error = Object<'repo>;
5047

51-
fn try_from(value: Object<'repo>) -> Result<Self, Self::Error> {
48+
fn try_from(mut value: Object<'repo>) -> Result<Self, Self::Error> {
5249
let handle = value.handle;
5350
match value.kind {
5451
object::Kind::Tree => Ok(Tree {
5552
id: value.id,
5653
handle,
57-
data: {
58-
drop(value);
59-
handle.free_buf()
60-
},
54+
data: steal(&mut value.data),
6155
}),
6256
_ => Err(value),
6357
}
6458
}
6559
}
60+
61+
/// In conjunction with the handles free list, leaving an empty Vec in place of the original causes it to not be
62+
/// returned to the free list.
63+
fn steal(data: &mut Vec<u8>) -> Vec<u8> {
64+
std::mem::take(data)
65+
}

0 commit comments

Comments
 (0)