Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ categories = ["network-programming", "web-programming"]
exclude.workspace = true

[workspace.package]
version = "23.3.2"
version = "23.4.0"
readme = "README.md"
edition = "2024"
authors = ["root@devhub.vip"]
Expand Down Expand Up @@ -86,13 +86,13 @@ members = [
]

[workspace.dependencies]
hyperlane = { path = ".", version = "23.3.2" }
http-type = { path = "type", version = "23.3.2" }
hyperlane-core = { path = "core", version = "23.3.2" }
http-request = { path = "request", version = "23.3.2" }
http-compress = { path = "compress", version = "23.3.2" }
http-constant = { path = "constant", version = "23.3.2" }
hyperlane-macros = { path = "macros", version = "23.3.2" }
hyperlane = { path = ".", version = "23.4.0" }
http-type = { path = "type", version = "23.4.0" }
hyperlane-core = { path = "core", version = "23.4.0" }
http-request = { path = "request", version = "23.4.0" }
http-compress = { path = "compress", version = "23.4.0" }
http-constant = { path = "constant", version = "23.4.0" }
hyperlane-macros = { path = "macros", version = "23.4.0" }

h3 = "0.0.8"
h2 = "0.4.20"
Expand Down
244 changes: 142 additions & 102 deletions compress/src/brotli/fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,35 +106,46 @@ pub fn encode_into(data: &[u8], out: &mut Vec<u8>) -> bool {
let mut available_in: usize = data.len();
let mut input_offset: usize = 0;
let mut total_out: Option<usize> = Some(0);
let mut scratch: [u8; BROTLI_ENCODE_SCRATCH_LEN] = [0u8; BROTLI_ENCODE_SCRATCH_LEN];
loop {
let mut available_out: usize = scratch.len();
let mut output_offset: usize = 0;
let ok: bool = state.compress_stream(
BrotliEncoderOperation::BROTLI_OPERATION_FINISH,
&mut available_in,
data,
&mut input_offset,
&mut available_out,
&mut scratch,
&mut output_offset,
&mut total_out,
&mut |_: &mut PredictionModeContextMap<InputReferenceMut<'_>>,
_: &mut [StaticCommand],
_: InputPair<'_>,
_: &mut BrotliTlAlloc| {},
);
if !ok {
out.truncate(start_len);
return false;
}
if output_offset > 0 {
out.extend_from_slice(&scratch[..output_offset]);
}
if state.is_finished() {
return true;
BROTLI_ENCODE_SCRATCH.with(|cell: &RefCell<[u8; BROTLI_ENCODE_SCRATCH_LEN]>| {
let mut fallback: [u8; BROTLI_ENCODE_SCRATCH_LEN];
let mut borrowed: Option<RefMut<'_, [u8; BROTLI_ENCODE_SCRATCH_LEN]>> =
cell.try_borrow_mut().ok();
let scratch: &mut [u8] = match borrowed.as_deref_mut() {
Some(buf) => buf.as_mut_slice(),
None => {
fallback = [0u8; BROTLI_ENCODE_SCRATCH_LEN];
fallback.as_mut_slice()
}
};
loop {
let mut available_out: usize = scratch.len();
let mut output_offset: usize = 0;
let ok: bool = state.compress_stream(
BrotliEncoderOperation::BROTLI_OPERATION_FINISH,
&mut available_in,
data,
&mut input_offset,
&mut available_out,
scratch,
&mut output_offset,
&mut total_out,
&mut |_: &mut PredictionModeContextMap<InputReferenceMut<'_>>,
_: &mut [StaticCommand],
_: InputPair<'_>,
_: &mut BrotliTlAlloc| {},
);
if !ok {
out.truncate(start_len);
return false;
}
if output_offset > 0 {
out.extend_from_slice(&scratch[..output_offset]);
}
if state.is_finished() {
return true;
}
}
}
})
}

/// Compresses the given data using the Brotli compression algorithm.
Expand Down Expand Up @@ -171,44 +182,59 @@ pub fn encode(data: &'_ [u8], buffer_size: usize) -> Cow<'_, [u8]> {
let mut available_in: usize = data.len();
let mut input_offset: usize = 0;
let mut total_out: Option<usize> = Some(0);
let mut scratch: [u8; BROTLI_ENCODE_SCRATCH_LEN] = [0u8; BROTLI_ENCODE_SCRATCH_LEN];
let mut out: Vec<u8> = Vec::new();
loop {
let mut available_out: usize = scratch.len();
let mut output_offset: usize = 0;
let ok: bool = state.compress_stream(
BrotliEncoderOperation::BROTLI_OPERATION_FINISH,
&mut available_in,
data,
&mut input_offset,
&mut available_out,
&mut scratch,
&mut output_offset,
&mut total_out,
&mut |_: &mut PredictionModeContextMap<InputReferenceMut<'_>>,
_: &mut [StaticCommand],
_: InputPair<'_>,
_: &mut BrotliTlAlloc| {},
);
if !ok {
return Cow::Owned(Vec::new());
}
if state.is_finished() && out.is_empty() {
return Cow::Owned(scratch[..output_offset].to_vec());
}
if output_offset > 0 {
if out.is_empty() {
let initial_capacity: usize = data
.len()
.min(buffer_size.max(output_offset))
.max(output_offset);
out.reserve(initial_capacity);
let ok: bool = BROTLI_ENCODE_SCRATCH.with(|cell: &RefCell<[u8; BROTLI_ENCODE_SCRATCH_LEN]>| {
let mut fallback: [u8; BROTLI_ENCODE_SCRATCH_LEN];
let mut borrowed: Option<RefMut<'_, [u8; BROTLI_ENCODE_SCRATCH_LEN]>> =
cell.try_borrow_mut().ok();
let scratch: &mut [u8] = match borrowed.as_deref_mut() {
Some(buf) => buf.as_mut_slice(),
None => {
fallback = [0u8; BROTLI_ENCODE_SCRATCH_LEN];
fallback.as_mut_slice()
}
};
loop {
let mut available_out: usize = scratch.len();
let mut output_offset: usize = 0;
let ok: bool = state.compress_stream(
BrotliEncoderOperation::BROTLI_OPERATION_FINISH,
&mut available_in,
data,
&mut input_offset,
&mut available_out,
scratch,
&mut output_offset,
&mut total_out,
&mut |_: &mut PredictionModeContextMap<InputReferenceMut<'_>>,
_: &mut [StaticCommand],
_: InputPair<'_>,
_: &mut BrotliTlAlloc| {},
);
if !ok {
return false;
}
if state.is_finished() && out.is_empty() {
out = scratch[..output_offset].to_vec();
return true;
}
if output_offset > 0 {
if out.is_empty() {
let initial_capacity: usize = data
.len()
.min(buffer_size.max(output_offset))
.max(output_offset);
out.reserve(initial_capacity);
}
out.extend_from_slice(&scratch[..output_offset]);
}
if state.is_finished() {
return true;
}
out.extend_from_slice(&scratch[..output_offset]);
}
if state.is_finished() {
break;
}
});
if !ok {
return Cow::Owned(Vec::new());
}
Cow::Owned(out)
}
Expand Down Expand Up @@ -236,51 +262,65 @@ pub fn decode(data: &'_ [u8], buffer_size: usize) -> Cow<'_, [u8]> {
let mut available_in: usize = data.len();
let mut input_offset: usize = 0;
let mut total_out: usize = 0;
let mut scratch: [u8; BROTLI_DECODE_SCRATCH_LEN] = [0u8; BROTLI_DECODE_SCRATCH_LEN];
let mut out: Vec<u8> = Vec::new();
loop {
let mut available_out: usize = scratch.len();
let mut output_offset: usize = 0;
let result: BrotliResult = BrotliDecompressStream(
&mut available_in,
&mut input_offset,
data,
&mut available_out,
&mut output_offset,
&mut scratch,
&mut total_out,
&mut state,
);
if out.len().saturating_add(output_offset) > MAX_DECOMPRESSED_BYTES {
return Cow::Owned(Vec::new());
}
match result {
BrotliResult::ResultSuccess => {
if out.is_empty() {
return Cow::Owned(scratch[..output_offset].to_vec());
}
if output_offset > 0 {
out.extend_from_slice(&scratch[..output_offset]);
}
return Cow::Owned(out);
let ok: bool = BROTLI_DECODE_SCRATCH.with(|cell: &RefCell<[u8; BROTLI_DECODE_SCRATCH_LEN]>| {
let mut fallback: [u8; BROTLI_DECODE_SCRATCH_LEN];
let mut borrowed: Option<RefMut<'_, [u8; BROTLI_DECODE_SCRATCH_LEN]>> =
cell.try_borrow_mut().ok();
let scratch: &mut [u8] = match borrowed.as_deref_mut() {
Some(buf) => buf.as_mut_slice(),
None => {
fallback = [0u8; BROTLI_DECODE_SCRATCH_LEN];
fallback.as_mut_slice()
}
};
loop {
let mut available_out: usize = scratch.len();
let mut output_offset: usize = 0;
let result: BrotliResult = BrotliDecompressStream(
&mut available_in,
&mut input_offset,
data,
&mut available_out,
&mut output_offset,
scratch,
&mut total_out,
&mut state,
);
if out.len().saturating_add(output_offset) > MAX_DECOMPRESSED_BYTES {
return false;
}
BrotliResult::NeedsMoreOutput => {
if output_offset > 0 {
match result {
BrotliResult::ResultSuccess => {
if out.is_empty() {
let initial_capacity: usize = data
.len()
.saturating_mul(4)
.max(output_offset.saturating_mul(2))
.min(buffer_size.max(output_offset.saturating_mul(2)))
.min(MAX_DECOMPRESSED_BYTES);
out.reserve(initial_capacity);
out = scratch[..output_offset].to_vec();
} else if output_offset > 0 {
out.extend_from_slice(&scratch[..output_offset]);
}
out.extend_from_slice(&scratch[..output_offset]);
return true;
}
BrotliResult::NeedsMoreOutput => {
if output_offset > 0 {
if out.is_empty() {
let initial_capacity: usize = data
.len()
.saturating_mul(4)
.max(output_offset.saturating_mul(2))
.min(buffer_size.max(output_offset.saturating_mul(2)))
.min(MAX_DECOMPRESSED_BYTES);
out.reserve(initial_capacity);
}
out.extend_from_slice(&scratch[..output_offset]);
}
}
BrotliResult::NeedsMoreInput | BrotliResult::ResultFailure => {
return false;
}
}
BrotliResult::NeedsMoreInput | BrotliResult::ResultFailure => {
return Cow::Owned(Vec::new());
}
}
});
if !ok {
return Cow::Owned(Vec::new());
}
Cow::Owned(out)
}
4 changes: 4 additions & 0 deletions compress/src/brotli/static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,8 @@ use super::*;
thread_local! {
/// Thread-local typed buffer pool reused across Brotli encode and decode operations.
pub(crate) static BROTLI_BUFFER_POOL: RefCell<BrotliBufferPool> = RefCell::new(BrotliBufferPool::default());
/// Thread-local reusable scratch buffer for Brotli streaming encode output.
pub(crate) static BROTLI_ENCODE_SCRATCH: RefCell<[u8; BROTLI_ENCODE_SCRATCH_LEN]> = const { RefCell::new([0u8; BROTLI_ENCODE_SCRATCH_LEN]) };
/// Thread-local reusable scratch buffer for Brotli streaming decode output.
pub(crate) static BROTLI_DECODE_SCRATCH: RefCell<[u8; BROTLI_DECODE_SCRATCH_LEN]> = const { RefCell::new([0u8; BROTLI_DECODE_SCRATCH_LEN]) };
}
2 changes: 1 addition & 1 deletion compress/src/compress/const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub(crate) const CONTENT_ENCODING_DEFLATE: &str = "deflate";
pub(crate) const CONTENT_ENCODING_BROTLI: &str = "br";

/// The default brotli compression quality.
pub(crate) const BROTLI_DEFAULT_QUALITY: u32 = 5;
pub(crate) const BROTLI_DEFAULT_QUALITY: u32 = 4;

/// The default brotli sliding window size, in bits.
pub(crate) const BROTLI_DEFAULT_WINDOW_BITS: u32 = 22;
Expand Down
3 changes: 3 additions & 0 deletions compress/src/deflate/const.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
/// Minimum output buffer reserve increment during raw deflate compression and decompression.
pub(crate) const DEFLATE_MIN_RESERVE: usize = 4096;

/// Default raw deflate compression level tuned for high-throughput HTTP responses.
pub(crate) const DEFLATE_DEFAULT_LEVEL: u32 = 4;

/// Maximum per-step output buffer growth increment while decompressing raw deflate streams.
pub(crate) const DEFLATE_MAX_GROW_STEP: usize = 1024 * 1024;
2 changes: 1 addition & 1 deletion compress/src/deflate/fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ pub(crate) fn compress_raw_into(data: &[u8], out: &mut Vec<u8>) -> bool {
compress_raw_with_state(&mut compressor, data, out)
} else {
let mut compressor: flate2::Compress =
flate2::Compress::new(Compression::default(), false);
flate2::Compress::new(Compression::new(DEFLATE_DEFAULT_LEVEL), false);
compress_raw_with_state(&mut compressor, data, out)
}
})
Expand Down
2 changes: 1 addition & 1 deletion compress/src/deflate/static.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use super::*;

thread_local! {
/// Thread-local reusable raw DEFLATE compressor state.
pub(crate) static DEFLATE_COMPRESSOR: RefCell<flate2::Compress> = RefCell::new(flate2::Compress::new(Compression::default(), false));
pub(crate) static DEFLATE_COMPRESSOR: RefCell<flate2::Compress> = RefCell::new(flate2::Compress::new(Compression::new(DEFLATE_DEFAULT_LEVEL), false));
/// Thread-local reusable raw DEFLATE decompressor state.
pub(crate) static DEFLATE_DECOMPRESSOR: RefCell<Decompress> = RefCell::new(Decompress::new(false));
}
2 changes: 1 addition & 1 deletion compress/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) use compress::{

use std::{
borrow::Cow,
cell::RefCell,
cell::{RefCell, RefMut},
collections::HashMap,
fmt,
mem::{MaybeUninit, size_of},
Expand Down
30 changes: 30 additions & 0 deletions compress/src/xx_hash3_64/const.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,32 @@
/// XXH64 avalanche multiplier prime 2.
pub(crate) const XXH3_PRIME64_2: u64 = 0xC2B2_AE3D_27D4_EB4F;

/// XXH64 avalanche multiplier prime 3.
pub(crate) const XXH3_PRIME64_3: u64 = 0x1656_67B1_9E37_79F9;

/// XXH3 avalanche multiplier prime MX1.
pub(crate) const XXH3_PRIME_MX1: u64 = 0x1656_6791_9E37_79F9;

/// XXH3 short-key multiplier prime MX2.
pub(crate) const XXH3_PRIME_MX2: u64 = 0x9FB2_1C65_1E98_DF25;

/// Precomputed XXH3-64 hash of the empty slice with the default seed and secret.
pub(crate) const XXH3_EMPTY_HASH: u64 = 0x2D06_8005_38D3_94C2;

/// Pre-XORed default-secret word for 1..=3 byte XXH3-64 inputs.
pub(crate) const XXH3_SECRET_1_3: u64 = 0x0000_0000_8727_5A9B;

/// Pre-XORed default-secret word for 4..=8 byte XXH3-64 inputs.
pub(crate) const XXH3_SECRET_4_8: u64 = 0xC73A_B174_C5EC_D5A2;

/// Maximum input length handled directly by the inline short-key XXH3-64 path.
pub(crate) const XXH3_SHORT_LIMIT: usize = 16;

/// Pre-XORed default-secret high word for 9..=16 byte XXH3-64 inputs.
pub(crate) const XXH3_SECRET_9_16_HI: u64 = 0xAF56_BC3B_0996_523A;

/// Pre-XORed default-secret low word for 9..=16 byte XXH3-64 inputs.
pub(crate) const XXH3_SECRET_9_16_LO: u64 = 0x6782_737B_EA42_39B9;

/// The maximum input length handled by the buffered short-key path.
pub(crate) const HASH_PREFIX_CAPACITY: usize = 128;
Loading
Loading