Skip to content

Commit

Permalink
fix: make std feature disable actually work
Browse files Browse the repository at this point in the history
Previously it didn't really work due to dependencies we pulled in in
no-std mode requiring std.
  • Loading branch information
AlexTMjugador committed May 18, 2024
1 parent b7affb9 commit 58b1081
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 32 deletions.
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 17 additions & 11 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,23 @@ repository = "https://github.com/zopfli-rs/zopfli"
readme = "README.md"
categories = ["compression", "no-std"]
exclude = [
".github/*",
".gitignore",
"Makefile",
"benchmark-builds/*",
"rustfmt.toml",
"test/*",
".github/*",
".gitignore",
"Makefile",
"benchmark-builds/*",
"rustfmt.toml",
"test/*",
]
edition = "2021"
rust-version = "1.66"

[dependencies]
crc32fast = { version = "1.4.0", default-features = false, optional = true }
simd-adler32 = { version = "0.3.7", default-features = false, optional = true }
typed-arena = { version = "2.0.2", default-features = false }
log = "0.4.21"
lockfree-object-pool = "0.1.5"
once_cell = "1.19.0"
bumpalo = "3.16.0"
log = { version = "0.4.21", optional = true }
lockfree-object-pool = { version = "0.1.5", optional = true }
once_cell = { version = "1.19.0", optional = true }

[dev-dependencies]
proptest = "1.4.0"
Expand All @@ -37,7 +37,13 @@ default = ["std", "gzip", "zlib"]
gzip = ["dep:crc32fast"]
zlib = ["dep:simd-adler32"]

std = ["crc32fast?/std", "simd-adler32?/std"]
std = [
"dep:log",
"dep:lockfree-object-pool",
"dep:once_cell",
"crc32fast?/std",
"simd-adler32?/std",
]
nightly = ["crc32fast?/nightly"]

[[bin]]
Expand Down
1 change: 1 addition & 0 deletions src/blocksplitter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use alloc::vec::Vec;

#[cfg(feature = "std")]
use log::{debug, log_enabled};

use crate::{cache::NoCache, deflate::calculate_block_size_auto_type, lz77::Lz77Store};
Expand Down
1 change: 1 addition & 0 deletions src/deflate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use alloc::vec::Vec;
use core::{cmp, iter};

#[cfg(feature = "std")]
use log::{debug, log_enabled};

use crate::{
Expand Down
17 changes: 17 additions & 0 deletions src/hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use alloc::{
};
use core::ptr::{addr_of, addr_of_mut, NonNull};

#[cfg(feature = "std")]
use lockfree_object_pool::LinearObjectPool;
#[cfg(feature = "std")]
use once_cell::sync::Lazy;

use crate::util::{ZOPFLI_MIN_MATCH, ZOPFLI_WINDOW_MASK, ZOPFLI_WINDOW_SIZE};
Expand Down Expand Up @@ -180,5 +182,20 @@ impl ZopfliHash {
}
}

#[cfg(feature = "std")]
pub static HASH_POOL: Lazy<LinearObjectPool<Box<ZopfliHash>>> =
Lazy::new(|| LinearObjectPool::new(ZopfliHash::new, |boxed| boxed.reset()));

#[cfg(not(feature = "std"))]
#[derive(Copy, Clone)]
pub struct ZopfliHashFactory;

#[cfg(not(feature = "std"))]
impl ZopfliHashFactory {
pub fn pull(self) -> Box<ZopfliHash> {
ZopfliHash::new()
}
}

#[cfg(not(feature = "std"))]
pub static HASH_POOL: ZopfliHashFactory = ZopfliHashFactory;
6 changes: 3 additions & 3 deletions src/katajainen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ use core::{
cmp::{self, Ordering},
};

use typed_arena::Arena;
use bumpalo::Bump;

// Bounded package merge algorithm, based on the paper
// "A Fast and Space-Economical Algorithm for Length-Limited Coding
// Jyrki Katajainen, Alistair Moffat, Andrew Turpin".

