Skip to content

Commit b769e29

Browse files
committed
Compress metadata section. Seems a minor speed win, major space win.
1 parent 31bbcf0 commit b769e29

File tree

9 files changed

+1978
-2
lines changed

9 files changed

+1978
-2
lines changed

LICENSE.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,11 @@ included:
235235
BSD-compatible licenses. See src/libuv/LICENSE for
236236
details.
237237

238+
* The src/rt/miniz.c file, carrying an implementation of
239+
RFC1950/RFC1951 DEFLATE, by Rich Geldreich
240+
<[email protected]>. All uses of this file are
241+
permitted by the embedded "unlicense" notice
242+
(effectively: public domain with warranty disclaimer).
238243

239244
* LLVM and Clang. Code for this package is found in
240245
src/llvm.

mk/rt.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ RUNTIME_CS_$(1) := \
6565
rt/rust_util.cpp \
6666
rt/circular_buffer.cpp \
6767
rt/isaac/randport.cpp \
68+
rt/miniz.cpp \
6869
rt/rust_kernel.cpp \
6970
rt/rust_shape.cpp \
7071
rt/rust_abi.cpp \

mk/tests.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ ALL_CS := $(wildcard $(S)src/rt/*.cpp \
121121
$(S)srcrustllvm/*.cpp)
122122
ALL_CS := $(filter-out $(S)src/rt/bigint/bigint_ext.cpp \
123123
$(S)src/rt/bigint/bigint_int.cpp \
124+
$(S)src/rt/miniz.cpp \
124125
,$(ALL_CS))
125126
ALL_HS := $(wildcard $(S)src/rt/*.h \
126127
$(S)src/rt/*/*.h \

src/libcore/core.rc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export cmp;
5656
export num;
5757
export path, path2;
5858
export managed;
59+
export flate;
5960

6061
// NDM seems to be necessary for resolve to work
6162
export option_iter;
@@ -265,6 +266,8 @@ mod unsafe;
265266

266267
mod managed;
267268

269+
mod flate;
270+
268271
// Modules supporting compiler-generated code
269272
// Exported but not part of the public interface
270273

src/libcore/flate.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import libc::{c_void, size_t, c_int};
2+
3+
extern mod rustrt {
4+
5+
fn tdefl_compress_mem_to_heap(psrc_buf: *const c_void,
6+
src_buf_len: size_t,
7+
pout_len: *size_t,
8+
flags: c_int) -> *c_void;
9+
10+
fn tinfl_decompress_mem_to_heap(psrc_buf: *const c_void,
11+
src_buf_len: size_t,
12+
pout_len: *size_t,
13+
flags: c_int) -> *c_void;
14+
}
15+
16+
const lz_none : c_int = 0x0; // Huffman-coding only.
17+
const lz_fast : c_int = 0x1; // LZ with only one probe
18+
const lz_norm : c_int = 0x80; // LZ with 128 probes, "normal"
19+
const lz_best : c_int = 0xfff; // LZ with 4095 probes, "best"
20+
21+
fn deflate_buf(buf: &[const u8]) -> ~[u8] {
22+
do vec::as_const_buf(buf) |b, len| {
23+
unsafe {
24+
let mut outsz : size_t = 0;
25+
let res =
26+
rustrt::tdefl_compress_mem_to_heap(b as *c_void,
27+
len as size_t,
28+
ptr::addr_of(outsz),
29+
lz_norm);
30+
assert res as int != 0;
31+
let out = vec::unsafe::from_buf(res as *u8,
32+
outsz as uint);
33+
libc::free(res);
34+
out
35+
}
36+
}
37+
}
38+
39+
fn inflate_buf(buf: &[const u8]) -> ~[u8] {
40+
do vec::as_const_buf(buf) |b, len| {
41+
unsafe {
42+
let mut outsz : size_t = 0;
43+
let res =
44+
rustrt::tinfl_decompress_mem_to_heap(b as *c_void,
45+
len as size_t,
46+
ptr::addr_of(outsz),
47+
0);
48+
assert res as int != 0;
49+
let out = vec::unsafe::from_buf(res as *u8,
50+
outsz as uint);
51+
libc::free(res);
52+
out
53+
}
54+
}
55+
}
56+
57+
#[test]
58+
fn test_flate_round_trip() {
59+
let r = rand::Rng();
60+
let mut words = ~[];
61+
for 20.times {
62+
vec::push(words, r.gen_bytes(r.gen_uint_range(1, 10)));
63+
}
64+
for 20.times {
65+
let mut in = ~[];
66+
for 2000.times {
67+
vec::push_all(in, r.choose(words));
68+
}
69+
debug!("de/inflate of %u bytes of random word-sequences",
70+
in.len());
71+
let cmp = flate::deflate_buf(in);
72+
let out = flate::inflate_buf(cmp);
73+
debug!("%u bytes deflated to %u (%.1f%% size)",
74+
in.len(), cmp.len(),
75+
100.0 * ((cmp.len() as float) / (in.len() as float)));
76+
assert(in == out);
77+
}
78+
}

0 commit comments

Comments
 (0)