Skip to content

Commit

Permalink
Add no_c_export feature to disable no_mangle so we don't need objco…
Browse files Browse the repository at this point in the history
…py for benching

Also fixes name of linked library in performance.sh
  • Loading branch information
oyvindln committed Oct 15, 2017
1 parent 517016d commit dec3db2
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 94 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ cc = "1.0"
[features]
default = []
miniz_zip = ["build_stub_miniz"]
fuzzing = ["build_orig_miniz"]
benching = ["build_orig_miniz"]
fuzzing = ["build_orig_miniz", "no_c_export"]
benching = ["build_orig_miniz", "no_c_export"]
build_orig_miniz = []
build_stub_miniz = []
no_c_export = []

[profile.dev]
panic = "abort"
Expand Down
84 changes: 46 additions & 38 deletions benches/bench.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ use libc::{c_int, c_void};
use miniz_oxide::deflate::compress_to_vec;
use miniz_oxide::deflate::core::{create_comp_flags_from_zip_params, CompressorOxide};

use miniz_oxide_c_api::{miniz_def_free_func, tdefl_compress_mem_to_heap,
tinfl_decompress_mem_to_heap};
use miniz_oxide_c_api::miniz_def_free_func;

/// Safe wrapper around a buffer.
pub struct HeapBuf {
Expand All @@ -35,22 +34,6 @@ fn w(buf: *mut c_void) -> HeapBuf {
HeapBuf { buf: buf }
}

extern "C" {
fn c_tinfl_decompress_mem_to_heap(
src_buf: *const c_void,
src_buf_len: usize,
out_len: *mut usize,
flags: c_int,
) -> *mut c_void;

fn c_tdefl_compress_mem_to_heap(
src_buf: *const c_void,
src_buf_len: usize,
out_len: *mut usize,
flags: c_int,
) -> *mut c_void;
}

fn get_test_file_data(name: &str) -> Vec<u8> {
use std::fs::File;
let mut input = Vec::new();
Expand All @@ -69,7 +52,7 @@ macro_rules! decompress_bench {

let mut out_len: usize = 0;
b.iter(|| unsafe {
::w(::$decompress_func(
::w($decompress_func(
compressed.as_ptr() as *mut ::c_void,
compressed.len(),
&mut out_len,
Expand All @@ -89,7 +72,7 @@ macro_rules! compress_bench {
let mut out_len: usize = 0;
let flags = ::create_comp_flags_from_zip_params($level, -15, 0) as i32;
b.iter(|| unsafe {
::w(::$compress_func(
::w($compress_func(
input.as_ptr() as *mut ::c_void,
input.len(),
&mut out_len,
Expand All @@ -101,6 +84,8 @@ macro_rules! compress_bench {
}

mod oxide {
use miniz_oxide_c_api::{tdefl_compress_mem_to_heap, tinfl_decompress_mem_to_heap};

compress_bench!(
compress_bin_lvl_1,
tdefl_compress_mem_to_heap,
Expand Down Expand Up @@ -217,116 +202,139 @@ mod oxide {
}

mod miniz {
use libc::{c_void, c_int};

/// Functions from miniz
/// We add the link attribute to make sure
/// these are linked to the miniz ones rather than
/// picking up the rust versions (as they may be exported).
#[link(name = "miniz")]
extern "C" {
fn tinfl_decompress_mem_to_heap(
src_buf: *const c_void,
src_buf_len: usize,
out_len: *mut usize,
flags: c_int,
) -> *mut c_void;

fn tdefl_compress_mem_to_heap(
src_buf: *const c_void,
src_buf_len: usize,
out_len: *mut usize,
flags: c_int,
) -> *mut c_void;
}

compress_bench!(
compress_bin_lvl_1,
c_tdefl_compress_mem_to_heap,
tdefl_compress_mem_to_heap,
1,
"benches/data/bin"
);
compress_bench!(
compress_bin_lvl_6,
c_tdefl_compress_mem_to_heap,
tdefl_compress_mem_to_heap,
6,
"benches/data/bin"
);
compress_bench!(
compress_bin_lvl_9,
c_tdefl_compress_mem_to_heap,
tdefl_compress_mem_to_heap,
9,
"benches/data/bin"
);

compress_bench!(
compress_code_lvl_1,
c_tdefl_compress_mem_to_heap,
tdefl_compress_mem_to_heap,
1,
"benches/data/code"
);
compress_bench!(
compress_code_lvl_6,
c_tdefl_compress_mem_to_heap,
tdefl_compress_mem_to_heap,
6,
"benches/data/code"
);
compress_bench!(
compress_code_lvl_9,
c_tdefl_compress_mem_to_heap,
tdefl_compress_mem_to_heap,
9,
"benches/data/code"
);

compress_bench!(
compress_compressed_lvl_1,
c_tdefl_compress_mem_to_heap,
tdefl_compress_mem_to_heap,
1,
"benches/data/compressed"
);
compress_bench!(
compress_compressed_lvl_6,
c_tdefl_compress_mem_to_heap,
tdefl_compress_mem_to_heap,
6,
"benches/data/compressed"
);
compress_bench!(
compress_compressed_lvl_9,
c_tdefl_compress_mem_to_heap,
tdefl_compress_mem_to_heap,
9,
"benches/data/compressed"
);

decompress_bench!(
decompress_bin_lvl_1,
c_tinfl_decompress_mem_to_heap,
tinfl_decompress_mem_to_heap,
1,
"benches/data/bin"
);
decompress_bench!(
decompress_bin_lvl_6,
c_tinfl_decompress_mem_to_heap,
tinfl_decompress_mem_to_heap,
6,
"benches/data/bin"
);
decompress_bench!(
decompress_bin_lvl_9,
c_tinfl_decompress_mem_to_heap,
tinfl_decompress_mem_to_heap,
9,
"benches/data/bin"
);

decompress_bench!(
decompress_code_lvl_1,
c_tinfl_decompress_mem_to_heap,
tinfl_decompress_mem_to_heap,
1,
"benches/data/code"
);
decompress_bench!(
decompress_code_lvl_6,
c_tinfl_decompress_mem_to_heap,
tinfl_decompress_mem_to_heap,
6,
"benches/data/code"
);
decompress_bench!(
decompress_code_lvl_9,
c_tinfl_decompress_mem_to_heap,
tinfl_decompress_mem_to_heap,
9,
"benches/data/code"
);

decompress_bench!(
decompress_compressed_lvl_1,
c_tinfl_decompress_mem_to_heap,
tinfl_decompress_mem_to_heap,
1,
"benches/data/compressed"
);
decompress_bench!(
decompress_compressed_lvl_6,
c_tinfl_decompress_mem_to_heap,
tinfl_decompress_mem_to_heap,
6,
"benches/data/compressed"
);
decompress_bench!(
decompress_compressed_lvl_9,
c_tinfl_decompress_mem_to_heap,
tinfl_decompress_mem_to_heap,
9,
"benches/data/compressed"
);
Expand Down
2 changes: 1 addition & 1 deletion performance.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -e
./build.sh --release

mkdir -p bin
g++ tests/miniz_tester.cpp tests/timer.cpp -o bin/miniz_tester -I. -O2 -L. -lminiz_oxide -lutil -ldl -lrt -lpthread -lgcc_s -lc -lm -lrt -lpthread -lutil
g++ tests/miniz_tester.cpp tests/timer.cpp -o bin/miniz_tester -I. -O2 -L. -lminiz_oxide_c_api -lutil -ldl -lrt -lpthread -lgcc_s -lc -lm -lrt -lpthread -lutil
#g++ tests/miniz_tester.cpp tests/timer.cpp miniz/* -o bin/miniz_tester -I. -O2

mkdir -p test_scratch
Expand Down
17 changes: 11 additions & 6 deletions src/build.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#[cfg(feature = "build_stub_miniz")]
#[cfg(any(feature = "build_stub_miniz", feature = "build_orig_miniz"))]
extern crate cc;

#[cfg(not(any(feature = "build_stub_miniz", feature = "build_orig_miniz")))]
Expand All @@ -20,9 +20,14 @@ fn main() {

#[cfg(feature = "build_orig_miniz")]
fn main() {
use std::process::Command;

Command::new("./build_orig_miniz.sh").status().unwrap();
println!("cargo:rustc-link-search=native=bin");
println!("cargo:rustc-link-lib=static=miniz");
cc::Build::new()
.files(
&[
"miniz/miniz.c",
"miniz/miniz_zip.c",
"miniz/miniz_tinfl.c",
"miniz/miniz_tdef.c",
],
)
.compile("libminiz.a");
}
Loading

0 comments on commit dec3db2

Please sign in to comment.