|
| 1 | +#![cfg(feature = "with-rust-allocator")] |
| 2 | + |
| 3 | +mod support { |
| 4 | + pub mod rust_allocator; |
| 5 | +} |
| 6 | + |
| 7 | +use std::io::{Read, Write}; |
| 8 | +use std::sync::atomic::Ordering::Relaxed; |
| 9 | +use support::rust_allocator::{LIVE_BYTES, TOTAL_BYTES}; |
| 10 | + |
| 11 | +fn input() -> Vec<u8> { |
| 12 | + b"the quick brown fox jumps over the lazy dog; ".repeat(10_000) |
| 13 | +} |
| 14 | + |
| 15 | +const WORKSPACE_FLOOR: usize = 512 * 1024; |
| 16 | + |
| 17 | +#[test] |
| 18 | +fn dictionaries_use_global_allocator() { |
| 19 | + let input = input(); |
| 20 | + let dict: Vec<u8> = (0..=255u8).cycle().take(4096).collect(); |
| 21 | + |
| 22 | + let before = LIVE_BYTES.load(Relaxed); |
| 23 | + let compressed = { |
| 24 | + let mut encoder = zstd::Encoder::with_dictionary(Vec::new(), 3, &dict) |
| 25 | + .expect("encoder"); |
| 26 | + encoder.write_all(&input).expect("compression failed"); |
| 27 | + assert!( |
| 28 | + LIVE_BYTES.load(Relaxed) - before > WORKSPACE_FLOOR, |
| 29 | + "Encoder::with_dictionary workspace is not visible to the \ |
| 30 | + global allocator" |
| 31 | + ); |
| 32 | + encoder.finish().expect("failed to finish frame") |
| 33 | + }; |
| 34 | + assert_eq!( |
| 35 | + LIVE_BYTES.load(Relaxed) - before, |
| 36 | + compressed.capacity(), |
| 37 | + "Encoder::with_dictionary leaked" |
| 38 | + ); |
| 39 | + |
| 40 | + let before = LIVE_BYTES.load(Relaxed); |
| 41 | + { |
| 42 | + let prepared = zstd::dict::EncoderDictionary::copy(&dict, 3); |
| 43 | + assert!( |
| 44 | + LIVE_BYTES.load(Relaxed) - before >= prepared.as_cdict().sizeof(), |
| 45 | + "EncoderDictionary::copy is not visible to the global allocator" |
| 46 | + ); |
| 47 | + let prepared = zstd::dict::DecoderDictionary::copy(&dict); |
| 48 | + assert!( |
| 49 | + LIVE_BYTES.load(Relaxed) - before >= prepared.as_ddict().sizeof(), |
| 50 | + "DecoderDictionary::copy is not visible to the global allocator" |
| 51 | + ); |
| 52 | + } |
| 53 | + assert_eq!( |
| 54 | + LIVE_BYTES.load(Relaxed), |
| 55 | + before, |
| 56 | + "prepared dictionaries leaked" |
| 57 | + ); |
| 58 | + |
| 59 | + let before = LIVE_BYTES.load(Relaxed); |
| 60 | + let mut roundtrip = Vec::with_capacity(input.len()); |
| 61 | + { |
| 62 | + let prepared = zstd::dict::DecoderDictionary::copy(&dict); |
| 63 | + let mut decoder = zstd::Decoder::with_prepared_dictionary( |
| 64 | + compressed.as_slice(), |
| 65 | + &prepared, |
| 66 | + ) |
| 67 | + .expect("decoder"); |
| 68 | + decoder |
| 69 | + .read_to_end(&mut roundtrip) |
| 70 | + .expect("decompression failed"); |
| 71 | + } |
| 72 | + assert_eq!( |
| 73 | + LIVE_BYTES.load(Relaxed) - before, |
| 74 | + roundtrip.capacity(), |
| 75 | + "Decoder::with_prepared_dictionary leaked" |
| 76 | + ); |
| 77 | + assert_eq!(input, roundtrip); |
| 78 | + |
| 79 | + // One-shot helpers. |
| 80 | + let total = TOTAL_BYTES.load(Relaxed); |
| 81 | + let live = LIVE_BYTES.load(Relaxed); |
| 82 | + let compressed = |
| 83 | + zstd::encode_all(input.as_slice(), 3).expect("encode_all"); |
| 84 | + let decoded = zstd::decode_all(compressed.as_slice()).expect("decode_all"); |
| 85 | + assert!(TOTAL_BYTES.load(Relaxed) - total > WORKSPACE_FLOOR); |
| 86 | + drop((compressed, decoded)); |
| 87 | + assert_eq!( |
| 88 | + LIVE_BYTES.load(Relaxed), |
| 89 | + live, |
| 90 | + "encode_all/decode_all leaked" |
| 91 | + ); |
| 92 | +} |
0 commit comments