lz4rip 0.2.1 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +34 -0
- data/Cargo.lock +20 -20
- data/README.md +19 -1
- data/ext/lz4rip/Cargo.toml +2 -2
- data/ext/lz4rip/src/lib.rs +135 -32
- data/ext/lz4rip/src/rb.rs +20 -25
- data/lib/lz4rip/block_codec.rb +47 -17
- data/lib/lz4rip/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 0be78a0755eec4e7af9fb1bd60e9f300acb7ebb1f985cabb74b9ddc58baa3caa
|
|
4
|
+
data.tar.gz: 1d55969e05addddaf4972dfe5164dc0ea8f9f8bc4c0217b3de64e6da32a129f6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 340789f5f802bd244149a7346d998c9f175964a60788f7b872c00b313310e323e7589373b1d10afac140083c006fca90b9d7a877cb6561e3d4f718f2e154ff6c
|
|
7
|
+
data.tar.gz: 17cc18e51f08808a923ffd0093ff300e4a391d518c71e9004cb68d8af8552624dd341cd6ba212498a8b76ac27f82edd8b151af865640f6f451cdb2210888bf75
|
data/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,40 @@
|
|
|
2
2
|
|
|
3
3
|
## [Unreleased]
|
|
4
4
|
|
|
5
|
+
## [0.3.0] - 2026-11-26
|
|
6
|
+
|
|
7
|
+
- Update the `lz4rip` crate dependency to 0.12.9, for the ratio profile with
|
|
8
|
+
a dictionary and the compressors' heap sizes.
|
|
9
|
+
- `BlockCodec.new(profile: :ratio)`, with or without `dict:`, compresses to
|
|
10
|
+
smaller output with lz4rip's ratio profile. Any LZ4 decoder reads it. With
|
|
11
|
+
a dictionary, it hashes the dictionary once instead of for each message.
|
|
12
|
+
It releases the GVL from 128 KiB inputs instead of 256 KiB: it compresses
|
|
13
|
+
1.4-10x slower per byte.
|
|
14
|
+
- `BlockCodec#decompress` raises `DecompressError` for a `decompressed_size:`
|
|
15
|
+
the block cannot decode to (more than 255 times its length) before
|
|
16
|
+
allocating, and takes an optional `max_decompressed_size:`, as
|
|
17
|
+
`FrameCodec#decompress` does.
|
|
18
|
+
- `BlockCodec` makes its compressor on its first `#compress`, so a codec
|
|
19
|
+
that only decompresses holds only its dictionary. A dictionary codec held
|
|
20
|
+
16 KiB of compressor tables from the start.
|
|
21
|
+
- `BlockCodec#size` reports the bytes the codec holds: its compressor
|
|
22
|
+
tables, including those a ratio codec makes on first use, and its copies
|
|
23
|
+
of the dictionary. A dictionary codec reported one table and one copy.
|
|
24
|
+
|
|
25
|
+
## [0.2.2] - 2026-11-26
|
|
26
|
+
|
|
27
|
+
- Update the `lz4rip` crate dependency to 0.12.7. Block decompression caps
|
|
28
|
+
its output buffer at what the input can decode to, so a large
|
|
29
|
+
`decompressed_size:` no longer allocates that much up front. Dictionary
|
|
30
|
+
compression output differs from 0.2.1.
|
|
31
|
+
- `DictTrainer#train` no longer raises `RuntimeError` (native Rust panic)
|
|
32
|
+
for a `max_dict_size` of 4 to 31.
|
|
33
|
+
- Let several threads compress or decompress one mutable String at once.
|
|
34
|
+
Calls that release the GVL now borrow a frozen copy of the input instead
|
|
35
|
+
of locking it, so they no longer raise `RuntimeError` when another call
|
|
36
|
+
holds the same String, and the caller may modify it meanwhile.
|
|
37
|
+
- Check the LZ4 frame magic before releasing the GVL.
|
|
38
|
+
|
|
5
39
|
## [0.2.1] - 2026-10-11
|
|
6
40
|
|
|
7
41
|
- Update the `lz4rip` crate dependency to 0.11.10 and `rb-sys` to 0.9.130.
|
data/Cargo.lock
CHANGED
|
@@ -90,9 +90,9 @@ checksum = "20870f649af7073d53e38067b2a84312175d56ea15217e1b15bc83506ec50afb"
|
|
|
90
90
|
|
|
91
91
|
[[package]]
|
|
92
92
|
name = "libc"
|
|
93
|
-
version = "0.2.
|
|
93
|
+
version = "0.2.191"
|
|
94
94
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
95
|
-
checksum = "
|
|
95
|
+
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
|
|
96
96
|
|
|
97
97
|
[[package]]
|
|
98
98
|
name = "libloading"
|
|
@@ -106,17 +106,17 @@ dependencies = [
|
|
|
106
106
|
|
|
107
107
|
[[package]]
|
|
108
108
|
name = "lz4rip"
|
|
109
|
-
version = "0.
|
|
109
|
+
version = "0.3.0"
|
|
110
110
|
dependencies = [
|
|
111
|
-
"lz4rip 0.
|
|
111
|
+
"lz4rip 0.12.9",
|
|
112
112
|
"rb-sys",
|
|
113
113
|
]
|
|
114
114
|
|
|
115
115
|
[[package]]
|
|
116
116
|
name = "lz4rip"
|
|
117
|
-
version = "0.
|
|
117
|
+
version = "0.12.9"
|
|
118
118
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
119
|
-
checksum = "
|
|
119
|
+
checksum = "4dd27acb1a4d2b823738e9b3e63e1ddba5d24f52e97de40834c59d728e8bcfc9"
|
|
120
120
|
dependencies = [
|
|
121
121
|
"lz4rip-core",
|
|
122
122
|
"lz4rip-decode",
|
|
@@ -126,24 +126,24 @@ dependencies = [
|
|
|
126
126
|
|
|
127
127
|
[[package]]
|
|
128
128
|
name = "lz4rip-core"
|
|
129
|
-
version = "0.
|
|
129
|
+
version = "0.6.2"
|
|
130
130
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
131
|
-
checksum = "
|
|
131
|
+
checksum = "052afa89a977207a207f3e074b1518b0fb5767dab3274464fd1ee5fc7699a30b"
|
|
132
132
|
|
|
133
133
|
[[package]]
|
|
134
134
|
name = "lz4rip-decode"
|
|
135
|
-
version = "0.
|
|
135
|
+
version = "0.11.3"
|
|
136
136
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
137
|
-
checksum = "
|
|
137
|
+
checksum = "45f2489cd1216c98522f01f257b2e7dd09d1ba964f2498ce239387fd8fee6aca"
|
|
138
138
|
dependencies = [
|
|
139
139
|
"lz4rip-core",
|
|
140
140
|
]
|
|
141
141
|
|
|
142
142
|
[[package]]
|
|
143
143
|
name = "lz4rip-encode"
|
|
144
|
-
version = "0.10.
|
|
144
|
+
version = "0.10.18"
|
|
145
145
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
146
|
-
checksum = "
|
|
146
|
+
checksum = "ba7a8cdade339cca4b0f1c1d334edd1b36a01ebe6976940ede6eea7a3e28e8e6"
|
|
147
147
|
dependencies = [
|
|
148
148
|
"lz4rip-core",
|
|
149
149
|
]
|
|
@@ -214,9 +214,9 @@ dependencies = [
|
|
|
214
214
|
|
|
215
215
|
[[package]]
|
|
216
216
|
name = "regex"
|
|
217
|
-
version = "1.13.
|
|
217
|
+
version = "1.13.2"
|
|
218
218
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
219
|
-
checksum = "
|
|
219
|
+
checksum = "9807af0e638c12e753aa1b4e601791bf2a8f71274023c8bbeec12fc149950857"
|
|
220
220
|
dependencies = [
|
|
221
221
|
"aho-corasick",
|
|
222
222
|
"memchr",
|
|
@@ -226,9 +226,9 @@ dependencies = [
|
|
|
226
226
|
|
|
227
227
|
[[package]]
|
|
228
228
|
name = "regex-automata"
|
|
229
|
-
version = "0.4.
|
|
229
|
+
version = "0.4.19"
|
|
230
230
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
231
|
-
checksum = "
|
|
231
|
+
checksum = "41d60ef07df457506a0a1a2073112eb4aae5733dbd2813c3da77d1b0fd441158"
|
|
232
232
|
dependencies = [
|
|
233
233
|
"aho-corasick",
|
|
234
234
|
"memchr",
|
|
@@ -237,9 +237,9 @@ dependencies = [
|
|
|
237
237
|
|
|
238
238
|
[[package]]
|
|
239
239
|
name = "regex-syntax"
|
|
240
|
-
version = "0.8.
|
|
240
|
+
version = "0.8.12"
|
|
241
241
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
242
|
-
checksum = "
|
|
242
|
+
checksum = "d902046e6a28a1e4c3cab97258a0ce11e994903f01c2d97a9447a0b3df85f4f6"
|
|
243
243
|
|
|
244
244
|
[[package]]
|
|
245
245
|
name = "rustc-hash"
|
|
@@ -278,9 +278,9 @@ checksum = "86a801b3cea342a06d468c8710662aa29e5e05e4f5c0d62f00bbb7f2ad7941c2"
|
|
|
278
278
|
|
|
279
279
|
[[package]]
|
|
280
280
|
name = "unicode-ident"
|
|
281
|
-
version = "1.0.
|
|
281
|
+
version = "1.0.27"
|
|
282
282
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
283
|
-
checksum = "
|
|
283
|
+
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
|
|
284
284
|
|
|
285
285
|
[[package]]
|
|
286
286
|
name = "windows-link"
|
data/README.md
CHANGED
|
@@ -54,7 +54,25 @@ original = codec.decompress(compressed, decompressed_size: 12_000)
|
|
|
54
54
|
```
|
|
55
55
|
|
|
56
56
|
Block decompression requires the original size up front. This is by design: LZ4
|
|
57
|
-
block format does not store it, so the caller must track it.
|
|
57
|
+
block format does not store it, so the caller must track it. A size the block
|
|
58
|
+
cannot decode to (more than 255 times its length) raises `DecompressError`
|
|
59
|
+
before anything is allocated, and `max_decompressed_size:` sets a lower limit, as
|
|
60
|
+
for `FrameCodec`:
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
codec.decompress(compressed, decompressed_size: peer_size, max_decompressed_size: 1 << 20)
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
`profile: :ratio` compresses to smaller output, more slowly. Any LZ4 decoder
|
|
67
|
+
reads it, so only the sender needs it. It holds an 8 KB table, plus 64 KB from
|
|
68
|
+
its first input above 4 KiB; with a dictionary, 16 KB of tables while input and
|
|
69
|
+
dictionary stay within 4 KiB, 64 KB more for larger inputs. A codec makes its
|
|
70
|
+
compressor on its first `#compress`, so one that only decompresses holds only
|
|
71
|
+
its dictionary. `#size` reports the bytes a codec holds.
|
|
72
|
+
|
|
73
|
+
```ruby
|
|
74
|
+
codec = Lz4rip::BlockCodec.new(profile: :ratio, dict: dict)
|
|
75
|
+
```
|
|
58
76
|
|
|
59
77
|
### Dictionary compression
|
|
60
78
|
|
data/ext/lz4rip/Cargo.toml
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[package]
|
|
2
2
|
name = "lz4rip"
|
|
3
|
-
version = "0.
|
|
3
|
+
version = "0.3.0"
|
|
4
4
|
edition = "2021"
|
|
5
5
|
build = "build.rs"
|
|
6
6
|
|
|
@@ -9,7 +9,7 @@ name = "lz4rip"
|
|
|
9
9
|
crate-type = ["cdylib", "rlib"]
|
|
10
10
|
|
|
11
11
|
[dependencies]
|
|
12
|
-
lz4 = { version = "0.
|
|
12
|
+
lz4 = { version = "0.12.9", package = "lz4rip" }
|
|
13
13
|
rb-sys = "0.9.130"
|
|
14
14
|
|
|
15
15
|
[build-dependencies]
|
data/ext/lz4rip/src/lib.rs
CHANGED
|
@@ -3,9 +3,12 @@ mod rb;
|
|
|
3
3
|
use std::ffi::c_void;
|
|
4
4
|
use std::io::{Cursor, Read, Write};
|
|
5
5
|
use std::panic::{catch_unwind, AssertUnwindSafe};
|
|
6
|
+
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
6
7
|
use std::sync::{Mutex, OnceLock, TryLockError};
|
|
7
8
|
|
|
8
|
-
use lz4::block::{
|
|
9
|
+
use lz4::block::{
|
|
10
|
+
self, Decompressor, DictCompressor, DictTrainer, RatioCompressor, RatioDictCompressor,
|
|
11
|
+
};
|
|
9
12
|
use lz4::frame::{BlockMode, FrameDecoder, FrameDecoderOptions, FrameEncoder, FrameInfo};
|
|
10
13
|
use rb_sys::{rb_data_type_struct__bindgen_ty_1, rb_data_type_t, size_t, VALUE};
|
|
11
14
|
|
|
@@ -15,6 +18,9 @@ const COMPRESSOR_HEAP_SIZE: usize = 8192;
|
|
|
15
18
|
|
|
16
19
|
const LZ4_FRAME_MAGIC: [u8; 4] = [0x04, 0x22, 0x4d, 0x18];
|
|
17
20
|
const GVL_COMPRESS_THRESHOLD: usize = 256 * 1024;
|
|
21
|
+
/// The ratio profile compresses 128 KiB about as slowly as the fast one
|
|
22
|
+
/// compresses 256 KiB, or slower (Silesia text, binaries, x-ray).
|
|
23
|
+
const GVL_RATIO_COMPRESS_THRESHOLD: usize = 128 * 1024;
|
|
18
24
|
const GVL_FRAME_DECOMPRESS_THRESHOLD: usize = 256 * 1024;
|
|
19
25
|
|
|
20
26
|
static DECOMPRESS_ERROR: OnceLock<GlobalValue> = OnceLock::new();
|
|
@@ -51,7 +57,7 @@ where
|
|
|
51
57
|
F: FnOnce(&mut T, &[u8]) -> RbResult<R>,
|
|
52
58
|
{
|
|
53
59
|
if release_gvl {
|
|
54
|
-
input.
|
|
60
|
+
input.freeze_for_without_gvl(true)?;
|
|
55
61
|
return rb::maybe_without_gvl(true, || {
|
|
56
62
|
let mut guard = mutex
|
|
57
63
|
.lock()
|
|
@@ -64,7 +70,7 @@ where
|
|
|
64
70
|
Ok(mut guard) => func(&mut guard, input.as_slice()),
|
|
65
71
|
Err(TryLockError::WouldBlock) => {
|
|
66
72
|
// Even a small input needs protection when contention releases GVL.
|
|
67
|
-
input.
|
|
73
|
+
input.freeze_for_without_gvl(true)?;
|
|
68
74
|
rb::maybe_without_gvl(true, || {
|
|
69
75
|
let mut guard = mutex
|
|
70
76
|
.lock()
|
|
@@ -227,24 +233,101 @@ unsafe extern "C" fn lz4rip_block_stream_size(_module: VALUE) -> VALUE {
|
|
|
227
233
|
|
|
228
234
|
// ---------- BlockCodec ----------
|
|
229
235
|
|
|
236
|
+
/// A block codec's compressor state. The fast profile without a dictionary
|
|
237
|
+
/// keeps none: it compresses with `block::compress`.
|
|
238
|
+
enum BlockCompressor {
|
|
239
|
+
Dict(DictCompressor),
|
|
240
|
+
Ratio(RatioCompressor),
|
|
241
|
+
RatioDict(RatioDictCompressor),
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
impl BlockCompressor {
|
|
245
|
+
fn compress(&mut self, input: &[u8]) -> Vec<u8> {
|
|
246
|
+
match self {
|
|
247
|
+
Self::Dict(comp) => comp.compress(input),
|
|
248
|
+
Self::Ratio(comp) => comp.compress(input),
|
|
249
|
+
Self::RatioDict(comp) => comp.compress(input),
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
fn heap_size(&self) -> usize {
|
|
254
|
+
match self {
|
|
255
|
+
Self::Dict(comp) => comp.heap_size(),
|
|
256
|
+
Self::Ratio(comp) => comp.heap_size(),
|
|
257
|
+
Self::RatioDict(comp) => comp.heap_size(),
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/// A codec's compressor, made by its first `compress`: a codec that only
|
|
263
|
+
/// decompresses, as a receiving connection's does, holds no tables.
|
|
264
|
+
enum CompressorSlot {
|
|
265
|
+
/// The dictionary to make it with, if any.
|
|
266
|
+
Unmade(Option<Vec<u8>>),
|
|
267
|
+
Made(Box<BlockCompressor>),
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
impl CompressorSlot {
|
|
271
|
+
fn get(&mut self, ratio: bool) -> &mut BlockCompressor {
|
|
272
|
+
if let Self::Unmade(dict) = self {
|
|
273
|
+
let compressor = match (dict.take(), ratio) {
|
|
274
|
+
(None, _) => BlockCompressor::Ratio(RatioCompressor::new()),
|
|
275
|
+
(Some(bytes), false) => BlockCompressor::Dict(DictCompressor::new(&bytes)),
|
|
276
|
+
(Some(bytes), true) => BlockCompressor::RatioDict(RatioDictCompressor::new(&bytes)),
|
|
277
|
+
};
|
|
278
|
+
*self = Self::Made(Box::new(compressor));
|
|
279
|
+
}
|
|
280
|
+
match self {
|
|
281
|
+
Self::Made(compressor) => compressor,
|
|
282
|
+
Self::Unmade(_) => unreachable!("made above"),
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
fn heap_size(&self) -> usize {
|
|
287
|
+
match self {
|
|
288
|
+
Self::Unmade(dict) => dict.as_ref().map_or(0, Vec::capacity),
|
|
289
|
+
Self::Made(compressor) => compressor.heap_size(),
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
}
|
|
293
|
+
|
|
230
294
|
struct BlockCodec {
|
|
231
|
-
|
|
295
|
+
/// None for the fast profile without a dictionary, which compresses
|
|
296
|
+
/// with `block::compress`.
|
|
297
|
+
compressor: Option<Mutex<CompressorSlot>>,
|
|
232
298
|
decompressor: Option<Mutex<Decompressor>>,
|
|
233
|
-
|
|
299
|
+
/// The ratio profile, which releases the GVL from a smaller input.
|
|
300
|
+
ratio: bool,
|
|
301
|
+
/// The compressor's heap bytes, kept up to date by `compress` so `#size`
|
|
302
|
+
/// never waits on a compression that released the GVL.
|
|
303
|
+
compressor_heap: AtomicUsize,
|
|
304
|
+
/// The decompressor's heap bytes: its copy of the dictionary.
|
|
305
|
+
decompressor_heap: usize,
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
impl BlockCodec {
|
|
309
|
+
/// Whether compressing `input_len` bytes takes long enough to release
|
|
310
|
+
/// the GVL for.
|
|
311
|
+
fn should_release_compress_gvl(&self, input_len: usize) -> bool {
|
|
312
|
+
if self.ratio {
|
|
313
|
+
input_len >= GVL_RATIO_COMPRESS_THRESHOLD
|
|
314
|
+
} else {
|
|
315
|
+
should_release_compress_gvl(input_len)
|
|
316
|
+
}
|
|
317
|
+
}
|
|
234
318
|
}
|
|
235
319
|
|
|
236
|
-
fn block_codec_new_impl(class: VALUE, rb_dict: VALUE) -> RbResult<VALUE> {
|
|
237
|
-
let
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
},
|
|
320
|
+
fn block_codec_new_impl(class: VALUE, rb_dict: VALUE, rb_ratio: VALUE) -> RbResult<VALUE> {
|
|
321
|
+
let ratio = rb_ratio == rb::qtrue();
|
|
322
|
+
let dict = rb::value_to_option_bytes(rb_dict)?;
|
|
323
|
+
let decompressor = dict.as_deref().map(Decompressor::with_dict);
|
|
324
|
+
let compressor = (dict.is_some() || ratio).then_some(CompressorSlot::Unmade(dict));
|
|
325
|
+
let codec = BlockCodec {
|
|
326
|
+
compressor_heap: AtomicUsize::new(compressor.as_ref().map_or(0, CompressorSlot::heap_size)),
|
|
327
|
+
decompressor_heap: decompressor.as_ref().map_or(0, Decompressor::heap_size),
|
|
328
|
+
compressor: compressor.map(Mutex::new),
|
|
329
|
+
decompressor: decompressor.map(Mutex::new),
|
|
330
|
+
ratio,
|
|
248
331
|
};
|
|
249
332
|
|
|
250
333
|
unsafe { rb::wrap_typed_data(class, Box::new(codec), block_codec_data_type()) }
|
|
@@ -252,23 +335,19 @@ fn block_codec_new_impl(class: VALUE, rb_dict: VALUE) -> RbResult<VALUE> {
|
|
|
252
335
|
|
|
253
336
|
fn block_codec_size_impl(rb_self: VALUE) -> RbResult<VALUE> {
|
|
254
337
|
let rb_self = unsafe { block_codec_ref(rb_self)? };
|
|
255
|
-
|
|
256
|
-
rb::usize_value(COMPRESSOR_HEAP_SIZE + rb_self.dict_len)
|
|
257
|
-
} else {
|
|
258
|
-
rb::usize_value(0)
|
|
259
|
-
}
|
|
338
|
+
rb::usize_value(rb_self.compressor_heap.load(Ordering::Relaxed) + rb_self.decompressor_heap)
|
|
260
339
|
}
|
|
261
340
|
|
|
262
341
|
fn block_codec_has_dict_impl(rb_self: VALUE) -> RbResult<VALUE> {
|
|
263
342
|
let rb_self = unsafe { block_codec_ref(rb_self)? };
|
|
264
|
-
Ok(rb::bool_value(rb_self.
|
|
343
|
+
Ok(rb::bool_value(rb_self.decompressor.is_some()))
|
|
265
344
|
}
|
|
266
345
|
|
|
267
346
|
fn block_codec_compress_impl(rb_self: VALUE, rb_input: VALUE) -> RbResult<VALUE> {
|
|
268
347
|
let rb_self = unsafe { block_codec_ref(rb_self)? };
|
|
269
348
|
let mut input = rb::input_bytes(rb_input)?;
|
|
270
|
-
let release_gvl = should_release_compress_gvl(input.len());
|
|
271
|
-
input.
|
|
349
|
+
let release_gvl = rb_self.should_release_compress_gvl(input.len());
|
|
350
|
+
input.freeze_for_without_gvl(release_gvl)?;
|
|
272
351
|
|
|
273
352
|
let out = match &rb_self.compressor {
|
|
274
353
|
None => rb::maybe_without_gvl(release_gvl, || block::compress(input.as_slice()))?,
|
|
@@ -277,7 +356,15 @@ fn block_codec_compress_impl(rb_self: VALUE, rb_input: VALUE) -> RbResult<VALUE>
|
|
|
277
356
|
release_gvl,
|
|
278
357
|
"BlockCodec compressor",
|
|
279
358
|
&mut input,
|
|
280
|
-
|
|
|
359
|
+
|slot, input| {
|
|
360
|
+
let out = slot.get(rb_self.ratio).compress(input);
|
|
361
|
+
// The first call makes the compressor, and a ratio one makes
|
|
362
|
+
// its larger tables on first use.
|
|
363
|
+
rb_self
|
|
364
|
+
.compressor_heap
|
|
365
|
+
.store(slot.heap_size(), Ordering::Relaxed);
|
|
366
|
+
Ok(out)
|
|
367
|
+
},
|
|
281
368
|
)?,
|
|
282
369
|
};
|
|
283
370
|
|
|
@@ -292,6 +379,17 @@ fn block_codec_decompress_impl(
|
|
|
292
379
|
let rb_self = unsafe { block_codec_ref(rb_self)? };
|
|
293
380
|
let decompressed_size = rb::value_to_usize(decompressed_size)?;
|
|
294
381
|
let mut compressed = rb::input_bytes(rb_input)?;
|
|
382
|
+
// The size comes from a peer: refuse one the input cannot decode to
|
|
383
|
+
// before the decoder allocates for it.
|
|
384
|
+
if decompressed_size > compressed.len().saturating_mul(LZ4_MAX_EXPANSION) {
|
|
385
|
+
return Err(RubyErr::new(
|
|
386
|
+
decompress_error(),
|
|
387
|
+
format!(
|
|
388
|
+
"lz4 block of {} bytes cannot decode to {decompressed_size} bytes",
|
|
389
|
+
compressed.len()
|
|
390
|
+
),
|
|
391
|
+
));
|
|
392
|
+
}
|
|
295
393
|
|
|
296
394
|
let result = match &rb_self.decompressor {
|
|
297
395
|
None => block::decompress(compressed.as_slice(), decompressed_size),
|
|
@@ -313,8 +411,8 @@ fn block_codec_decompress_impl(
|
|
|
313
411
|
}
|
|
314
412
|
}
|
|
315
413
|
|
|
316
|
-
unsafe extern "C" fn block_codec_new(class: VALUE, rb_dict: VALUE) -> VALUE {
|
|
317
|
-
rb::wrap(|| block_codec_new_impl(class, rb_dict))
|
|
414
|
+
unsafe extern "C" fn block_codec_new(class: VALUE, rb_dict: VALUE, rb_ratio: VALUE) -> VALUE {
|
|
415
|
+
rb::wrap(|| block_codec_new_impl(class, rb_dict, rb_ratio))
|
|
318
416
|
}
|
|
319
417
|
|
|
320
418
|
unsafe extern "C" fn block_codec_size(rb_self: VALUE) -> VALUE {
|
|
@@ -366,7 +464,7 @@ fn frame_codec_compress_impl(rb_self: VALUE, rb_input: VALUE) -> RbResult<VALUE>
|
|
|
366
464
|
let rb_self = unsafe { frame_codec_ref(rb_self)? };
|
|
367
465
|
let mut input = rb::input_bytes(rb_input)?;
|
|
368
466
|
let release_gvl = should_release_compress_gvl(input.len());
|
|
369
|
-
input.
|
|
467
|
+
input.freeze_for_without_gvl(release_gvl)?;
|
|
370
468
|
|
|
371
469
|
let out = rb::maybe_without_gvl(release_gvl, || compress_frame(rb_self, input.as_slice()))?
|
|
372
470
|
.map_err(RubyErr::runtime)?;
|
|
@@ -403,8 +501,6 @@ fn frame_codec_decompress_impl(
|
|
|
403
501
|
let rb_self = unsafe { frame_codec_ref(rb_self)? };
|
|
404
502
|
let max_decompressed_size = rb::value_to_option_usize(max_decompressed_size)?;
|
|
405
503
|
let mut input = rb::input_bytes(rb_input)?;
|
|
406
|
-
let release_gvl = should_release_frame_decompress_gvl(input.len());
|
|
407
|
-
input.lock_for_without_gvl(release_gvl)?;
|
|
408
504
|
|
|
409
505
|
if input.len() < 4 || input.as_slice()[..4] != LZ4_FRAME_MAGIC {
|
|
410
506
|
return Err(RubyErr::new(
|
|
@@ -413,6 +509,9 @@ fn frame_codec_decompress_impl(
|
|
|
413
509
|
));
|
|
414
510
|
}
|
|
415
511
|
|
|
512
|
+
let release_gvl = should_release_frame_decompress_gvl(input.len());
|
|
513
|
+
input.freeze_for_without_gvl(release_gvl)?;
|
|
514
|
+
|
|
416
515
|
let out = rb::maybe_without_gvl(release_gvl, || {
|
|
417
516
|
decompress_frame(rb_self, input.as_slice(), max_decompressed_size)
|
|
418
517
|
})?
|
|
@@ -487,6 +586,10 @@ unsafe extern "C" fn frame_codec_id(rb_self: VALUE) -> VALUE {
|
|
|
487
586
|
|
|
488
587
|
const LZ4_MAX_DISTANCE: usize = 65535;
|
|
489
588
|
|
|
589
|
+
/// The most bytes one byte of an LZ4 block decodes to: a match length
|
|
590
|
+
/// extension byte adds up to 255.
|
|
591
|
+
const LZ4_MAX_EXPANSION: usize = 255;
|
|
592
|
+
|
|
490
593
|
struct RbDictTrainer {
|
|
491
594
|
inner: Mutex<Option<DictTrainer>>,
|
|
492
595
|
max_dict_size: usize,
|
|
@@ -640,7 +743,7 @@ fn init() -> RbResult<()> {
|
|
|
640
743
|
let codec_class = unsafe { rb::define_class_under(module, c"BlockCodec", rb_sys::rb_cObject)? };
|
|
641
744
|
unsafe {
|
|
642
745
|
rb::undef_alloc_func(codec_class)?;
|
|
643
|
-
rb::
|
|
746
|
+
rb::define_singleton_method_2(codec_class, c"_native_new", block_codec_new)?;
|
|
644
747
|
rb::define_method_0(codec_class, c"size", block_codec_size)?;
|
|
645
748
|
rb::define_method_0(codec_class, c"has_dict?", block_codec_has_dict)?;
|
|
646
749
|
rb::define_method_1(codec_class, c"compress", block_codec_compress)?;
|
data/ext/lz4rip/src/rb.rs
CHANGED
|
@@ -6,13 +6,6 @@ use rb_sys::{rb_data_type_t, VALUE};
|
|
|
6
6
|
|
|
7
7
|
pub type RbResult<T = VALUE> = Result<T, RubyErr>;
|
|
8
8
|
|
|
9
|
-
#[cfg(ruby_engine = "mri")]
|
|
10
|
-
unsafe extern "C" {
|
|
11
|
-
fn rb_obj_frozen_p(obj: VALUE) -> VALUE;
|
|
12
|
-
fn rb_str_locktmp(str: VALUE) -> VALUE;
|
|
13
|
-
fn rb_str_unlocktmp(str: VALUE) -> VALUE;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
9
|
#[cfg(ruby_engine = "mri")]
|
|
17
10
|
type RbWithoutGvlFunc = unsafe extern "C" fn(*mut c_void) -> *mut c_void;
|
|
18
11
|
#[cfg(ruby_engine = "mri")]
|
|
@@ -185,7 +178,7 @@ where
|
|
|
185
178
|
output: None,
|
|
186
179
|
};
|
|
187
180
|
// Ruby checks interrupts before and after the callback. Protect the call
|
|
188
|
-
// so an exception still drops native output and
|
|
181
|
+
// so an exception still drops native output and borrowed strings.
|
|
189
182
|
protect_unit(|| unsafe {
|
|
190
183
|
rb_thread_call_without_gvl(
|
|
191
184
|
Some(without_gvl_trampoline::<F, R>),
|
|
@@ -277,19 +270,19 @@ pub fn value_to_bytes(value: VALUE) -> RbResult<Vec<u8>> {
|
|
|
277
270
|
bytes_from_string_value(string).map(|bytes| bytes.to_vec())
|
|
278
271
|
}
|
|
279
272
|
|
|
280
|
-
pub fn
|
|
273
|
+
pub fn value_to_option_usize(value: VALUE) -> RbResult<Option<usize>> {
|
|
281
274
|
if value == qnil() {
|
|
282
275
|
Ok(None)
|
|
283
276
|
} else {
|
|
284
|
-
|
|
277
|
+
value_to_usize(value).map(Some)
|
|
285
278
|
}
|
|
286
279
|
}
|
|
287
280
|
|
|
288
|
-
pub fn
|
|
281
|
+
pub fn value_to_option_bytes(value: VALUE) -> RbResult<Option<Vec<u8>>> {
|
|
289
282
|
if value == qnil() {
|
|
290
283
|
Ok(None)
|
|
291
284
|
} else {
|
|
292
|
-
|
|
285
|
+
value_to_bytes(value).map(Some)
|
|
293
286
|
}
|
|
294
287
|
}
|
|
295
288
|
|
|
@@ -314,14 +307,14 @@ impl InputBytes {
|
|
|
314
307
|
self.as_slice().len()
|
|
315
308
|
}
|
|
316
309
|
|
|
317
|
-
pub fn
|
|
310
|
+
pub fn freeze_for_without_gvl(&mut self, release_gvl: bool) -> RbResult<()> {
|
|
318
311
|
if !release_gvl {
|
|
319
312
|
return Ok(());
|
|
320
313
|
}
|
|
321
314
|
|
|
322
315
|
match self {
|
|
323
316
|
#[cfg(ruby_engine = "mri")]
|
|
324
|
-
Self::Borrowed(bytes) => bytes.
|
|
317
|
+
Self::Borrowed(bytes) => bytes.freeze(),
|
|
325
318
|
#[cfg(not(ruby_engine = "mri"))]
|
|
326
319
|
Self::Owned(_) => Ok(()),
|
|
327
320
|
}
|
|
@@ -333,7 +326,7 @@ pub struct BorrowedStringBytes {
|
|
|
333
326
|
value: VALUE,
|
|
334
327
|
ptr: *const u8,
|
|
335
328
|
len: usize,
|
|
336
|
-
|
|
329
|
+
frozen: bool,
|
|
337
330
|
}
|
|
338
331
|
|
|
339
332
|
#[cfg(ruby_engine = "mri")]
|
|
@@ -345,7 +338,7 @@ impl BorrowedStringBytes {
|
|
|
345
338
|
value: string,
|
|
346
339
|
ptr: bytes.as_ptr(),
|
|
347
340
|
len: bytes.len(),
|
|
348
|
-
|
|
341
|
+
frozen: false,
|
|
349
342
|
})
|
|
350
343
|
}
|
|
351
344
|
|
|
@@ -357,13 +350,20 @@ impl BorrowedStringBytes {
|
|
|
357
350
|
}
|
|
358
351
|
}
|
|
359
352
|
|
|
360
|
-
|
|
361
|
-
|
|
353
|
+
// Borrow from a frozen copy before releasing the GVL. The copy shares the
|
|
354
|
+
// heap buffer, and a write to the caller's String copies that buffer first,
|
|
355
|
+
// so other threads may mutate or borrow the caller's String meanwhile.
|
|
356
|
+
fn freeze(&mut self) -> RbResult<()> {
|
|
357
|
+
if self.frozen {
|
|
362
358
|
return Ok(());
|
|
363
359
|
}
|
|
364
360
|
|
|
365
|
-
protect_value(|| unsafe {
|
|
366
|
-
|
|
361
|
+
let frozen = protect_value(|| unsafe { rb_sys::rb_str_new_frozen(self.value) })?;
|
|
362
|
+
let bytes = bytes_from_string_value(frozen)?;
|
|
363
|
+
self.value = frozen;
|
|
364
|
+
self.ptr = bytes.as_ptr();
|
|
365
|
+
self.len = bytes.len();
|
|
366
|
+
self.frozen = true;
|
|
367
367
|
Ok(())
|
|
368
368
|
}
|
|
369
369
|
}
|
|
@@ -371,11 +371,6 @@ impl BorrowedStringBytes {
|
|
|
371
371
|
#[cfg(ruby_engine = "mri")]
|
|
372
372
|
impl Drop for BorrowedStringBytes {
|
|
373
373
|
fn drop(&mut self) {
|
|
374
|
-
if self.locked {
|
|
375
|
-
unsafe {
|
|
376
|
-
rb_str_unlocktmp(self.value);
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
374
|
let _ = rb_sys::rb_gc_guard!(self.value);
|
|
380
375
|
}
|
|
381
376
|
}
|
data/lib/lz4rip/block_codec.rb
CHANGED
|
@@ -8,14 +8,10 @@ module Lz4rip
|
|
|
8
8
|
# `BlockCodec` keeps mutable native scratch state and is intended to be used
|
|
9
9
|
# per Ractor.
|
|
10
10
|
#
|
|
11
|
-
# @!method self.
|
|
12
|
-
# Create a block codec.
|
|
13
|
-
# @param dict [Dictionary, String, nil] optional LZ4 dictionary
|
|
14
|
-
# @return [BlockCodec]
|
|
15
|
-
#
|
|
16
|
-
# @!method self._native_new(dict)
|
|
11
|
+
# @!method self._native_new(dict, ratio)
|
|
17
12
|
# Native constructor used by `.new`.
|
|
18
13
|
# @param dict [String, nil] dictionary bytes
|
|
14
|
+
# @param ratio [Boolean] use the ratio profile
|
|
19
15
|
# @return [BlockCodec]
|
|
20
16
|
#
|
|
21
17
|
# @!method compress(bytes)
|
|
@@ -23,13 +19,6 @@ module Lz4rip
|
|
|
23
19
|
# @param bytes [String] uncompressed bytes
|
|
24
20
|
# @return [String]
|
|
25
21
|
#
|
|
26
|
-
# @!method decompress(bytes, decompressed_size:)
|
|
27
|
-
# Decompress a raw LZ4 block.
|
|
28
|
-
# @param bytes [String] compressed LZ4 block
|
|
29
|
-
# @param decompressed_size [Integer] original byte size
|
|
30
|
-
# @return [String]
|
|
31
|
-
# @raise [DecompressError]
|
|
32
|
-
#
|
|
33
22
|
# @!method _decompress(bytes, decompressed_size)
|
|
34
23
|
# Native decompression entry used by #decompress.
|
|
35
24
|
# @param bytes [String] compressed LZ4 block
|
|
@@ -41,15 +30,56 @@ module Lz4rip
|
|
|
41
30
|
# @return [Boolean]
|
|
42
31
|
#
|
|
43
32
|
# @!method size
|
|
44
|
-
#
|
|
33
|
+
# Bytes the codec holds natively: its compressor tables made so far and
|
|
34
|
+
# its copies of the dictionary.
|
|
35
|
+
# @return [Integer]
|
|
45
36
|
class BlockCodec
|
|
46
|
-
|
|
47
|
-
|
|
37
|
+
PROFILES = %i[fast ratio].freeze
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
# Create a block codec.
|
|
41
|
+
#
|
|
42
|
+
# The `:ratio` profile compresses to smaller output, more slowly, and
|
|
43
|
+
# decodes with any LZ4 decoder. It holds an 8 KB table, plus 64 KB from
|
|
44
|
+
# its first input above 4 KiB. With a dictionary it hashes the dictionary
|
|
45
|
+
# once and holds 16 KB of tables while input and dictionary stay within
|
|
46
|
+
# 4 KiB, 64 KB more for larger inputs. Either profile makes its compressor
|
|
47
|
+
# on the first `#compress`, so a codec that only decompresses holds only
|
|
48
|
+
# its dictionary.
|
|
49
|
+
#
|
|
50
|
+
# @param dict [Dictionary, String, nil] optional LZ4 dictionary
|
|
51
|
+
# @param profile [Symbol] `:fast` or `:ratio`
|
|
52
|
+
# @return [BlockCodec]
|
|
53
|
+
# @raise [ArgumentError] on an unknown profile
|
|
54
|
+
def self.new(dict: nil, profile: :fast)
|
|
55
|
+
unless PROFILES.include?(profile)
|
|
56
|
+
raise ArgumentError, "profile must be :fast or :ratio, got #{profile.inspect}"
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
_native_new(Dictionary === dict ? dict.bytes : dict, profile == :ratio)
|
|
48
60
|
end
|
|
49
61
|
|
|
50
62
|
|
|
51
|
-
|
|
63
|
+
# Decompress a raw LZ4 block.
|
|
64
|
+
#
|
|
65
|
+
# `decompressed_size` is the original size, often declared by a peer. A
|
|
66
|
+
# size the block cannot decode to (more than 255 times its length), or
|
|
67
|
+
# above `max_decompressed_size`, raises before anything is allocated for
|
|
68
|
+
# it.
|
|
69
|
+
#
|
|
70
|
+
# @param bytes [String] compressed LZ4 block
|
|
71
|
+
# @param decompressed_size [Integer] original byte size
|
|
72
|
+
# @param max_decompressed_size [Integer, nil] largest size to accept
|
|
73
|
+
# @return [String]
|
|
74
|
+
# @raise [DecompressError]
|
|
75
|
+
def decompress(bytes, decompressed_size:, max_decompressed_size: nil)
|
|
76
|
+
if max_decompressed_size && decompressed_size > max_decompressed_size
|
|
77
|
+
raise DecompressError,
|
|
78
|
+
"decompressed size #{decompressed_size} exceeds max_decompressed_size #{max_decompressed_size}"
|
|
79
|
+
end
|
|
80
|
+
|
|
52
81
|
_decompress(bytes, decompressed_size)
|
|
53
82
|
end
|
|
83
|
+
|
|
54
84
|
end
|
|
55
85
|
end
|
data/lib/lz4rip/version.rb
CHANGED