Permalink
Browse files

Compress metadata section. Seems a minor speed win, major space win.

  • Loading branch information...
1 parent 31bbcf0 commit b769e296803f578d99654e9fc271c4109e50ab75 @graydon graydon committed Aug 28, 2012
Showing with 1,978 additions and 2 deletions.
  1. +5 −0 LICENSE.txt
  2. +1 −0 mk/rt.mk
  3. +1 −0 mk/tests.mk
  4. +3 −0 src/libcore/core.rc
  5. +78 −0 src/libcore/flate.rs
  6. +1,884 −0 src/rt/miniz.cpp
  7. +2 −0 src/rt/rustrt.def.in
  8. +1 −1 src/rustc/metadata/encoder.rs
  9. +3 −1 src/rustc/metadata/loader.rs
View
@@ -235,6 +235,11 @@ included:
BSD-compatible licenses. See src/libuv/LICENSE for
details.
+* The src/rt/miniz.c file, carrying an implementation of
+ RFC1950/RFC1951 DEFLATE, by Rich Geldreich
+ <richgel99@gmail.com>. All uses of this file are
+ permitted by the embedded "unlicense" notice
+ (effectively: public domain with warranty disclaimer).
* LLVM and Clang. Code for this package is found in
src/llvm.
View
@@ -65,6 +65,7 @@ RUNTIME_CS_$(1) := \
rt/rust_util.cpp \
rt/circular_buffer.cpp \
rt/isaac/randport.cpp \
+ rt/miniz.cpp \
rt/rust_kernel.cpp \
rt/rust_shape.cpp \
rt/rust_abi.cpp \
View
@@ -121,6 +121,7 @@ ALL_CS := $(wildcard $(S)src/rt/*.cpp \
$(S)srcrustllvm/*.cpp)
ALL_CS := $(filter-out $(S)src/rt/bigint/bigint_ext.cpp \
$(S)src/rt/bigint/bigint_int.cpp \
+ $(S)src/rt/miniz.cpp \
,$(ALL_CS))
ALL_HS := $(wildcard $(S)src/rt/*.h \
$(S)src/rt/*/*.h \
View
@@ -56,6 +56,7 @@ export cmp;
export num;
export path, path2;
export managed;
+export flate;
// NDM seems to be necessary for resolve to work
export option_iter;
@@ -265,6 +266,8 @@ mod unsafe;
mod managed;
+mod flate;
+
// Modules supporting compiler-generated code
// Exported but not part of the public interface
View
@@ -0,0 +1,78 @@
+import libc::{c_void, size_t, c_int};
+
+extern mod rustrt {
+
+ fn tdefl_compress_mem_to_heap(psrc_buf: *const c_void,
+ src_buf_len: size_t,
+ pout_len: *size_t,
+ flags: c_int) -> *c_void;
+
+ fn tinfl_decompress_mem_to_heap(psrc_buf: *const c_void,
+ src_buf_len: size_t,
+ pout_len: *size_t,
+ flags: c_int) -> *c_void;
+}
+
+const lz_none : c_int = 0x0; // Huffman-coding only.
+const lz_fast : c_int = 0x1; // LZ with only one probe
+const lz_norm : c_int = 0x80; // LZ with 128 probes, "normal"
+const lz_best : c_int = 0xfff; // LZ with 4095 probes, "best"
+
+fn deflate_buf(buf: &[const u8]) -> ~[u8] {
+ do vec::as_const_buf(buf) |b, len| {
+ unsafe {
+ let mut outsz : size_t = 0;
+ let res =
+ rustrt::tdefl_compress_mem_to_heap(b as *c_void,
+ len as size_t,
+ ptr::addr_of(outsz),
+ lz_norm);
+ assert res as int != 0;
+ let out = vec::unsafe::from_buf(res as *u8,
+ outsz as uint);
+ libc::free(res);
+ out
+ }
+ }
+}
+
+fn inflate_buf(buf: &[const u8]) -> ~[u8] {
+ do vec::as_const_buf(buf) |b, len| {
+ unsafe {
+ let mut outsz : size_t = 0;
+ let res =
+ rustrt::tinfl_decompress_mem_to_heap(b as *c_void,
+ len as size_t,
+ ptr::addr_of(outsz),
+ 0);
+ assert res as int != 0;
+ let out = vec::unsafe::from_buf(res as *u8,
+ outsz as uint);
+ libc::free(res);
+ out
+ }
+ }
+}
+
+#[test]
+fn test_flate_round_trip() {
+ let r = rand::Rng();
+ let mut words = ~[];
+ for 20.times {
+ vec::push(words, r.gen_bytes(r.gen_uint_range(1, 10)));
+ }
+ for 20.times {
+ let mut in = ~[];
+ for 2000.times {
+ vec::push_all(in, r.choose(words));
+ }
+ debug!("de/inflate of %u bytes of random word-sequences",
+ in.len());
+ let cmp = flate::deflate_buf(in);
+ let out = flate::inflate_buf(cmp);
+ debug!("%u bytes deflated to %u (%.1f%% size)",
+ in.len(), cmp.len(),
+ 100.0 * ((cmp.len() as float) / (in.len() as float)));
+ assert(in == out);
+ }
+}
Oops, something went wrong.

0 comments on commit b769e29

Please sign in to comment.