-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce allocations & accept bytes and bytearray inputs (#22)
- Allow taking (and returning) bytes and bytearray as input - Reduce allocations by either estimating output size or taking an optional `output_len` for all variants
- Loading branch information
1 parent
0d3dc4b
commit a7c41df
Showing
15 changed files
with
1,106 additions
and
478 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,138 @@ | ||
use brotli2::read::{BrotliDecoder, BrotliEncoder}; | ||
use std::error::Error; | ||
use std::io::prelude::*; | ||
use crate::exceptions::{CompressionError, DecompressionError}; | ||
use crate::{to_py_err, BytesType, Output}; | ||
use pyo3::prelude::*; | ||
use pyo3::types::{PyByteArray, PyBytes}; | ||
use pyo3::wrap_pyfunction; | ||
use pyo3::{PyResult, Python}; | ||
|
||
/// Decompress via Brotli | ||
pub fn decompress(data: &[u8]) -> Result<Vec<u8>, Box<dyn Error>> { | ||
let mut decoder = BrotliDecoder::new(data); | ||
let mut buf = vec![]; | ||
decoder.read_to_end(&mut buf)?; | ||
Ok(buf) | ||
pub fn init_py_module(m: &PyModule) -> PyResult<()> { | ||
m.add_function(wrap_pyfunction!(compress, m)?)?; | ||
m.add_function(wrap_pyfunction!(decompress, m)?)?; | ||
Ok(()) | ||
} | ||
|
||
/// Compress via Brotli | ||
pub fn compress(data: &[u8], level: u32) -> Result<Vec<u8>, Box<dyn Error>> { | ||
let mut encoder = BrotliEncoder::new(data, level); | ||
let mut buf = vec![]; | ||
encoder.read_to_end(&mut buf)?; | ||
Ok(buf) | ||
/// Brotli decompression. | ||
/// | ||
/// Python Example | ||
/// -------------- | ||
/// ```python | ||
/// >>> cramjam.brotli.decompress(compressed_bytes, output_len=Optional[int]) | ||
/// ``` | ||
#[pyfunction] | ||
pub fn decompress<'a>(py: Python<'a>, data: BytesType<'a>, output_len: Option<usize>) -> PyResult<BytesType<'a>> { | ||
match data { | ||
BytesType::Bytes(input) => match output_len { | ||
Some(len) => { | ||
let pybytes = PyBytes::new_with(py, len, |buffer| { | ||
let output = Output::Slice(buffer); | ||
to_py_err!(DecompressionError -> self::internal::decompress(input.as_bytes(), output))?; | ||
Ok(()) | ||
})?; | ||
Ok(BytesType::Bytes(pybytes)) | ||
} | ||
None => { | ||
let mut buffer = Vec::with_capacity(data.len() / 10); | ||
let output = Output::Vector(&mut buffer); | ||
to_py_err!(DecompressionError -> self::internal::decompress(input.as_bytes(), output))?; | ||
Ok(BytesType::Bytes(PyBytes::new(py, &buffer))) | ||
} | ||
}, | ||
BytesType::ByteArray(input) => match output_len { | ||
Some(len) => { | ||
let mut size = 0; | ||
let pybytes = PyByteArray::new_with(py, len, |buffer| { | ||
let output = Output::Slice(buffer); | ||
size = to_py_err!(DecompressionError -> self::internal::decompress(unsafe { input.as_bytes() }, output))?; | ||
Ok(()) | ||
})?; | ||
pybytes.resize(size)?; | ||
Ok(BytesType::ByteArray(pybytes)) | ||
} | ||
None => { | ||
let mut buffer = Vec::with_capacity(data.len() / 10); | ||
let output = Output::Vector(&mut buffer); | ||
to_py_err!(DecompressionError -> self::internal::decompress(unsafe { input.as_bytes() }, output))?; | ||
Ok(BytesType::ByteArray(PyByteArray::new(py, &buffer))) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
/// Brotli compression. | ||
/// | ||
/// Python Example | ||
/// -------------- | ||
/// ```python | ||
/// >>> cramjam.brotli.compress(b'some bytes here', level=9, output_len=Option[int]) # level defaults to 11 | ||
/// ``` | ||
#[pyfunction] | ||
pub fn compress<'a>( | ||
py: Python<'a>, | ||
data: BytesType<'a>, | ||
level: Option<u32>, | ||
output_len: Option<usize>, | ||
) -> PyResult<BytesType<'a>> { | ||
let level = level.unwrap_or_else(|| 11); | ||
match data { | ||
BytesType::Bytes(input) => match output_len { | ||
Some(len) => { | ||
let pybytes = PyBytes::new_with(py, len, |buffer| { | ||
let output = Output::Slice(buffer); | ||
to_py_err!(CompressionError -> self::internal::compress(input.as_bytes(), output, level))?; | ||
Ok(()) | ||
})?; | ||
Ok(BytesType::Bytes(pybytes)) | ||
} | ||
None => { | ||
let mut buffer = Vec::with_capacity(data.len() / 10); | ||
let output = Output::Vector(&mut buffer); | ||
to_py_err!(CompressionError -> self::internal::compress(input.as_bytes(), output, level))?; | ||
Ok(BytesType::Bytes(PyBytes::new(py, &buffer))) | ||
} | ||
}, | ||
BytesType::ByteArray(input) => match output_len { | ||
Some(len) => { | ||
let mut size = 0; | ||
let pybytes = PyByteArray::new_with(py, len, |buffer| { | ||
let output = Output::Slice(buffer); | ||
size = to_py_err!(CompressionError -> self::internal::compress(unsafe { input.as_bytes() }, output, level))?; | ||
Ok(()) | ||
})?; | ||
pybytes.resize(size)?; | ||
Ok(BytesType::ByteArray(pybytes)) | ||
} | ||
None => { | ||
let mut buffer = Vec::with_capacity(data.len() / 10); | ||
let output = Output::Vector(&mut buffer); | ||
to_py_err!(CompressionError -> self::internal::compress(unsafe { input.as_bytes() }, output, level))?; | ||
Ok(BytesType::ByteArray(PyByteArray::new(py, &buffer))) | ||
} | ||
}, | ||
} | ||
} | ||
|
||
mod internal { | ||
|
||
use crate::Output; | ||
use brotli2::read::{BrotliDecoder, BrotliEncoder}; | ||
use std::io::prelude::*; | ||
use std::io::Error; | ||
|
||
/// Decompress via Brotli | ||
pub fn decompress<'a>(data: &[u8], output: Output<'a>) -> Result<usize, Error> { | ||
let mut decoder = BrotliDecoder::new(data); | ||
match output { | ||
Output::Slice(slice) => decoder.read(slice), | ||
Output::Vector(v) => decoder.read_to_end(v), | ||
} | ||
} | ||
|
||
/// Compress via Brotli | ||
pub fn compress<'a>(data: &'a [u8], output: Output<'a>, level: u32) -> Result<usize, Error> { | ||
let mut encoder = BrotliEncoder::new(data, level); | ||
match output { | ||
Output::Slice(slice) => encoder.read(slice), | ||
Output::Vector(v) => encoder.read_to_end(v), | ||
} | ||
} | ||
} |
Oops, something went wrong.