struct Thing<'a> {
node_arena: &'a Arena<Node<'a>>,
node_arena: &'a Bump,
leaves: Vec<Leaf>,
lists: [List<'a>; 15],
}
Expand Down Expand Up @@ -94,7 +94,7 @@ pub fn length_limited_code_lengths(frequencies: &[usize], max_bits: usize) -> Ve
assert!(max_bits <= 15);

let arena_capacity = max_bits * 2 * num_symbols;
let node_arena: Arena<Node> = Arena::with_capacity(arena_capacity);
let node_arena = Bump::with_capacity(arena_capacity);

let node0 = node_arena.alloc(Node {
weight: leaves[0].weight,
Expand Down
21 changes: 18 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@
feature(core_intrinsics)
)]

// No-op log implementation for no-std targets
#[cfg(not(feature = "std"))]
macro_rules! debug {
( $( $_:expr ),* ) => {};
}
#[cfg(not(feature = "std"))]
macro_rules! trace {
( $( $_:expr ),* ) => {};
}
#[cfg(not(feature = "std"))]
macro_rules! log_enabled {
( $( $_:expr ),* ) => {
false
};
}

#[cfg_attr(not(feature = "std"), macro_use)]
extern crate alloc;

Expand Down Expand Up @@ -74,8 +90,6 @@ use std::io::{Error, Write};
#[cfg(any(doc, not(feature = "std")))]
pub use io::{Error, ErrorKind, Write};

use crate::hash::HASH_POOL;

/// Options for the Zopfli compression algorithm.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(all(test, feature = "std"), derive(proptest_derive::Arbitrary))]
Expand Down Expand Up @@ -176,8 +190,9 @@ pub fn compress<R: std::io::Read, W: Write>(

/// Populates object pools for expensive objects that Zopfli uses. Call this on a background thread
/// when you know ahead of time that compression will be needed.
#[cfg(feature = "std")]
pub fn prewarm_object_pools() {
HASH_POOL.pull();
hash::HASH_POOL.pull();
}

#[cfg(all(test, feature = "std"))]
Expand Down
6 changes: 2 additions & 4 deletions src/lz77.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ impl Lz77Store {
return;
}
let windowstart = instart.saturating_sub(ZOPFLI_WINDOW_SIZE);
let hash_pool = &*HASH_POOL;
let mut h = hash_pool.pull();
let mut h = HASH_POOL.pull();

let arr = &in_data[..inend];
h.warmup(arr, windowstart, inend);
Expand Down Expand Up @@ -240,8 +239,7 @@ impl Lz77Store {
return;
}

let hash_pool = &*HASH_POOL;
let mut h = hash_pool.pull();
let mut h = HASH_POOL.pull();

let arr = &in_data[..inend];
h.warmup(arr, windowstart, inend);
Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::{
io::{self, prelude::*},
};

#[cfg(feature = "std")]
use log::info;

fn main() {
Expand Down
7 changes: 3 additions & 4 deletions src/squeeze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use alloc::vec::Vec;
use core::cmp;

#[cfg(feature = "std")]
use log::{debug, trace};

use crate::{
Expand Down Expand Up @@ -421,8 +422,7 @@ pub fn lz77_optimal_fixed<C: Cache>(
inend: usize,
store: &mut Lz77Store,
) {
let hash_pool = &*HASH_POOL;
let mut h = hash_pool.pull();
let mut h = HASH_POOL.pull();
let mut costs = Vec::with_capacity(inend - instart);
lz77_optimal_run(
lmc,
Expand Down Expand Up @@ -456,8 +456,7 @@ pub fn lz77_optimal<C: Cache>(
let mut stats = SymbolStats::default();
stats.get_statistics(&currentstore);

let hash_pool = &*HASH_POOL;
let mut h = hash_pool.pull();
let mut h = HASH_POOL.pull();
let mut costs = Vec::with_capacity(inend - instart + 1);

let mut beststats = SymbolStats::default();
Expand Down

0 comments on commit 58b1081

Please sign in to comment.