Skip to content

Commit 5a9a7a3

Browse files
committed
try tree-traversal without thread_local!
1 parent b9eb407 commit 5a9a7a3

File tree

2 files changed

+17
-55
lines changed

2 files changed

+17
-55
lines changed

gix-pack/src/cache/delta/traverse/resolve.rs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,15 @@ where
5353
E: std::error::Error + Send + Sync + 'static,
5454
{
5555
let mut decompressed_bytes_by_pack_offset = BTreeMap::new();
56-
let decompress_from_resolver = |slice: EntryRange, out: &mut Vec<u8>| -> Result<(data::Entry, u64), Error> {
56+
let mut inflate = zlib::Inflate::default();
57+
let mut decompress_from_resolver = |slice: EntryRange, out: &mut Vec<u8>| -> Result<(data::Entry, u64), Error> {
5758
let bytes = resolve(slice.clone(), resolve_data).ok_or(Error::ResolveFailed {
5859
pack_offset: slice.start,
5960
})?;
6061
let entry = data::Entry::from_bytes(bytes, slice.start, hash_len);
6162
let compressed = &bytes[entry.header_size()..];
6263
let decompressed_len = entry.decompressed_size as usize;
63-
decompress_all_at_once_with(compressed, decompressed_len, out)?;
64+
decompress_all_at_once_with(&mut inflate, compressed, decompressed_len, out)?;
6465
Ok((entry, slice.end))
6566
};
6667

@@ -235,15 +236,16 @@ where
235236
move || -> Result<(), Error> {
236237
let mut fully_resolved_delta_bytes = Vec::new();
237238
let mut delta_bytes = Vec::new();
238-
let decompress_from_resolver =
239+
let mut inflate = zlib::Inflate::default();
240+
let mut decompress_from_resolver =
239241
|slice: EntryRange, out: &mut Vec<u8>| -> Result<(data::Entry, u64), Error> {
240242
let bytes = resolve(slice.clone(), resolve_data).ok_or(Error::ResolveFailed {
241243
pack_offset: slice.start,
242244
})?;
243245
let entry = data::Entry::from_bytes(bytes, slice.start, hash_len);
244246
let compressed = &bytes[entry.header_size()..];
245247
let decompressed_len = entry.decompressed_size as usize;
246-
decompress_all_at_once_with(compressed, decompressed_len, out)?;
248+
decompress_all_at_once_with(&mut inflate, compressed, decompressed_len, out)?;
247249
Ok((entry, slice.end))
248250
};
249251

@@ -407,31 +409,17 @@ fn set_len(v: &mut Vec<u8>, new_len: usize) {
407409
}
408410
}
409411

410-
fn decompress_all_at_once_with(b: &[u8], decompressed_len: usize, out: &mut Vec<u8>) -> Result<(), Error> {
412+
fn decompress_all_at_once_with(
413+
inflate: &mut zlib::Inflate,
414+
b: &[u8],
415+
decompressed_len: usize,
416+
out: &mut Vec<u8>,
417+
) -> Result<(), Error> {
411418
set_len(out, decompressed_len);
412-
// TODO: try to put this back after the next zlib-ng upgrade.
413-
// This is from 3a2d5286084597d4c68549903709cda77dda4357 and it worked until zlib-ng-sys 1.1.9. Then it started to
414-
// fail with `incorrect data check` 25% of the time.
415-
// Note that thread_local! usage was also removed in two other places in `decode/entry.rs` for good measure.
416-
// use std::cell::RefCell;
417-
// thread_local! {
418-
// pub static INFLATE: RefCell<zlib::Inflate> = RefCell::new(zlib::Inflate::default());
419-
// }
420-
//
421-
// INFLATE.with(|inflate| {
422-
// let mut inflate = inflate.borrow_mut();
423-
// let res = inflate.once(b, out).map_err(|err| Error::ZlibInflate {
424-
// source: err,
425-
// message: "Failed to decompress entry",
426-
// });
427-
// inflate.reset();
428-
// res
429-
// })?;
430-
zlib::Inflate::default()
431-
.once(b, out)
432-
.map_err(|err| Error::ZlibInflate {
433-
source: err,
434-
message: "Failed to decompress entry",
435-
})?;
419+
inflate.reset();
420+
inflate.once(b, out).map_err(|err| Error::ZlibInflate {
421+
source: err,
422+
message: "Failed to decompress entry",
423+
})?;
436424
Ok(())
437425
}

gix-pack/src/data/file/decode/entry.rs

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,6 @@ impl File {
126126
let offset: usize = data_offset.try_into().expect("offset representable by machine");
127127
assert!(offset < self.data.len(), "entry offset out of bounds");
128128

129-
// use std::cell::RefCell;
130-
// thread_local! {
131-
// pub static INFLATE: RefCell<zlib::Inflate> = RefCell::new(zlib::Inflate::default());
132-
// }
133-
// INFLATE.with(|inflate| {
134-
// let mut inflate = inflate.borrow_mut();
135-
// let res = inflate
136-
// .once(&self.data[offset..], out)
137-
// .map(|(_status, consumed_in, _consumed_out)| consumed_in);
138-
// inflate.reset();
139-
// res
140-
// })
141129
zlib::Inflate::default()
142130
.once(&self.data[offset..], out)
143131
.map(|(_status, consumed_in, _consumed_out)| consumed_in)
@@ -152,20 +140,6 @@ impl File {
152140
let offset: usize = data_offset.try_into().expect("offset representable by machine");
153141
assert!(offset < self.data.len(), "entry offset out of bounds");
154142

155-
// TODO: put this back in once we know that zlib-ng doesn't fail once in a million times (see tree-traversal)
156-
// use std::cell::RefCell;
157-
// thread_local! {
158-
// pub static INFLATE: RefCell<zlib::Inflate> = RefCell::new(zlib::Inflate::default());
159-
// }
160-
//
161-
// INFLATE.with(|inflate| {
162-
// let mut inflate = inflate.borrow_mut();
163-
// let res = inflate
164-
// .once(&self.data[offset..], out)
165-
// .map(|(_status, consumed_in, consumed_out)| (consumed_in, consumed_out));
166-
// inflate.reset();
167-
// res
168-
// })
169143
zlib::Inflate::default()
170144
.once(&self.data[offset..], out)
171145
.map(|(_status, consumed_in, consumed_out)| (consumed_in, consumed_out))

0 commit comments

Comments
 (0)